Skip to content

Commit 371ecc5

Browse files
committed
Comment Styling Fix
1 parent eacb6fb commit 371ecc5

File tree

18 files changed

+381
-244
lines changed

18 files changed

+381
-244
lines changed

ngcsimlib/compartment.py

Lines changed: 19 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -7,13 +7,12 @@
77
class Compartment:
88
"""
99
Compartments in ngcsimlib are container objects for storing the stateful
10-
values of components. Compartments are
11-
tracked globaly and are automatically linked to components and methods
12-
during compiling to allow for stateful
13-
mechanics to be run without the need for the class object. Compartments
14-
also provide an entry and exit point for
15-
values inside of components allowing for cables to be connected for
16-
sending and receiving values.
10+
values of components. Compartments are tracked globally and are
11+
automatically linked to components and methods during compiling to allow
12+
for stateful mechanics to be run without the need for the class object.
13+
Compartments also provide an entry and exit point for values inside of
14+
components allowing for cables to be connected for sending and receiving
15+
values.
1716
"""
1817

1918
@classmethod
@@ -31,19 +30,18 @@ def is_compartment(cls, obj):
3130
"""
3231
return hasattr(obj, "_is_compartment")
3332

34-
def __init__(self, initial_value=None, static=False, is_input=False, display_name=None, units=None):
33+
def __init__(self, initial_value=None, static=False, is_input=False,
34+
display_name=None, units=None):
3535
"""
3636
Builds a compartment to be used inside a component. It is important
37-
to note that building compartments
38-
outside of components may cause unexpected behavior as components
39-
interact with their compartments during
40-
construction to finish initializing them.
37+
to note that building compartments outside of components may cause
38+
unexpected behavior as components interact with their compartments
39+
during construction to finish initializing them.
4140
Args:
4241
initial_value: The initial value of the compartment. As a general
43-
practice it is a good idea to
44-
provide a value that is similar to the values that will
45-
normally be stored here, such as an array of
46-
zeros of the correct length. (default: None)
42+
practice it is a good idea to provide a value that is similar to
43+
the values that will normally be stored here, such as an array of
44+
zeros of the correct length. (default: None)
4745
4846
static: a flag to lock a compartment to be static (default: False)
4947
"""
@@ -105,10 +103,9 @@ def __str__(self):
105103
def __lshift__(self, other) -> None:
106104
"""
107105
Overrides the left shift operation to be used for wiring compartments
108-
into one another
109-
if other is not an Operation it will create an overwrite operation
110-
with other as the argument,
111-
otherwise it will use the provided operation
106+
into one another if other is not an Operation it will create an
107+
overwrite operation with other as the argument, otherwise it will use
108+
the provided operation
112109
113110
Args:
114111
other: Either another component or an instance of BaseOp
@@ -136,9 +133,9 @@ def is_wired(self):
136133

137134
@property
138135
def display_name(self):
139-
return self._display_name if self._display_name is not None else self.name
136+
return self._display_name if self._display_name is not None else (
137+
self.name)
140138

141139
@property
142140
def units(self):
143141
return self._units if self._units is not None else "dimensionless"
144-

ngcsimlib/compilers/command_compiler.py

Lines changed: 57 additions & 35 deletions
Original file line numberDiff line numberDiff line change
@@ -1,44 +1,58 @@
11
"""
22
This is the file that contains the code to compile a given command on a model.
33
4-
There are a few ways to compile the commands for a model, firstly if there is a command object already initialized
5-
that has a valid compile key and a list of components the base method of `compile_command(command)` can be used to produce
6-
the desired output. If no command object has been initialized then the `dynamic_compile(*components, compile_key=None)`
7-
can be used to produce the desired output without the need to first go through a command object. The output of either
8-
compile method will be the same.
4+
There are a few ways to compile the commands for a model, firstly if there is
5+
a command object already initialized that has a valid compile key and a list
6+
of components the base method of `compile_command(command)` can be used to
7+
produce the desired output. If no command object has been initialized then
8+
the `dynamic_compile(*components, compile_key=None)` can be used to produce
9+
the desired output without the need to first go through a command object. The
10+
output of either compile method will be the same.
911
1012
The output produced by compiling a command will be two objects.
1113
12-
The first object produced by compiling a command is the compiled method itself. This method requires at least one
13-
positional argument and then any number of additional arguments. The first argument that is provided to the compiled
14-
method is a python dictionary that contains the state for all compartments this method will need to access,
15-
as discerning this can be a challenge it is normal to just pass it all compartments present on your model. The
16-
remaining list of arguments are all the run time arguments that the various compiled methods need to run properly.
17-
The return value of this compiled method is the final state of all compartments after running through the compiled
18-
command. Note here that the value on the compartments are not automatically updated and that will need to be done after.
19-
20-
The second object produced by compiling a command is the list of arguments that the compile command is expecting to
21-
be passed in alongside the initial state of all the compartments. It is a good habit to get into printing this list
22-
out after compiling as it can help catch typos present in the compiled methods that will not cause the compiling to
23-
fail but will produce unknown behavior.
24-
25-
There is a wrapper method offered in this file we recommend using to assist with the design patterned required by the
26-
compiled command. This is done with `wrap_command(command)`. This method will return another method that removes the
27-
need for creating the initial state of the compartments and setting all the compartment values after running. Arguments
28-
are still required to be passed in at run time.
14+
The first object produced by compiling a command is the compiled method
15+
itself. This method requires at least one positional argument and then any
16+
number of additional arguments. The first argument that is provided to the
17+
compiled method is a python dictionary that contains the state for all
18+
compartments this method will need to access, as discerning this can be a
19+
challenge it is normal to just pass it all compartments present on your
20+
model. The remaining list of arguments are all the run time arguments that
21+
the various compiled methods need to run properly. The return value of this
22+
compiled method is the final state of all compartments after running through
23+
the compiled command. Note here that the value on the compartments are not
24+
automatically updated and that will need to be done after.
25+
26+
The second object produced by compiling a command is the list of arguments
27+
that the compile command is expecting to be passed in alongside the initial
28+
state of all the compartments. It is a good habit to get into printing this
29+
list out after compiling as it can help catch typos present in the compiled
30+
methods that will not cause the compiling to fail but will produce unknown
31+
behavior.
32+
33+
There is a wrapper method offered in this file we recommend using to assist
34+
with the design patterned required by the compiled command. This is done with
35+
`wrap_command(command)`. This method will return another method that removes
36+
the need for creating the initial state of the compartments and setting all
37+
the compartment values after running. Arguments are still required to be
38+
passed in at run time.
2939
3040
"""
31-
from ngcsimlib.compilers.component_compiler import parse as parse_component, compile as compile_component
41+
from ngcsimlib.compilers.component_compiler import parse as parse_component, \
42+
compile as compile_component
3243
from ngcsimlib.compilers.op_compiler import parse as parse_connection
3344
from ngcsimlib.utils import Get_Compartment_Batch, Set_Compartment_Batch
3445
from ngcsimlib.logger import critical
3546

47+
3648
def _compile(compile_key, components):
3749
"""
38-
This is the top level compile method for commands. Note this does not actually require you to compile a
39-
specific command object as it works purely off the compile key provided to the method.
40-
The general process that this takes to compile down everything, is by producing an execution order that knows which
41-
methods that are going to be called and where the results of the method are supposed to be stored.
50+
This is the top level compile method for commands. Note this does not
51+
actually require you to compile a specific command object as it works
52+
purely off the compile key provided to the method. The general process
53+
that this takes to compile down everything, is by producing an execution
54+
order that knows which methods that are going to be called and where the
55+
results of the method are supposed to be stored.
4256
4357
The execution order is as follows:
4458
@@ -47,8 +61,8 @@ def _compile(compile_key, components):
4761
| resolve the outputs of the compiled function
4862
4963
Args:
50-
compile_key: The key that is being compiled (mapped to each function that has the @resolver decorator
51-
above it)
64+
compile_key: The key that is being compiled (mapped to each function
65+
that has the @resolver decorator above it)
5266
5367
components: The list of components to compile for this function
5468
@@ -86,11 +100,17 @@ def _compile(compile_key, components):
86100
for c_name, component in components.items():
87101
exc_order.extend(compile_component(component, resolvers[c_name]))
88102

89-
def compiled(compartment_values, **kwargs):
103+
def compiled(compartment_values=None, **kwargs):
104+
if compartment_values is None:
105+
critical(
106+
f"Attempting to call a compiled method without the current "
107+
f"state of the model. "
108+
f"Verify the method is wrapped or a current state is provided")
90109
for n in needed_args:
91110
if n not in kwargs:
92-
critical(f"Missing keyword argument \"{n}\" in compiled function."
93-
f"\tExpected keyword arguments {needed_args}")
111+
critical(
112+
f"Missing keyword argument \"{n}\" in compiled function."
113+
f"\tExpected keyword arguments {needed_args}")
94114

95115
for exc, outs, name, comp_ids in exc_order:
96116
_comps = {key: compartment_values[key] for key in comp_ids}
@@ -107,7 +127,8 @@ def compiled(compartment_values, **kwargs):
107127

108128
def compile_command(command):
109129
"""
110-
Compiles a given command object to the spec described at the top of this file
130+
Compiles a given command object to the spec described at the top of this
131+
file
111132
112133
Args:
113134
command: the command object
@@ -121,8 +142,8 @@ def compile_command(command):
121142

122143
def dynamic_compile(*components, compile_key=None):
123144
"""
124-
Dynamically compiles a command without the need of a command object to produce
125-
the spec described at the top of this file.
145+
Dynamically compiles a command without the need of a command object to
146+
produce the spec described at the top of this file.
126147
127148
Args:
128149
*components: a list of components to be compiled
@@ -149,6 +170,7 @@ def wrap_command(command):
149170
Returns:
150171
the output of the command after it's been executed
151172
"""
173+
152174
def _wrapped(**kwargs):
153175
vals = command(Get_Compartment_Batch(), **kwargs)
154176
Set_Compartment_Batch(vals)

ngcsimlib/component.py

Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -3,21 +3,20 @@
33
from ngcsimlib.compartment import Compartment
44
from ngcsimlib.logger import warn
55

6+
67
class Component(metaclass=MetaComponent):
78
"""
89
Components are a foundational part of ngclearn and its component/command
910
structure. In ngclearn, all stateful parts of a model take the form of
10-
components. The internal storage of the state within a component takes one
11-
of two forms, either as a compartment or as a member variable. The member
12-
variables are values such as hyperparameters and weights/synaptic
13-
efficacies,
14-
where the transfer of their individual state from component to component is
15-
not needed.
16-
Compartments, on the other hand, are where the state information, both from
17-
and for other components, are stored. As the components are the stateful
18-
pieces of the model, they also contain the methods and logic behind
19-
advancing
20-
their internal state (values) forward in time.
11+
components. The internal storage of the state within a component takes
12+
one of two forms, either as a compartment or as a member variable. The
13+
member variables are values such as hyperparameters and weights/synaptic
14+
efficacies, where the transfer of their individual state from component
15+
to component is not needed. Compartments, on the other hand, are where
16+
the state information, both from and for other components, are stored. As
17+
the components are the stateful pieces of the model, they also contain
18+
the methods and logic behind advancing their internal state (values)
19+
forward in time.
2120
"""
2221

2322
def __init__(self, name, **kwargs):
@@ -31,8 +30,7 @@ def __init__(self, name, **kwargs):
3130
name: the name of the component
3231
3332
kwargs: additional keyword arguments. These are not used in the
34-
base class,
35-
but this is here for future use if needed.
33+
base class, but this is here for future use if needed.
3634
"""
3735
# Component Data
3836
self.name = name
@@ -112,4 +110,3 @@ def save(self, directory, **kwargs):
112110
@abstractmethod
113111
def help(cls):
114112
pass
115-

ngcsimlib/configManager.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,8 @@ def get_config(configName):
5555
configName: configuration section to get
5656
5757
Returns:
58-
dictionary representing the configuration section, None if section is not present
58+
dictionary representing the configuration section, None if section
59+
is not present
5960
"""
6061
return _GlobalConfig.get_config(configName)
6162

@@ -68,6 +69,7 @@ def provide_namespace(configName):
6869
configName: configuration section to get
6970
7071
Returns:
71-
simple namespace representing the configuration section, none if section is not present
72+
simple namespace representing the configuration section, none if
73+
section is not present
7274
"""
7375
return _GlobalConfig.provide_namespace(configName)

ngcsimlib/context.py

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -52,10 +52,9 @@ def __new__(cls, name, *args, **kwargs):
5252
def __init__(self, name, should_validate=None):
5353
"""
5454
Builds the initial context object, if `__new__` provides an already
55-
initialized context do not continue with
56-
construction as it is already initialized. This is where the path to
57-
a context is assigned so their paths are
58-
dependent on the current context path upon creation.
55+
initialized context do not continue with construction as it is
56+
already initialized. This is where the path to a context is assigned
57+
so their paths are dependent on the current context path upon creation.
5958
6059
Args:
6160
name: The name of the new context can not be empty
@@ -112,8 +111,8 @@ def get_components(self, *component_names, unwrap=True):
112111
a single component is retrieved
113112
114113
Returns:
115-
either a list of components or a single component depending on
116-
the number of components being retrieved
114+
either a list of components or a single component depending on
115+
the number of components being retrieved
117116
"""
118117
if len(component_names) == 0:
119118
return None
@@ -163,8 +162,7 @@ def register_command(self, klass, *args, components=None, command_name=None,
163162
def register_component(self, component, *args, **kwargs):
164163
"""
165164
Adds a component to the local json storage for saving, will provide a
166-
warning for all values it fails to
167-
serialize into a json file
165+
warning for all values it fails to serialize into a json file
168166
169167
Args:
170168
component: the component object to save
@@ -235,10 +233,9 @@ def add_command(self, command, name=None):
235233
def save_to_json(self, directory, model_name=None, custom_save=True,
236234
overwrite=False):
237235
"""
238-
Dumps all the required json files to rebuild the current controller
239-
to a specified directory. If there is a
240-
`save` command present on the controller and custom_save is True,
241-
it will run that command as well.
236+
Dumps all the required json files to rebuild the current controller to
237+
a specified directory. If there is a `save` command present on the
238+
controller and custom_save is True, it will run that command as well.
242239
243240
Args:
244241
directory: The top level directory to save the model to
@@ -251,8 +248,7 @@ def save_to_json(self, directory, model_name=None, custom_save=True,
251248
command if present on the controller (Default: True)
252249
253250
overwrite: A boolean for if the saved model should be in a unique
254-
folder or if it should overwrite
255-
existing folders
251+
folder or if it should overwrite existing folders
256252
257253
Returns:
258254
a tuple where the first value is the path to the model, and the
@@ -372,9 +368,8 @@ def make_components(self, path_to_components_file, custom_file_dir=None):
372368
and extension
373369
374370
custom_file_dir: the path to the custom directory for custom load
375-
methods,
376-
this directory is named `custom` if the save_to_json method is
377-
used. (Default: None)
371+
methods, this directory is named `custom` if the save_to_json
372+
method is used. (Default: None)
378373
"""
379374
made_components = []
380375
with open(path_to_components_file, 'r') as file:
@@ -466,9 +461,8 @@ def _make_op(self, op_spec):
466461
def dynamicCommand(fn):
467462
"""
468463
Provides a decorator that will automatically bind the decorated
469-
method to the current context.
470-
Note this if this is called from a context object it will still use
471-
the current context not the object
464+
method to the current context. Note this if this is called from a
465+
context object it will still use the current context not the object
472466
473467
Args:
474468
fn: The wrapped method
@@ -482,8 +476,8 @@ def dynamicCommand(fn):
482476

483477
def compile_by_key(self, *components, compile_key, name=None):
484478
"""
485-
Compiles a given set of components with a given compile key.
486-
It will automatically add it to the context after compiling
479+
Compiles a given set of components with a given compile key. It will
480+
automatically add it to the context after compiling
487481
488482
Args:
489483
*components: positional arguments for all components
@@ -600,6 +594,7 @@ def view_guide(self, guide, skip=None):
600594
"""
601595
Views the specified guide for each component class in the model,
602596
skipping over any classes in skip.
597+
603598
Args:
604599
guide: A ngclearn.GuideList value
605600
skip: a list of classes to skip, will also skip component classes

0 commit comments

Comments
 (0)