@@ -62,20 +62,6 @@ class Reform(BaseModel, table=True):
6262 )
6363 simulations : List ["Simulation" ] = Relationship (back_populates = "reform" )
6464
65- def __init__ (self , ** kwargs ):
66- super ().__init__ (** kwargs )
67-
68- if "parameters_dict" in kwargs :
69- # Create ParameterChange objects from the provided dictionary
70- for parameter_name , changes in kwargs ["parameters_dict" ].items ():
71- for time_period , value in changes .items ():
72- parameter_change = ParameterChange (
73- parameter_name = parameter_name ,
74- time_period = time_period ,
75- value = value ,
76- )
77- self .parameter_changes .append (parameter_change )
78-
7965
8066class Parameter (BaseModel , table = True ):
8167 """Tax or benefit parameter definition"""
@@ -190,14 +176,14 @@ class VariableState(BaseModel, table=True):
190176 entity_id : int = Field (foreign_key = "entity.id" )
191177 time_period : str # '2025'
192178 value : str # '30000'
193- simulation_run_id : Optional [int ] = Field (
194- default = None , foreign_key = "simulationrun .id"
179+ simulation_id : Optional [int ] = Field (
180+ default = None , foreign_key = "simulation .id"
195181 )
196182
197183 # Relationships
198184 variable : Variable = Relationship (back_populates = "variable_states" )
199185 entity : Entity = Relationship (back_populates = "variable_states" )
200- simulation_run : Optional ["Simulation" ] = Relationship (
186+ simulation : Optional ["Simulation" ] = Relationship (
201187 back_populates = "variable_states"
202188 )
203189
@@ -227,136 +213,3 @@ def create_db_and_tables(connection_string="sqlite:///tax_policy.db"):
227213 engine = create_engine (connection_string )
228214 SQLModel .metadata .create_all (engine )
229215 return engine
230-
231-
232- # Example data creation for UK tax parameter change
233- def add_uk_sim ():
234- """Create example data for the UK tax rate change scenario"""
235- Path ("tax_policy.db" ).unlink (missing_ok = True )
236- engine = create_db_and_tables ()
237-
238- from policyengine import Simulation
239-
240- sim = Simulation (
241- country = "uk" ,
242- scope = "macro" ,
243- subsample = 1000 ,
244- )
245-
246- person_df = sim .baseline_simulation .calculate_dataframe (
247- ["person_id" , "age" ]
248- )
249- household_df = sim .baseline_simulation .calculate_dataframe (
250- ["household_id" , "household_net_income" ]
251- )
252-
253- with Session (engine ) as session :
254- # Create countries
255- uk = Country (code = "uk" , name = "United Kingdom" )
256- us = Country (code = "us" , name = "United States" )
257- session .add (uk )
258- session .add (us )
259- session .commit ()
260-
261- # Create dataset and series
262- dataset_series = DatasetSeries (
263- name = "Enhanced FRS" , description = "Enhanced Family Resources Survey"
264- )
265- session .add (dataset_series )
266-
267- dataset = Dataset (
268- name = "EFRS 2022" ,
269- description = "Enhanced Family Resources Survey 2022" ,
270- )
271- session .add (dataset )
272- session .commit ()
273-
274- # Tag dataset
275- dataset_tag = Dataset (
276- id = 1 , # Doesn't seem to work without this
277- dataset = dataset ,
278- dataset_series = dataset_series ,
279- version = "2025.1" ,
280- )
281- session .add (dataset_tag )
282- session .commit ()
283-
284- # Add simulation run
285-
286- sim_run = Simulation (
287- country = uk ,
288- reform = None ,
289- package_version = "1.0.0" ,
290- dataset = dataset ,
291- run_date = datetime .utcnow (),
292- )
293- session .add (sim_run )
294- session .commit ()
295-
296- # Create variables
297- variable_names = list (person_df .columns ) + list (household_df .columns )
298- for variable_name in variable_names :
299- variable = Variable (
300- country = uk ,
301- name = variable_name ,
302- description = f"Variable { variable_name } for UK tax simulation" ,
303- )
304- session .add (variable )
305- session .commit ()
306-
307- # Create all person entities
308- for i in range (len (person_df )):
309- person = Entity (
310- country = uk ,
311- entity_type = "person" ,
312- dataset_tag_id = dataset_tag .id ,
313- )
314- session .add (person )
315- for variable_name in list (person_df .columns ):
316- # Get the variable object by name
317- variable = session .exec (
318- select (Variable )
319- .where (Variable .name == variable_name )
320- .where (Variable .country_id == uk .id )
321- ).one ()
322- variable_state = VariableState (
323- variable = variable ,
324- entity = person ,
325- time_period = "2025" ,
326- value = str (
327- person_df [variable_name ].iloc [i ]
328- ), # Convert to string as value is expected to be str
329- simulation_run = sim_run ,
330- )
331- session .add (variable_state )
332- session .commit ()
333-
334- # Create all household entities
335- for i in range (len (household_df )):
336- household = Entity (
337- country = uk ,
338- entity_type = "household" ,
339- dataset_tag_id = dataset_tag .id ,
340- )
341- session .add (household )
342- for variable_name in list (household_df .columns ):
343- # Get the variable object by name
344- variable = session .exec (
345- select (Variable )
346- .where (Variable .name == variable_name )
347- .where (Variable .country_id == uk .id )
348- ).one ()
349- variable_state = VariableState (
350- variable = variable ,
351- entity = household ,
352- time_period = "2025" ,
353- value = household_df [variable_name ].iloc [i ],
354- )
355- session .add (variable_state )
356- session .commit ()
357-
358- print ("Successfully created example data for UK tax parameter change." )
359-
360-
361- if __name__ == "__main__" :
362- add_uk_sim ()
0 commit comments