Conversation
|
OK Please do not remove the method |
No, the type field is still referring to a module. Nothing has changed in the yaml file. I also likely want to change it to refer to a class rather than a module later but that's not part of this pull request. I know that the ConfigModel appears in the beginning but it's still confusing because people are not used to having to look at another class to understand what input parameters they have to put together to be able to create the object they are interested in. Also if I in python do which means that I need to instead do help on the ConfigModel to get the information I was interested in.
|
|
OK I see. |
|
Problem is that the PYAMLCLASS will also not work if we want to make it possible to configure third-party devices/applications and use them as if they were pyAML standard devices/applications. I don't see how we can enforce the use of either ConfigModel or PYAMLCLASS and especially not since this is code that the labs already have and aren't writing from scratch. It becomes difficult then to require a certain module structure because that would likely mean major refactoring for them. The only thing I think we can do is to enforce protocols or rely on duck typing. Or you think there is some other way? I think the ConfigModel problem can easily be fixed with this decorator that links the class to it's config class. And if you don't care about validation you can just ignore it by not adding the decorator. For the PYAMLCLASS my suggestion at the moment would be to change the config to be based on class rather than module. I think that would also be more logical for the user because you want to build an object of a class and not an object of a module. But that is another topic and more complicated because it would mean rewriting all existing config files. So it's not part of this PR because it requires it's own discussion. And maybe there is also other solutions. I will continue working on it then and see what happens if I start to add this decorator also to the other classes. |
|
I have nothing against removing the |
|
@TeresiaOlsson i.e. class ConfigModel(BaseModel):
"""
Configuration model for response matrix
Parameters
----------
matrix : list[list[float]]
Response matrix data
input_names : list[str], optional
Input names, basically the actuators
output_names : list[str]
Output names, basically the measurements
rf_response : list[float], optional
RF response data
"""
model_config = ConfigDict(arbitrary_types_allowed=True, extra="forbid")
matrix: list[list[float]]
input_names: Optional[list[str]]
output_names: list[str]
rf_response: Optional[list[float]] = None
input_planes: Optional[list[str]] = None
output_planes: Optional[list[str]] = None
@wrap_config
class ResponseMatrix(object):To allow resp : ResponseMatrix = sr.design.orbit.reponse_matrix
print(resp.input_names) |
I like the idea of supporting a For validating the interface I think the easiest is to use protocols. Then if a third-party application doesn't already fulfill them it doesn't feel so complicated to me to write a little wrapper that imports the protocols and adds the missing functionality. But maybe there are other options also. |
OK so we may also rename |
My idea was rather abstract interfaces together with the possibility to override existing tools. So, for instance, if you want to write your own tune monitor, you just override the present one add your specific stuff. This will allow to preserve the attachment to simulator part and you can do want you want for the CS part. |
You mean a decorator which unpacks the config object and makes individual attributes of the attributes in the ConfigModel? So you can do something like |
Okay. That would also work. My impression was that protocols in python is very similar to abstract classes. I'm still trying to understand what the actual difference is between |
Yes. |
Okay, that's a nice idea. Probably better than what I was trying to do to remove the |
I have started an idea to introduce a validator decorator that you can use to specify which ConfigModel to use to validate a specific class. The idea is:
Instead of enforcing that a module needs to contain a main class defined by the
PYAMLCLASSand a configuration model that has to be namedConfigModeluse a decorator to attach a validation model to a specific class. For example like:This would add an attribute
_validator_modelbut also two functionsvalidateandfrom_validated.validateallows you to validate the input data without creating an object of the class (for example if you want to create the dict separately but want some way to check that it's okay before loading it).from_validatedvalidates the input data and creates the object.This would make it easier to integrate devices/applications from third-party packages where it's not possible to enforce a certain module structure. As long as a class has the attributes/methods required by pyAML I think it should be possible to integrate and configure it in the same way as the standard pyAML classes despite that the module has been written differently.
The docs are currently difficult to understand because you can't see which attributes a class needs without having to also look at the config class. Instead I suggest to not build the config model into the class but separate validation and object creation.
This means you need to write the required attributes twice which is a bit annoying, but at the same time it makes it possible to create objects without mandatory validation (which could be useful if you already have a validated config and don't need to validate every time) and make the dependency of pydantic lighter.
I have so far made a test only including the
Acceleratorto show the idea and try how it can be implemented. Before I continue I would like input on what you think about the idea and my suggestion for how to implement it?I know the field_locations are currently not included. I'm working on getting them back.
It will also not yet work for building any other objects than the accelerator. For testing only a minimal config will work so far: