Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Dev #31

Merged
merged 5 commits into from
Aug 7, 2024
Merged

Dev #31

Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 34 additions & 0 deletions .github/workflows/python-package-conda.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
name: Python Package using Conda

on: [push]

jobs:
build-linux:
runs-on: ubuntu-latest
strategy:
max-parallel: 5

steps:
- uses: actions/checkout@v4
- name: Set up Python 3.10
uses: actions/setup-python@v3
with:
python-version: '3.10'
- name: Add conda to system path
run: |
# $CONDA is an environment variable pointing to the root of the miniconda directory
echo $CONDA/bin >> $GITHUB_PATH
- name: Install current library and dependencies
run: |
pip install -e .
# - name: Lint with flake8
# run: |
# conda install flake8
# # stop the build if there are Python syntax errors or undefined names
# flake8 . --count --select=E9,F63,F7,F82 --show-source --statistics
# # exit-zero treats all errors as warnings. The GitHub editor is 127 chars wide
# flake8 . --count --exit-zero --max-complexity=10 --max-line-length=127 --statistics
- name: Test with pytest
run: |
conda install pytest
pytest
13 changes: 12 additions & 1 deletion ngcsimlib/compartment.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ def is_compartment(cls, obj):
"""
return hasattr(obj, "_is_compartment")

def __init__(self, initial_value=None, static=False, is_input=False):
def __init__(self, initial_value=None, static=False, is_input=False, display_name=None, units=None):
"""
Builds a compartment to be used inside a component. It is important
to note that building compartments
Expand All @@ -56,6 +56,8 @@ def __init__(self, initial_value=None, static=False, is_input=False):
self.path = None
self.is_input = is_input
self._is_destination = False
self._display_name = display_name
self._units = units

def _setup(self, current_component, key):
"""
Expand Down Expand Up @@ -131,3 +133,12 @@ def is_wired(self):
return True

return self._is_destination

@property
def display_name(self):
return self._display_name if self._display_name is not None else self.name

@property
def units(self):
return self._units if self._units is not None else "dimensionless"

4 changes: 2 additions & 2 deletions ngcsimlib/compilers/command_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,8 +92,8 @@ def compiled(compartment_values, **kwargs):
critical(f"Missing keyword argument \"{n}\" in compiled function."
f"\tExpected keyword arguments {needed_args}")

for exc, outs, name in exc_order:
_comps = {key: compartment_values[key] for key in needed_comps}
for exc, outs, name, comp_ids in exc_order:
_comps = {key: compartment_values[key] for key in comp_ids}
vals = exc(**kwargs, **_comps)
if len(outs) == 1:
compartment_values[outs[0]] = vals
Expand Down
19 changes: 14 additions & 5 deletions ngcsimlib/compilers/component_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,9 +37,16 @@ def parse(component, compile_key):
the compartments needed

"""
(pure_fn, output_compartments), (
args, parameters, compartments, parse_varnames) = \
get_resolver(component.__class__, compile_key)
if component.__class__.__dict__.get("auto_resolve", True):
(pure_fn, output_compartments), (
args, parameters, compartments, parse_varnames) = \
get_resolver(component.__class__, compile_key)
else:
build_method = component.__class__.__dict__.get(f"build_{compile_key}", None)
if build_method is None:
critical(f"Component {component.name} if flagged to not use resolvers but "
f"does not have a build_{compile_key} method")
return build_method(component)

if parse_varnames:
args = []
Expand Down Expand Up @@ -95,11 +102,13 @@ def compile(component, resolver):

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

comp_key_key = [(narg.split('/')[-1], narg) for narg in comp_ids]

def compiled(**kwargs):
funArgs = {narg: kwargs.get(narg) for narg in _args}
funComps = {narg.split('/')[-1]: kwargs.get(narg) for narg in comp_ids}
funComps = {knarg: kwargs.get(narg) for knarg, narg in comp_key_key}

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

exc_order.append((compiled, out_ids, component.name))
exc_order.append((compiled, out_ids, component.name, comp_ids))
return exc_order
9 changes: 7 additions & 2 deletions ngcsimlib/compilers/op_compiler.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,8 +85,13 @@ def compile(op):
else:
iids.append(str(s.path))

additional_idds = []
for _, _, _, _iids in exc_order:
additional_idds.extend(_iids)

# print(additional_idds)
def _op_compiled(**kwargs):
computed_values = [cmd(**kwargs) for cmd, _, _ in exc_order]
computed_values = [cmd(**kwargs) for cmd, _, _, _ in exc_order]
compartment_args = [kwargs.get(narg) for narg in iids]

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

return op.operation(*_args)

return (_op_compiled, [str(output)], "op")
return (_op_compiled, [str(output)], op.__class__.__name__, iids + additional_idds)
9 changes: 8 additions & 1 deletion ngcsimlib/metaComponent.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
from ngcsimlib.compartment import Compartment
from ngcsimlib.utils import get_current_context
from ngcsimlib.utils.help import Guides
from ngcsimlib.logger import debug
from ngcsimlib.logger import debug, warn


class MetaComponent(type):
Expand Down Expand Up @@ -82,7 +82,14 @@ def wrapped_init(self, *args, **kwargs):
else:
cls.pre_init(self, *args, **kwargs)
x._orig_init(self, *args, **kwargs)
args_count = self._orig_init.__code__.co_argcount
_kwargs = self._orig_init.__code__.co_varnames[:args_count]
for key, value in kwargs.items():
if key not in _kwargs:
debug(f"There is an extra param {key} in component constructor for {self.name}")
cls.post_init(self, *args, **kwargs)
if hasattr(self, "_setup"):
self._setup()

x.__init__ = wrapped_init

Expand Down
6 changes: 6 additions & 0 deletions pytest.ini
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
# content of pytest.ini
[pytest]
python_files = *_test.py
python_classes = *Test
python_functions = test_* *_test
testpaths = tests
22 changes: 22 additions & 0 deletions tests/compartment_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@

import pathlib
import sys

# NOTE: VN: Since we have installed using `pip install -e .` in the workflow file, we
# might not need to manually add to the path
sys.path.append(str(pathlib.Path(__file__).parent.parent))

# import ngcsimlib

class CompartmentTest:

def test_import(self):
success = False
try:
from ngcsimlib.compartment import Compartment
success = True
except:
success = False
assert success, "Import failed!"


Loading