Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Creating parameters in a simple way #30

Open
jeromelecoq opened this issue May 19, 2024 · 7 comments
Open

Creating parameters in a simple way #30

jeromelecoq opened this issue May 19, 2024 · 7 comments

Comments

@jeromelecoq
Copy link

Just going through this with an eye for simplicity.

taskA = TaskA(task_parameters=TaskAParameters())

I appreciate it is important to validate parameters values against a schema but could external user just pass a dictionary instead of subclassing?

@bruno-f-cruz
Copy link
Collaborator

While I strongly disagree with this practice, if you really want to use dictionaries you can.

from typing import Literal, Dict

from pydantic import Field

from aind_behavior_curriculum import (
    Task,
    TaskParameters,
)


# --- TASKS ---
class TaskAParameters(TaskParameters):
    pass


class TaskA(Task):
    name: Literal["Task A"] = "Task A"
    task_parameters: TaskAParameters = Field(
        ..., description="Fill w/ Parameter Defaults", validate_default=True
    )

ThisIsADictionary: Dict[str, float] = {"field_a": 0, "field_b": 0.0}
Foo = TaskA(task_parameters=ThisIsADictionary)
print(Foo)
#name='Task A' description='' task_parameters=TaskAParameters(field_a=0, field_b=0.0)
print(Foo.task_parameters.model_dump()["field_a"])
#0
print(Foo.task_parameters.model_dump() == ThisIsADictionary)
#True
print(Foo.model_dump_json())
#{"name":"Task A","description":"","task_parameters":{"field_a":0,"field_b":0.0}}
print(TaskA.model_validate_json(Foo.model_dump_json()))
#name='Task A' description='' task_parameters=TaskAParameters(field_a=0, field_b=0.0)

@jeromelecoq
Copy link
Author

Why do we need to do all those sub-classing? You could not just simply use existing classes?

@jeromelecoq
Copy link
Author

An API like this :
Foo = Task(task_parameters= {"field_a": 0, "field_b": 0.0})

Would be clear to me for example.

@jeromelecoq
Copy link
Author

If you want to give a name to Task you could do :
Foo = Task(name="TaskA", task_parameters= {"field_a": 0, "field_b": 0.0})

@jeromelecoq
Copy link
Author

jeromelecoq commented May 20, 2024

I think my concern perhaps comes down to expecting all users to know all of the inner workings of pydantic. Creating curriculum should be simple even if the inner validation are rich and precise.

@bruno-f-cruz
Copy link
Collaborator

You can do it, but then you have to deal with python's proclivity for lack of typing :P. I rather encourage people to do things a bit safer. Use at your own risk....

from typing import Literal, Dict

from pydantic import Field

from aind_behavior_curriculum import (
    Task)

ThisIsADictionary: Dict[str, float] = {"field_a": 0, "field_b": 0.0}
Foo = Task(name="IAmAnUnknownTask", task_parameters=ThisIsADictionary)
print(Foo)
#name='IAmAnUnknownTask' description='' task_parameters=TaskParameters(field_a=0, field_b=0.0)
print(Foo.task_parameters.model_dump()["field_a"])
#0
print(Foo.task_parameters.model_dump() == ThisIsADictionary)
#True
print(Foo.model_dump_json())
#{"name":"IAmAnUnknownTask","description":"","task_parameters":{"field_a":0,"field_b":0.0}}
print(Task.model_validate_json(Foo.model_dump_json()))
#name='IAmAnUnknownTask' description='' task_parameters=TaskParameters(field_a=0, field_b=0.0)

@jeromelecoq
Copy link
Author

Ok. that's good.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants