-
Notifications
You must be signed in to change notification settings - Fork 0
/
curriculum.py
217 lines (143 loc) · 5.14 KB
/
curriculum.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
"""
Example of Curriculum creation
"""
import json
from typing import Literal, Union
from pydantic import Field
from aind_behavior_curriculum import (
GRADUATED,
INIT_STAGE,
BehaviorGraph,
Curriculum,
Graduated,
Metrics,
ModifiableAttr,
Policy,
PolicyTransition,
Stage,
StageTransition,
Task,
TaskParameters,
)
# --- TASKS ---
class TaskAParameters(TaskParameters):
field_a: int = ModifiableAttr(default=0)
class TaskA(Task):
name: Literal["Task A"] = "Task A"
task_parameters: TaskAParameters = Field(
..., description="Fill w/ Parameter Defaults"
)
class TaskBParameters(TaskParameters):
field_b: float = ModifiableAttr(default=0.0)
class TaskB(Task):
name: Literal["Task B"] = "Task B"
task_parameters: TaskBParameters = Field(
..., description="Fill w/ Parameter Defaults"
)
# --- METRICS ---
class ExampleMetrics(Metrics):
"""
Totally made up values we will edit ourselves to simulate mouse training.
Each theta value is reserved for a test case.
"""
theta_1: int = ModifiableAttr(default=0)
theta_2: int = ModifiableAttr(default=0)
theta_3: int = ModifiableAttr(default=0)
# --- STAGES ---
class StageA(Stage):
name: Literal["Stage A"] = "Stage A"
task: TaskA = Field(..., description="Fill with Task Instance")
class StageB(Stage):
name: Literal["Stage B"] = "Stage B"
task: TaskB = Field(..., description="Fill with Task Instance")
# --- POLICIES ---
def stageA_policyA_rule(
metrics: ExampleMetrics, task_params: TaskAParameters
) -> TaskAParameters:
task_params = task_params.model_copy(deep=True)
task_params.field_a = 8
return task_params
stageA_policyA = Policy(rule=stageA_policyA_rule)
def stageA_policyB_rule(
metrics: ExampleMetrics, task_params: TaskAParameters
) -> TaskAParameters:
task_params = task_params.model_copy(deep=True)
task_params.field_a = 16
return task_params
stageA_policyB = Policy(rule=stageA_policyB_rule)
def stageB_policyA_rule(
metrics: ExampleMetrics, task_params: TaskBParameters
) -> TaskBParameters:
task_params = task_params.model_copy(deep=True)
task_params.field_b = 8
return task_params
stageB_policyA = Policy(rule=stageB_policyA_rule)
def stageB_policyB_rule(
metrics: ExampleMetrics, task_params: TaskBParameters
) -> TaskBParameters:
task_params = task_params.model_copy(deep=True)
task_params.field_b = 16
return task_params
stageB_policyB = Policy(rule=stageB_policyB_rule)
# --- POLICY TRANSTITIONS ---
def t1_5_rule(metrics: ExampleMetrics) -> bool:
return metrics.theta_1 > 5
t1_5 = PolicyTransition(rule=t1_5_rule)
def t1_10_rule(metrics: ExampleMetrics) -> bool:
return metrics.theta_1 > 10
t1_10 = PolicyTransition(rule=t1_10_rule)
def t3_5_rule(metrics: ExampleMetrics) -> bool:
return metrics.theta_3 > 5
t3_5 = PolicyTransition(rule=t3_5_rule)
def t3_10_rule(metrics: ExampleMetrics) -> bool:
return metrics.theta_3 > 10
t3_10 = PolicyTransition(rule=t3_10_rule)
# --- STAGE TRANSITIONS ---
def t2_5_rule(metrics: ExampleMetrics) -> bool:
return metrics.theta_2 > 5
t2_5 = StageTransition(rule=t2_5_rule)
def t2_10_rule(metrics: ExampleMetrics) -> bool:
return metrics.theta_2 > 10
t2_10 = StageTransition(rule=t2_10_rule)
# --- CURRICULUM ---
Curriculum_Stages = Union[StageA, StageB, Graduated]
class MyCurriculum(Curriculum):
name: Literal["My Curriculum"] = "My Curriculum"
graph: BehaviorGraph[Curriculum_Stages, StageTransition] = BehaviorGraph[
Curriculum_Stages, StageTransition
]()
def construct_curriculum() -> MyCurriculum:
"""
Useful for testing.
"""
# Init Stages
taskA = TaskA(task_parameters=TaskAParameters())
taskB = TaskB(task_parameters=TaskBParameters())
stageA = StageA(task=taskA)
stageB = StageB(task=taskB)
stageA.add_policy_transition(INIT_STAGE, stageA_policyB, t1_10)
stageA.add_policy_transition(INIT_STAGE, stageA_policyA, t1_5)
stageA.add_policy_transition(stageA_policyA, stageA_policyB, t1_10)
stageA.set_start_policies(INIT_STAGE)
stageB.add_policy_transition(INIT_STAGE, stageB_policyB, t3_10)
stageB.add_policy_transition(INIT_STAGE, stageB_policyA, t3_5)
stageB.add_policy_transition(stageB_policyA, stageB_policyB, t3_10)
stageB.set_start_policies(INIT_STAGE)
# Construct the Curriculum
ex_curr = MyCurriculum(name="My Curriculum")
ex_curr.add_stage_transition(stageA, GRADUATED, t2_10)
ex_curr.add_stage_transition(stageA, stageB, t2_5)
ex_curr.add_stage_transition(stageB, GRADUATED, t2_10)
return ex_curr
if __name__ == "__main__":
ex_curr = construct_curriculum()
with open("examples/stage_instance.json", "w") as f:
stageA = ex_curr.see_stages()[0]
json_dict = stageA.model_dump()
json_string = json.dumps(json_dict, indent=4)
f.write(json_string)
with open("examples/curr_instance.json", "w") as f:
json_dict = ex_curr.model_dump()
json_string = json.dumps(json_dict, indent=4)
f.write(json_string)
ex_curr.export_diagram(output_directory="examples/my_curr_diagram/")