math-spec-mapping 0.3.3__py3-none-any.whl → 0.3.4__py3-none-any.whl

Sign up to get free protection for your applications and to get access to all the features.
@@ -8,6 +8,7 @@ class BoundaryAction(Block):
8
8
  super().__init__(data)
9
9
  self.boundary_action_options = data["boundary_action_options"]
10
10
  self.block_type = "Boundary Action"
11
+ self.model_name = self.name.replace(" ", "_").lower()
11
12
 
12
13
 
13
14
  class BoundaryActionOption:
@@ -15,3 +16,4 @@ class BoundaryActionOption:
15
16
  self.name = data["name"]
16
17
  self.description = data["description"]
17
18
  self.logic = data["logic"]
19
+ self.implementations = data["implementations"]
@@ -364,7 +364,7 @@ class MathSpec:
364
364
  ]
365
365
  opts.extend(
366
366
  [
367
- (x, x.boundary_actions)
367
+ (x, x.boundary_action_options)
368
368
  for x in self.boundary_actions.values()
369
369
  if len(x.boundary_action_options) > 1
370
370
  ]
@@ -604,6 +604,34 @@ class MathSpec:
604
604
  with open(path, "w") as f:
605
605
  f.write(out)
606
606
 
607
+ def metaprogramming_boundary_actions(
608
+ self, model_directory, overwrite=False, default_values=None
609
+ ):
610
+ path = model_directory + "/boundary_actions.py"
611
+ if not overwrite:
612
+ assert "boundary_actions.py" not in os.listdir(
613
+ model_directory
614
+ ), "The boundary actions file is already written, either delete it or switch to overwrite mode"
615
+ out = ""
616
+
617
+ unique_spaces = set().union(
618
+ *[x.all_spaces_used for x in self.boundary_actions.values()]
619
+ )
620
+ unique_spaces = [x.name_variable for x in unique_spaces]
621
+
622
+ out += "from .spaces import {}".format(", ".join(unique_spaces))
623
+ out += "\n\n"
624
+ for x in self.boundary_actions.values():
625
+ out += "def "
626
+ out += x.model_name
627
+ out += "(state, params) -> ({}):".format(
628
+ ", ".join([y.name_variable for y in x.codomain])
629
+ )
630
+ out += "\n\n"
631
+
632
+ with open(path, "w") as f:
633
+ f.write(out)
634
+
607
635
  def metaprogramming_julia_types(self, model_directory, overwrite=False):
608
636
  path = model_directory + "/types.jl"
609
637
  if not overwrite:
@@ -652,7 +680,7 @@ class MathSpecImplementation:
652
680
  self.ms = deepcopy(ms)
653
681
  self.params = params
654
682
  self.control_actions = self.load_control_actions()
655
- self.boundary_actions = {}
683
+ self.boundary_actions = self.load_boundary_actions()
656
684
  self.policies = self.load_policies()
657
685
  self.mechanisms = self.load_mechanisms()
658
686
  self.load_wiring()
@@ -670,24 +698,64 @@ class MathSpecImplementation:
670
698
  else:
671
699
  assert (
672
700
  "FP {}".format(ca.name) in self.params
673
- ), "No functional parameterization for {}".format(ca.name)
701
+ ), "No functional parameterization for {}. To fix this error, add {} to the parameters passed to ms.build_implementation. Option can be: {}".format(
702
+ ca.name, "FP " + ca.name, [x.name for x in opts]
703
+ )
674
704
  opt = self.ms.functional_parameters["FP {}".format(ca.name)][
675
705
  self.params["FP {}".format(ca.name)]
676
706
  ]
677
707
 
678
- assert (
679
- "python" in opt.implementations
680
- ), "No python implementation for {} / {}".format(ca.name, opt.name)
681
-
682
- control_actions[ca.name] = opt.implementations["python"]
708
+ if "python" not in opt.implementations:
709
+ print(
710
+ "No python implementation for {} / {}. To fix this, go to Implementations/Python/ControlActions and add {}".format(
711
+ ca.name, opt.name, opt.name
712
+ )
713
+ )
714
+ else:
715
+ control_actions[ca.name] = opt.implementations["python"]
683
716
  return control_actions
684
717
 
718
+ def load_boundary_actions(self):
719
+ boundary_actions = {}
720
+ for ba in self.ms.boundary_actions:
721
+ ba = self.ms.boundary_actions[ba]
722
+ opts = ba.boundary_action_options
723
+ if len(opts) == 0:
724
+ print("{} has no boundary action options".format(ba.name))
725
+ else:
726
+ if len(opts) == 1:
727
+ opt = opts[0]
728
+ else:
729
+ assert (
730
+ "FP {}".format(ba.name) in self.params
731
+ ), "No functional parameterization for {}. To fix this error, add {} to the parameters passed to ms.build_implementation. Option can be: {}".format(
732
+ ba.name, ba.name, [x.name for x in opts]
733
+ )
734
+
735
+ opt = self.ms.functional_parameters["FP {}".format(ba.name)][
736
+ self.params["FP {}".format(ba.name)]
737
+ ]
738
+
739
+ if "python" not in opt.implementations:
740
+ print(
741
+ "No python implementation for {} / {}. To fix this, go to Implementations/Python/BoundaryActions and add {}".format(
742
+ ba.name, opt.name, opt.name
743
+ )
744
+ )
745
+ else:
746
+ boundary_actions[ba.name] = opt.implementations["python"]
747
+ return boundary_actions
748
+
685
749
  def load_mechanisms(self):
686
750
  mechanisms = {}
687
751
  for m in self.ms.mechanisms:
688
752
  m = self.ms.mechanisms[m]
689
753
  if "python" not in m.implementations:
690
- print("No python implementation for {}".format(m.name))
754
+ print(
755
+ "No python implementation for {}. To fix this, go to Implementations/Python/Mechanisms and add {}".format(
756
+ m.name, m.name
757
+ )
758
+ )
691
759
  else:
692
760
  mechanisms[m.name] = m.implementations["python"]
693
761
  return mechanisms
@@ -737,14 +805,18 @@ class MathSpecImplementation:
737
805
  else:
738
806
  assert (
739
807
  "FP {}".format(p.name) in self.params
740
- ), "No functional parameterization for {}".format(p.name)
808
+ ), "No functional parameterization for {}. To fix this error, add {} to the parameters passed to ms.build_implementation. Option can be: {}".format(
809
+ p.name, p.name, [x.name for x in opts]
810
+ )
741
811
  opt = self.ms.functional_parameters["FP {}".format(p.name)][
742
812
  self.params["FP {}".format(p.name)]
743
813
  ]
744
814
 
745
815
  if "python" not in opt.implementations:
746
816
  print(
747
- "No python implementation for {} / {}".format(p.name, opt.name)
817
+ "No python implementation for {} / {}. To fix this, go to Implementations/Python/Policies and add {}".format(
818
+ p.name, opt.name, opt.name
819
+ )
748
820
  )
749
821
  else:
750
822
  policies[p.name] = opt.implementations["python"]
@@ -34,6 +34,17 @@ def convert_boundary_action(data: Dict, ms: Dict) -> BoundaryAction:
34
34
  new_bao = []
35
35
  for ba in data["boundary_action_options"]:
36
36
  check_json_keys(ba, "Boundary Action Option")
37
+
38
+ ba["implementations"] = {}
39
+ if "python" in ms["Implementations"]:
40
+ if "boundary_action_options" in ms["Implementations"]["python"]:
41
+ if (
42
+ ba["name"]
43
+ in ms["Implementations"]["python"]["boundary_action_options"]
44
+ ):
45
+ ba["implementations"]["python"] = ms["Implementations"]["python"][
46
+ "boundary_action_options"
47
+ ][ba["name"]]
37
48
  new_bao.append(BoundaryActionOption(ba))
38
49
  data["boundary_action_options"] = new_bao
39
50
 
@@ -7,7 +7,9 @@ def load_wiring(ms, json):
7
7
  ms["Displays"]["Wiring"] = []
8
8
  for display in json["Displays"]["wiring"]:
9
9
  for component in display["components"]:
10
- assert component in ms["Blocks"], "{} is not a valid block".format(
11
- component
10
+ assert (
11
+ component in ms["Blocks"]
12
+ ), "{} referenced in {} is not a valid block".format(
13
+ component, display["name"]
12
14
  )
13
15
  ms["Displays"]["Wiring"].append(display)
@@ -25,7 +25,9 @@ def convert_state(ms, data: Dict) -> State:
25
25
  new_variables = []
26
26
  for var in data["variables"]:
27
27
  check_json_keys(var, "State Variable")
28
- assert var["type"] in ms["Types"], "Type {} not in ms".format(var["name"])
28
+ assert var["type"] in ms["Types"], "Type {} referenced by {} not in ms".format(
29
+ var["type"], var["name"]
30
+ )
29
31
  var["type"] = ms["Types"][var["type"]]
30
32
  if "metadata" not in var:
31
33
  var["metadata"] = {}
@@ -577,7 +577,7 @@ def write_metrics_markdown_report(ms, path, metric, add_metadata=True):
577
577
  out += "\n\n"
578
578
 
579
579
  out += "## Parameters Used\n"
580
- for i, x in enumerate(sorted(metric.parameters_used, key=lambda x: x.name)):
580
+ for i, x in enumerate(sorted(metric.parameters_used, key=lambda x: x)):
581
581
  out += "{}. [[{}]]".format(i + 1, x)
582
582
  var = ms.parameters.parameter_map[x]
583
583
  if var.symbol:
@@ -711,7 +711,7 @@ def write_wiring_display_markdown_report(ms, path, wiring, add_metadata=True):
711
711
  parameters = set().union(*parameters)
712
712
  parameters = sorted(parameters, key=lambda x: x)
713
713
  out += "## Unique Parameters Used\n"
714
- for i, x in enumerate(sorted(parameters, key=lambda x: x.name)):
714
+ for i, x in enumerate(sorted(parameters, key=lambda x: x)):
715
715
  out += "{}. [[{}]]".format(i + 1, x)
716
716
  out += "\n"
717
717
  out += "\n"
@@ -130,7 +130,7 @@
130
130
  },
131
131
  "control_action_options": {
132
132
  "type": "array",
133
- "items": {},
133
+ "items": {"$ref": "./schema.schema.json/#/definitions/ControlActionOption"},
134
134
  "description": "Possible implementations of the control action"
135
135
  },
136
136
  "codomain": {
@@ -157,6 +157,38 @@
157
157
  "title": "ControlAction",
158
158
  "description": "The definition of actions that the system might call, such as an action to refill the stock of an item when reserves run too low or something that could get triggered from a sensor. The key differentiator from boundary actions is that there is no entity calling it and it is not done with randomness."
159
159
  },
160
+ "BoundaryActionOption": {"type": "object",
161
+ "additionalProperties": false,
162
+ "properties": {"name": {"type": "string",
163
+ "description": "The name of the boundary action option"},
164
+ "description": {"type": "string",
165
+ "description": "A description of what this implementation does"},
166
+ "logic": {"type": "string",
167
+ "description": "The logic related to the implementation"},
168
+ "metadata": {"type": "object"}},
169
+ "title": "BoundaryActionOption",
170
+ "required": [
171
+ "name",
172
+ "description",
173
+ "logic"
174
+ ],
175
+ "description": "Specific implementations of a control action which are in the same form of the underlying control action definition."},
176
+ "ControlActionOption": {"type": "object",
177
+ "additionalProperties": false,
178
+ "properties": {"name": {"type": "string",
179
+ "description": "The name of the control action option"},
180
+ "description": {"type": "string",
181
+ "description": "A description of what this implementation does"},
182
+ "logic": {"type": "string",
183
+ "description": "The logic related to the implementation"},
184
+ "metadata": {"type": "object"}},
185
+ "title": "ControlActionOption",
186
+ "required": [
187
+ "name",
188
+ "description",
189
+ "logic"
190
+ ],
191
+ "description": "Specific implementations of a control action which are in the same form of the underlying control action definition."},
160
192
  "Entity": {
161
193
  "type": "object",
162
194
  "additionalProperties": false,
@@ -469,7 +501,7 @@
469
501
  },
470
502
  "boundary_action_options": {
471
503
  "type": "array",
472
- "items": {},
504
+ "items": {"$ref": "./schema.schema.json/#/definitions/BoundaryActionOption"},
473
505
  "description": "The options for implementation of the boundary action"
474
506
  },
475
507
  "called_by": {
@@ -1,6 +1,6 @@
1
1
  Metadata-Version: 2.1
2
2
  Name: math-spec-mapping
3
- Version: 0.3.3
3
+ Version: 0.3.4
4
4
  Summary: A library for easy mapping of mathematical specifications.
5
5
  Author-email: Sean McOwen <Sean@Block.Science>
6
6
  Classifier: Programming Language :: Python :: 3
@@ -1,12 +1,12 @@
1
1
  math_spec_mapping/__init__.py,sha256=CzT9KycdX5nuiUzDFmLXVeAIr8v8UKGbXsEQF8vjl1c,961
2
2
  math_spec_mapping/schema.py,sha256=6mrRqzEnTTSXjb19xJ63MBp0KjKH0s7i6TfT4MkAY9k,233
3
- math_spec_mapping/schema.schema.json,sha256=5_DiubQzPo8koyx5PKFh_zIINyjtrhGWG_7N73s9b1Q,29400
3
+ math_spec_mapping/schema.schema.json,sha256=hJP2TcV5WPFPmx4u_A5U1xtnpkE1LeYaTeYOXadTot0,30916
4
4
  math_spec_mapping/Classes/ActionTransmissionChannel.py,sha256=zWMo5QsgPh5WGIWXl-xOrZNMXYJXmK6Vejw1dQvi0og,246
5
5
  math_spec_mapping/Classes/Block.py,sha256=OsWM1ZAi6ZvavDhMFJi_H7w8ZF9LqJIKR3ZLTXnts9w,17437
6
- math_spec_mapping/Classes/BoundaryAction.py,sha256=AOENCqCEfpjotnHhzUj_F2SOP0SGpkN1tNPr8Mtl6Tc,476
6
+ math_spec_mapping/Classes/BoundaryAction.py,sha256=KuZqtvZwSHWDP7gFQXGnG-r3EuYJU_oU9wMCuqvRkzk,593
7
7
  math_spec_mapping/Classes/ControlAction.py,sha256=ysqpgANwdizVdwqtgZmnxXMpCqrzmVEF_6YT0M3l4Ho,526
8
8
  math_spec_mapping/Classes/Entity.py,sha256=fA0-b128_OHHxfCg4pzqyQV083EYev1HlVpy86S5igg,1226
9
- math_spec_mapping/Classes/MathSpec.py,sha256=KEk7oUs21YznNmsdCUtldpOl7p-rwFmpZcolVNHlaTo,28854
9
+ math_spec_mapping/Classes/MathSpec.py,sha256=AcfqbhYWhqpm3ybK3LmBClQ0mEMmIX9k2S1Snha-aDQ,32143
10
10
  math_spec_mapping/Classes/Mechanism.py,sha256=2sLm3wYBIeTQaMBcsJ9btqIWsbS895Ra8NY6Y9_G_Dg,379
11
11
  math_spec_mapping/Classes/Metric.py,sha256=AhPgYppOP6q49xvR8S9STxQsXUKJlTWx7wI1LfZEtww,581
12
12
  math_spec_mapping/Classes/Parameter.py,sha256=ZuJ_w0sChvRElJ4sOnXZ2EV4Ell2xXFulKLjVOpgz2E,1237
@@ -22,9 +22,9 @@ math_spec_mapping/Convenience/documentation.py,sha256=1ziWVJznbCUxeAAt03nAdEYtMl
22
22
  math_spec_mapping/Convenience/starter.py,sha256=Pv0b5pHnv5Bxk3PkY8O9Nv62z2C_gxj5xcf8G9r4Pkw,11010
23
23
  math_spec_mapping/Load/__init__.py,sha256=_ga5nHi7U5rY5lCF36_XI9Qmybq4P8R4m5I5mmjLBk8,33
24
24
  math_spec_mapping/Load/action_transmission_channel.py,sha256=9Wer7g2s5SSOcUYuZ0PqSKUVVnW3EvGQJZNXJTwW__0,2561
25
- math_spec_mapping/Load/boundary_actions.py,sha256=mKX4hUHlX4xoJzm-6W3W3AjWRC9RNiEWb0al7lN0DBs,2142
25
+ math_spec_mapping/Load/boundary_actions.py,sha256=q-jxhu8FaKNjdOdexlK6f22GDgraGa1jnL5kEzQ7mJY,2625
26
26
  math_spec_mapping/Load/control_actions.py,sha256=4E57s730W4tX5SaKXKaAvet37r8Bwt-g8Kk6EanF4TU,2042
27
- math_spec_mapping/Load/displays.py,sha256=ooHWT_OryzEkp7F3LcGywwdLMRJLxuyPK82zJ3gcyN0,414
27
+ math_spec_mapping/Load/displays.py,sha256=uQvs0Jhp8-9SXGex8SG3ibxHJu7ahAV3xLeBFbT8QEE,480
28
28
  math_spec_mapping/Load/entities.py,sha256=Ds7VQY_govWEn1vSHYVrLa8IadSNyOQzaCK18JPYPKk,1289
29
29
  math_spec_mapping/Load/general.py,sha256=2q6aGKxXhebiHHTZhtACvM4nWIkTben0o5rXuvfv2Vw,4463
30
30
  math_spec_mapping/Load/implementations.py,sha256=SGKZ_YXeNpJUTnw5fajQTXji7S2bNQo8sh-7YcIO6pk,395
@@ -36,7 +36,7 @@ math_spec_mapping/Load/policy.py,sha256=HvlhGHGJTweVs7DOI2HSL_2_diECr8ukDUmzoFLQ
36
36
  math_spec_mapping/Load/spaces.py,sha256=7zgGA57Te7T4hfuCCDElffiidWgn1lKm5E14e1yjt8M,1116
37
37
  math_spec_mapping/Load/state_update_transmission_channels.py,sha256=FJWp5n4HdtHAfof5BUQ6BnRakljatL2h8dWCapaVSc0,2238
38
38
  math_spec_mapping/Load/stateful_metrics.py,sha256=uGSTc6x6lld5xptgSUMHrO0Vg0QDRIL14C6zTg33S8o,1977
39
- math_spec_mapping/Load/states.py,sha256=WTLvonzrUQWDVnkF9OPzHeyImrxoMPmonEl5O9O6Pxg,1313
39
+ math_spec_mapping/Load/states.py,sha256=3YurI7eTNkN6nrXRFVrc58wH0VfM22XOuWE07HVpR7Y,1365
40
40
  math_spec_mapping/Load/type.py,sha256=z6cBdBNjWed7cRyA0Bj7Jd5PmtemVVh07n3mWFj_5U4,4356
41
41
  math_spec_mapping/Load/wiring.py,sha256=1dR94U5N1W_WI5rL6lYBltH25ZvApB2pIpq9r5Opkug,3083
42
42
  math_spec_mapping/Reports/__init__.py,sha256=W27I6S9Ro1hWeHmnxIokCA06awB__eYey7PvKD4Hc1s,933
@@ -44,7 +44,7 @@ math_spec_mapping/Reports/boundary_actions.py,sha256=45BPp4QjWdD-3E9ZWwqgj_nI2-Y
44
44
  math_spec_mapping/Reports/control_actions.py,sha256=NksekZKIPFSIkubttFstKFthc5AU9B9PWRLSl9j1wWs,1216
45
45
  math_spec_mapping/Reports/general.py,sha256=WOOn6Wlb8M4fsdN49FlKLwOka6vJPQ9aCUy88TL2ki0,1610
46
46
  math_spec_mapping/Reports/html.py,sha256=tqydmwFWz_1Bg_J_avG4t9LtE-DUGbOKAPNuwg6lgRI,9007
47
- math_spec_mapping/Reports/markdown.py,sha256=K1amcJrAs4dNv9mN8NeC8U97RtWGJrRmJfEkZlAn_jA,22881
47
+ math_spec_mapping/Reports/markdown.py,sha256=t6C2D6pk3Y9K9vOR0IC6rLe18tBm3JFgJ3rC9tnvt_Q,22871
48
48
  math_spec_mapping/Reports/mechanisms.py,sha256=d2Rxt3JBYvqAOAYUynl0buYVoXEHrO8EGq7GK6hK8NA,1322
49
49
  math_spec_mapping/Reports/node_map.py,sha256=FdSMDQG16NX6n9sZcH-T5xwsvgjrV9OqBHc9J_VlNK0,3129
50
50
  math_spec_mapping/Reports/parameters.py,sha256=yizNG4lNGrgrlzYYcHMGfXKDFlPw4PMDYshDqZ3YARs,535
@@ -53,8 +53,8 @@ math_spec_mapping/Reports/spaces.py,sha256=-76hR5wQBv4lsG000ypBJ-OprjsNjI-rNRMYd
53
53
  math_spec_mapping/Reports/state.py,sha256=RkqfSonar74KVC8PpA9GgI2xaHX6BrkNdAuWMZlYQfI,2321
54
54
  math_spec_mapping/Reports/tables.py,sha256=O0CNuqh3LMECq5uLjBOoxMUk5hUvkUK660FNnwWUxDY,1505
55
55
  math_spec_mapping/Reports/wiring.py,sha256=u9SvKWy6T-WJUEgFI6-zgZanoOaTTs_2YwmEceDLsV8,1618
56
- math_spec_mapping-0.3.3.dist-info/LICENSE,sha256=ObyEzSw8kgCaFbEfpu1zP4TrcAKLA0xhqHMZZfyh7N0,1069
57
- math_spec_mapping-0.3.3.dist-info/METADATA,sha256=M8t5087vsXIk7Xw-SROeZa8xWRuuMs2lkaqswl0_srU,6012
58
- math_spec_mapping-0.3.3.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
59
- math_spec_mapping-0.3.3.dist-info/top_level.txt,sha256=AImhn9wgazkdV0a9vfiphtQR8uGe2nq-ZIOp-6yUk9o,18
60
- math_spec_mapping-0.3.3.dist-info/RECORD,,
56
+ math_spec_mapping-0.3.4.dist-info/LICENSE,sha256=ObyEzSw8kgCaFbEfpu1zP4TrcAKLA0xhqHMZZfyh7N0,1069
57
+ math_spec_mapping-0.3.4.dist-info/METADATA,sha256=YxnBIW_L8GLJvqyioONJHgj3DyILY0idwpjj-G-McnE,6012
58
+ math_spec_mapping-0.3.4.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
59
+ math_spec_mapping-0.3.4.dist-info/top_level.txt,sha256=AImhn9wgazkdV0a9vfiphtQR8uGe2nq-ZIOp-6yUk9o,18
60
+ math_spec_mapping-0.3.4.dist-info/RECORD,,