Autumn School Dynamic MSM16-18 November 2015 | L-Esch-sur-Alzette Slide 1 Advanced topics
Autumn School Dynamic MSM16-18 November 2015 | L-Esch-sur-Alzette Slide 2 Topics 1.Advanced globals 2.Import other models 3.Functions with arguments 4.Loops
Autumn School Dynamic MSM16-18 November 2015 | L-Esch-sur-Alzette Slide 3 Advanced globals
Autumn School Dynamic MSM16-18 November 2015 | L-Esch-sur-Alzette Slide 4 Globals - in csv file globals: periodic: path: param\globals.csv fields: # PERIOD is implicit - WEMRA: float No need to reimport dataset when globals change Different globals with same dataset (variants) Usage is the same
Autumn School Dynamic MSM16-18 November 2015 | L-Esch-sur-Alzette Slide 5 globals: othertable: path: param\othertable.csv fields: # PERIOD is NOT implicit - PERIOD: int - INTFIELD: int - FLOATFIELD: float […] processes : test_globals : - result : othertable.INTFIELD - result : othertable.INTFIELD[period] Globals - other tables with PERIOD field
Autumn School Dynamic MSM16-18 November 2015 | L-Esch-sur-Alzette Slide 6 globals: othertable2: fields: - INTFIELD: int […] processes: test_globals: # explicit index - result: othertable2.INTFIELD[0] # expr index - row_number: trunc((period ) / 10) - result: othertable2.INTFIELD[row_number] Globals - other tables without PERIOD field Row 0
Autumn School Dynamic MSM16-18 November 2015 | L-Esch-sur-Alzette Slide 7 globals: ARRAY: type: float MIG: path: param\mig.csv type: float […] processes: test_globals: - result: MIG * 2.5 Globals - (multi dimensional) arrays
Autumn School Dynamic MSM16-18 November 2015 | L-Esch-sur-Alzette Slide 8 Import other models (demo09.yml)
Autumn School Dynamic MSM16-18 November 2015 | L-Esch-sur-Alzette Slide 9 Re-use other model(s) inside a model/file Usage Split large model files Variants How? Merge ! "importing model" items replaces "imported model" items with the same name new fields can be added, existing fields definitions are replaced if present functions can be added, existing functions can be (completely) replaced (simulation) processes lists: replaced if present each file does not need to be a valid model (only the merged result)! Imports
Autumn School Dynamic MSM16-18 November 2015 | L-Esch-sur-Alzette Slide 10 import: imported.yml # Or import: - imported1.yml - imported2.yml entities: [everything that changes] simulation: [everything that changes] Imports - format
Autumn School Dynamic MSM16-18 November 2015 | L-Esch-sur-Alzette Slide 11 import: demo08.yml entities: person: fields: # additional field not present in the base model # (nor in the input in this case) - study_years_failed: {type: int, initialdata: False} processes: # we override the ineducation process ineducation: - … Imports - small example
Autumn School Dynamic MSM16-18 November 2015 | L-Esch-sur-Alzette Slide 12 Functions with arguments
Autumn School Dynamic MSM16-18 November 2015 | L-Esch-sur-Alzette Slide 13 function_name(arg1, arg2): - code - return result other_func: - result: function_name(value1, value2) Functions with arguments
Autumn School Dynamic MSM16-18 November 2015 | L-Esch-sur-Alzette Slide 14 myaverage(a, b): - c: a + b - return c / 2 other_func: # 1.5 (scalar) - result1: myaverage(1, 2) # one value per person - result2: myaverage(age, partner.age) Functions with arguments - example
Autumn School Dynamic MSM16-18 November 2015 | L-Esch-sur-Alzette Slide 15 While loops
Autumn School Dynamic MSM16-18 November 2015 | L-Esch-sur-Alzette Slide 16 - while scalar_condition : - the_code_to_repeat condition must be a scalar expression single value for all individuals number of iterations is always the same for all individuals count_to_5: - i: 1 - while i <= 5: - show(i) - i: i + 1 while loops
Autumn School Dynamic MSM16-18 November 2015 | L-Esch-sur-Alzette Slide 17 while loops repeat_while_below_1: - score: age / max(age) - while score < 1: # <-- this line is WRONG ! - score: score show(score) ValueError: The truth value of an array with more than one element is ambiguous.Use a.any() or a.all()
Autumn School Dynamic MSM16-18 November 2015 | L-Esch-sur-Alzette Slide 18 while loops repeat_while_below_1: - score: age / max(age) - while any(score < 1): - score: score show(score) repeat_while_below_1: - score: age / max(age) - while any(score < 1): - score: if(score < 1, score + 0.1, score) - show(score) Or
Autumn School Dynamic MSM16-18 November 2015 | L-Esch-sur-Alzette Slide 19 Exercises (demo05.yml)
Autumn School Dynamic MSM16-18 November 2015 | L-Esch-sur-Alzette Slide Split "demo05.yml" into "demo05fields.yml" containing fields definition and "demo05rest.yml" and run "demo05rest.yml" to check it works 2. Create "demo05variant.yml" reuse (import -- do NOT modify it in place) demo05.yml change agegroup to be groups of 5 years including for people 50+ put all output in a directory called "output_variant" Exercises - imports