Skip to content

Commit 2ad5049

Browse files
authored
Merge pull request #31 from NACLab/dev
Dev
2 parents bdc0810 + eacb6fb commit 2ad5049

File tree

8 files changed

+105
-11
lines changed

8 files changed

+105
-11
lines changed
Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,34 @@
1+
name: Python Package using Conda
2+
3+
on: [push]
4+
5+
jobs:
6+
build-linux:
7+
runs-on: ubuntu-latest
8+
strategy:
9+
max-parallel: 5
10+
11+
steps:
12+
- uses: actions/checkout@v4
13+
- name: Set up Python 3.10
14+
uses: actions/setup-python@v3
15+
with:
16+
python-version: '3.10'
17+
- name: Add conda to system path
18+
run: |
19+
# $CONDA is an environment variable pointing to the root of the miniconda directory
20+
echo $CONDA/bin >> $GITHUB_PATH
21+
- name: Install current library and dependencies
22+
run: |
23+
pip install -e .
24+
# - name: Lint with flake8
25+
# run: |
26+
# conda install flake8
27+
# # stop the build if there are Python syntax errors or undefined names
28+
# flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
29+
# # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
30+
# flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
31+
- name: Test with pytest
32+
run: |
33+
conda install pytest
34+
pytest

ngcsimlib/compartment.py

Lines changed: 12 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ def is_compartment(cls, obj):
3131
"""
3232
return hasattr(obj, "_is_compartment")
3333

34-
def __init__(self, initial_value=None, static=False, is_input=False):
34+
def __init__(self, initial_value=None, static=False, is_input=False, display_name=None, units=None):
3535
"""
3636
Builds a compartment to be used inside a component. It is important
3737
to note that building compartments
@@ -56,6 +56,8 @@ def __init__(self, initial_value=None, static=False, is_input=False):
5656
self.path = None
5757
self.is_input = is_input
5858
self._is_destination = False
59+
self._display_name = display_name
60+
self._units = units
5961

6062
def _setup(self, current_component, key):
6163
"""
@@ -131,3 +133,12 @@ def is_wired(self):
131133
return True
132134

133135
return self._is_destination
136+
137+
@property
138+
def display_name(self):
139+
return self._display_name if self._display_name is not None else self.name
140+
141+
@property
142+
def units(self):
143+
return self._units if self._units is not None else "dimensionless"
144+

ngcsimlib/compilers/command_compiler.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -92,8 +92,8 @@ def compiled(compartment_values, **kwargs):
9292
critical(f"Missing keyword argument \"{n}\" in compiled function."
9393
f"\tExpected keyword arguments {needed_args}")
9494

95-
for exc, outs, name in exc_order:
96-
_comps = {key: compartment_values[key] for key in needed_comps}
95+
for exc, outs, name, comp_ids in exc_order:
96+
_comps = {key: compartment_values[key] for key in comp_ids}
9797
vals = exc(**kwargs, **_comps)
9898
if len(outs) == 1:
9999
compartment_values[outs[0]] = vals

ngcsimlib/compilers/component_compiler.py

Lines changed: 14 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -37,9 +37,16 @@ def parse(component, compile_key):
3737
the compartments needed
3838
3939
"""
40-
(pure_fn, output_compartments), (
41-
args, parameters, compartments, parse_varnames) = \
42-
get_resolver(component.__class__, compile_key)
40+
if component.__class__.__dict__.get("auto_resolve", True):
41+
(pure_fn, output_compartments), (
42+
args, parameters, compartments, parse_varnames) = \
43+
get_resolver(component.__class__, compile_key)
44+
else:
45+
build_method = component.__class__.__dict__.get(f"build_{compile_key}", None)
46+
if build_method is None:
47+
critical(f"Component {component.name} if flagged to not use resolvers but "
48+
f"does not have a build_{compile_key} method")
49+
return build_method(component)
4350

4451
if parse_varnames:
4552
args = []
@@ -95,11 +102,13 @@ def compile(component, resolver):
95102

96103
funParams = {narg: component.__dict__[narg] for narg in params}
97104

105+
comp_key_key = [(narg.split('/')[-1], narg) for narg in comp_ids]
106+
98107
def compiled(**kwargs):
99108
funArgs = {narg: kwargs.get(narg) for narg in _args}
100-
funComps = {narg.split('/')[-1]: kwargs.get(narg) for narg in comp_ids}
109+
funComps = {knarg: kwargs.get(narg) for knarg, narg in comp_key_key}
101110

102111
return pure_fn.__func__(**funParams, **funArgs, **funComps)
103112

104-
exc_order.append((compiled, out_ids, component.name))
113+
exc_order.append((compiled, out_ids, component.name, comp_ids))
105114
return exc_order

ngcsimlib/compilers/op_compiler.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -85,8 +85,13 @@ def compile(op):
8585
else:
8686
iids.append(str(s.path))
8787

88+
additional_idds = []
89+
for _, _, _, _iids in exc_order:
90+
additional_idds.extend(_iids)
91+
92+
# print(additional_idds)
8893
def _op_compiled(**kwargs):
89-
computed_values = [cmd(**kwargs) for cmd, _, _ in exc_order]
94+
computed_values = [cmd(**kwargs) for cmd, _, _, _ in exc_order]
9095
compartment_args = [kwargs.get(narg) for narg in iids]
9196

9297
_val_loc = 0
@@ -103,4 +108,4 @@ def _op_compiled(**kwargs):
103108

104109
return op.operation(*_args)
105110

106-
return (_op_compiled, [str(output)], "op")
111+
return (_op_compiled, [str(output)], op.__class__.__name__, iids + additional_idds)

ngcsimlib/metaComponent.py

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
from ngcsimlib.compartment import Compartment
22
from ngcsimlib.utils import get_current_context
33
from ngcsimlib.utils.help import Guides
4-
from ngcsimlib.logger import debug
4+
from ngcsimlib.logger import debug, warn
55

66

77
class MetaComponent(type):
@@ -82,7 +82,14 @@ def wrapped_init(self, *args, **kwargs):
8282
else:
8383
cls.pre_init(self, *args, **kwargs)
8484
x._orig_init(self, *args, **kwargs)
85+
args_count = self._orig_init.__code__.co_argcount
86+
_kwargs = self._orig_init.__code__.co_varnames[:args_count]
87+
for key, value in kwargs.items():
88+
if key not in _kwargs:
89+
debug(f"There is an extra param {key} in component constructor for {self.name}")
8590
cls.post_init(self, *args, **kwargs)
91+
if hasattr(self, "_setup"):
92+
self._setup()
8693

8794
x.__init__ = wrapped_init
8895

pytest.ini

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
# content of pytest.ini
2+
[pytest]
3+
python_files = *_test.py
4+
python_classes = *Test
5+
python_functions = test_* *_test
6+
testpaths = tests

tests/compartment_test.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
2+
import pathlib
3+
import sys
4+
5+
# NOTE: VN: Since we have installed using `pip install -e .` in the workflow file, we
6+
# might not need to manually add to the path
7+
sys.path.append(str(pathlib.Path(__file__).parent.parent))
8+
9+
# import ngcsimlib
10+
11+
class CompartmentTest:
12+
13+
def test_import(self):
14+
success = False
15+
try:
16+
from ngcsimlib.compartment import Compartment
17+
success = True
18+
except:
19+
success = False
20+
assert success, "Import failed!"
21+
22+

0 commit comments

Comments
 (0)