Skip to content

Commit 98df2d1

Browse files
committed
Loading
Added saving processes to contexts, changed ops to use path's to allow for cross model operations
1 parent 79072ff commit 98df2d1

File tree

2 files changed

+32
-8
lines changed

2 files changed

+32
-8
lines changed

ngcsimlib/context.py

Lines changed: 30 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,11 @@
11
from ngcsimlib.utils import make_unique_path, check_attributes, \
22
check_serializable, load_from_path, get_compartment_by_name, \
33
get_context, add_context, get_current_path, get_current_context, \
4-
set_new_context, load_module, is_pre_loaded, GuideList
4+
set_new_context, load_module, GuideList, infer_context
55
from ngcsimlib.logger import warn, info, critical
66
from ngcsimlib import preload_modules
77
from ngcsimlib.compilers import dynamic_compile, wrap_command
8+
from ngcsimlib.compilers.process import Process
89
from ngcsimlib.component import Component
910
from ngcsimlib.configManager import get_config
1011
import json, os, shutil, copy
@@ -73,7 +74,7 @@ def __init__(self, name, should_validate=None):
7374
self.path = get_current_path() + "/" + str(name)
7475
self._last_context = ""
7576

76-
self._json_objects = {"ops": [], "components": {}, "commands": {}}
77+
self._json_objects = {"ops": [], "components": {}, "commands": {}, "processes" : []}
7778

7879
if should_validate is None:
7980
_base_config = get_config("context")
@@ -200,6 +201,11 @@ def register_component(self, component, *args, **kwargs):
200201
"kwargs": _kwargs}
201202
self._json_objects['components'][c_path] = obj
202203

204+
def register_process(self, process):
205+
self._json_objects['processes'].append(process)
206+
207+
208+
203209
def add_component(self, component):
204210
"""
205211
Adds a component to the context if it does not exist already in the
@@ -278,6 +284,12 @@ def save_to_json(self, directory, model_name=None, custom_save=True,
278284
with open(path + "/commands.json", 'w') as fp:
279285
json.dump(self._json_objects['commands'], fp, indent=4)
280286

287+
with open(path + "/processes.json", 'w') as fp:
288+
objs = []
289+
for process in self._json_objects['processes']:
290+
objs.append(process.as_obj())
291+
json.dump(objs, fp, indent=4)
292+
281293
with open(path + "/components.json", 'w') as fp:
282294
hyperparameters = {}
283295
_components = copy.deepcopy(self._json_objects['components'])
@@ -349,14 +361,16 @@ def load_from_dir(self, directory, custom_folder="/custom"):
349361
components. (Default: `/custom`)
350362
"""
351363

352-
if os.path.isfile(directory + "/modules.json") and not is_pre_loaded():
364+
if os.path.isfile(directory + "/modules.json"):
353365
info("No modules file loaded, loading from model directory")
354366
preload_modules(path=directory + "/modules.json")
355367

356368
self.make_components(directory + "/components.json",
357369
directory + custom_folder)
358370
self.make_ops(directory + "/ops.json")
359371
self.make_commands(directory + "/commands.json")
372+
self.make_process(directory + "/processes.json")
373+
360374

361375
def make_components(self, path_to_components_file, custom_file_dir=None):
362376
"""
@@ -445,18 +459,28 @@ def _make_op(self, op_spec):
445459
_sources.append(self._make_op(s))
446460
else:
447461
_sources.append(
448-
get_compartment_by_name(get_current_context(), s))
462+
get_compartment_by_name(infer_context(s, trailing_path=2),
463+
"/".join(s.split("/")[-2:])))
449464

450465
obj = klass(*_sources)
451466

452467
if op_spec['destination'] is None:
453468
return obj
454469

455470
else:
456-
dest = get_compartment_by_name(get_current_context(),
457-
op_spec['destination'])
471+
d = op_spec['destination']
472+
dest = get_compartment_by_name(infer_context(d, trailing_path=2),
473+
"/".join(d.split("/")[-2:]))
458474
dest << obj
459475

476+
def make_process(self, path_to_process_file):
477+
with open(path_to_process_file, 'r') as file:
478+
process_spec = json.load(file)
479+
480+
all_processes = [Process.make_process(p) for p in process_spec]
481+
for p in all_processes:
482+
self.add_command(p.pure, p.name)
483+
460484
@staticmethod
461485
def dynamicCommand(fn):
462486
"""

ngcsimlib/operations/baseOp.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -95,9 +95,9 @@ def dump(self):
9595
if isinstance(source, BaseOp):
9696
source_array.append(source.dump())
9797
else:
98-
source_array.append(source.name)
98+
source_array.append(source.path)
9999

100-
destination = self.destination.name if self.destination is not None \
100+
destination = self.destination.path if self.destination is not None \
101101
else None
102102

103103
return {"class": class_name, "sources": source_array,

0 commit comments

Comments
 (0)