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

General tidying of code and actions #71

Merged
merged 4 commits into from
Mar 21, 2024
Merged
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
5 changes: 4 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,9 @@ jobs:
steps:
- uses: actions/checkout@v4
- uses: actions/setup-python@v5
- uses: pre-commit/[email protected]
with:
python-version: "3.10"
- uses: pre-commit/[email protected]
with:
extra_args: --hook-stage manual --all-files

Expand Down Expand Up @@ -52,6 +54,7 @@ jobs:
run: |
cd examples
python document.py
git diff

cd test
python test.py
Expand Down
2 changes: 1 addition & 1 deletion docs/sphinx/source/api/Contributors.md
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
# Modelspec contributors

This page list names and Github profiles of contributors to Modelspec, listed in no particular order.
This page is generated periodically, most recently on 2024-02-07.
This page is generated periodically, most recently on 2024-03-21.

- Padraig Gleeson ([@pgleeson](https://github.com/pgleeson))
- Manifest Chakalov ([@mqnifestkelvin](https://github.com/mqnifestkelvin))
Expand Down
6 changes: 3 additions & 3 deletions docs/sphinx/source/api/examples/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from modelspec.base_types import Base
from typing import List
import sys
import json
import yaml
import bson

# Example showing how to create a model of a document and use it to create/serialize instances

Expand Down Expand Up @@ -96,9 +99,6 @@ class Document(Base):
print("\n >> Generating specification in dict form...")
doc_dict = doc.generate_documentation(format="dict")

import json
import yaml
import bson

with open("document.specification.json", "w") as d:
d.write(json.dumps(doc_dict, indent=4))
Expand Down
6 changes: 3 additions & 3 deletions examples/document.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@
from modelspec.base_types import Base
from typing import List
import sys
import json
import yaml
import bson

# Example showing how to create a model of a document and use it to create/serialize instances

Expand Down Expand Up @@ -96,9 +99,6 @@ class Document(Base):
print("\n >> Generating specification in dict form...")
doc_dict = doc.generate_documentation(format="dict")

import json
import yaml
import bson

with open("document.specification.json", "w") as d:
d.write(json.dumps(doc_dict, indent=4))
Expand Down
4 changes: 1 addition & 3 deletions examples/test/test.py
Original file line number Diff line number Diff line change
@@ -1,10 +1,9 @@
import modelspec
from modelspec import field, instance_of, optional
from modelspec.base_types import Base
from modelspec.utils import load_json
from typing import List
from typing import Any
import sys
import yaml


# Example testing multiple options...
Expand Down Expand Up @@ -104,7 +103,6 @@ class TopClass(Base):
print("\nGenerating specification in dict form")
doc_dict = tc.generate_documentation(format="dict")

import yaml

print("Generating specification in YAML")
with open("test.specification.yaml", "w") as d:
Expand Down
8 changes: 1 addition & 7 deletions src/modelspec/base_types.py
Original file line number Diff line number Diff line change
Expand Up @@ -169,18 +169,13 @@ def from_yaml(cls, yaml_str: str) -> "Base":
@classmethod
def from_yaml_file(cls, yaml_file: str) -> "Base":
"""Instantiate an modelspec object from a file containing YAML"""
return cls.from_dict(yaml.load(yaml_str, Loader=yaml.SafeLoader))
return cls.from_dict(yaml.load(yaml_file, Loader=yaml.SafeLoader))

@classmethod
def from_json(cls, json_str: str) -> "Base":
"""Instantiate an modelspec object from a JSON string"""
return cls.from_dict(json.loads(json_str))

@classmethod
def from_json_file(cls, json_file: str) -> "Base":
"""Instantiate an modelspec object from a file containing JSON"""
return cls.from_dict(json.load(json_file))

@classmethod
def from_bson(cls, bson_str: str) -> "Base":
"""Instantiate an modelspec object from a BSON string"""
Expand Down Expand Up @@ -318,7 +313,6 @@ def to_xml_file(
filename: Optional[str] = None,
include_metadata: bool = True,
) -> str:
from modelspec.utils import build_xml_element

if filename is None:
filename = f"{self.id}.xml"
Expand Down
34 changes: 17 additions & 17 deletions src/modelspec/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@
from modelspec.base_types import EvaluableExpression

from random import Random
from typing import Union, Dict
from typing import Union

verbose = False

Expand Down Expand Up @@ -272,11 +272,11 @@ def build_xml_element(data, parent=None):
)
if attribute_value is not None:
if (
type(attribute_value) == int
or type(attribute_value) == float
or type(attribute_value) == str
or type(attribute_value) == bool
or type(attribute_value) == list
type(attribute_value) is int
or type(attribute_value) is float
or type(attribute_value) is str
or type(attribute_value) is bool
or type(attribute_value) is list
):
parent.set(attribute_name, str(attribute_value))
elif type(attribute_value) == dict:
Expand Down Expand Up @@ -411,15 +411,15 @@ def _val_info(param_val):
pp = "%s" % param_val
pp = pp.replace("\n", "")
# pp+=' (TF %s %s)'%(param_val.shape,param_val.dtype)
elif type(param_val) == tuple:
elif type(param_val) is tuple:
# If param_val is a tuple, recursively print its elements
# separated by commas and wrapped in parentheses
pp = "(" + ", ".join([_val_info(el) for el in param_val]) + ")"
else:
pp = "%s" % param_val
t = type(param_val)
if not (t == int or t == float):
pp += "(%s)" % (t if type(t) == str else t.__name__)
if not (t is int or t is float):
pp += "(%s)" % (t if type(t) is str else t.__name__)
return pp


Expand Down Expand Up @@ -476,13 +476,13 @@ def evaluate(
verbose,
)
try:
if type(expr) == str and expr in parameters:
if type(expr) is str and expr in parameters:
expr = parameters[
expr
] # replace with the value in parameters & check whether it's float/int...
print_(" Using for that param: %s" % _val_info(expr), verbose)

if type(expr) == str:
if type(expr) is str:
try:
if array_format == FORMAT_TENSORFLOW:
expr = tf.constant(int(expr))
Expand All @@ -498,14 +498,14 @@ def evaluate(
except:
pass

if type(expr) == list:
if type(expr) is list:
print_(" Returning a list in format: %s" % array_format, verbose)
if array_format == FORMAT_TENSORFLOW:
return tf.constant(expr, dtype=tf.float64)
else:
return np.array(expr)

if type(expr) == np.ndarray:
if type(expr) is np.ndarray:
print_(" Returning a numpy array in format: %s" % array_format, verbose)
if array_format == FORMAT_TENSORFLOW:
return tf.convert_to_tensor(expr, dtype=tf.float64)
Expand Down Expand Up @@ -538,9 +538,9 @@ def evaluate(
"The expression [%s] contains a random() call, but a random number generator (rng) must be supplied to the evaluate() call when this expression string is to be evaluated"
)

if type(expr) == str and "math." in expr:
if type(expr) is str and "math." in expr:
parameters["math"] = math
if type(expr) == str and "numpy." in expr:
if type(expr) is str and "numpy." in expr:
parameters["numpy"] = np

print_(
Expand All @@ -556,7 +556,7 @@ def evaluate(
verbose,
)

if (type(v) == float or type(v) == str) and int(v) == v:
if (type(v) is float or type(v) is str) and int(v) == v:

print_(" Returning int: %s" % int(v), verbose)

Expand All @@ -583,7 +583,7 @@ def parse_list_like(list_str):
return [list_str]
elif isinstance(list_str, list):
return list_str
elif type(list_str) == str:
elif type(list_str) is str:
try:
expr = int(list_str)
return [expr]
Expand Down
4 changes: 2 additions & 2 deletions test_all.sh
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
#!/bin/bash
set -ex

## Install
## Install modelspec incl. dev dependencies

pip install .
pip install .[dev]


## Test main example
Expand Down
5 changes: 3 additions & 2 deletions tests/test_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -192,8 +192,9 @@ def test_save_load_json(tmp_path):
nety3 = NewNetwork.from_yaml_file(filenamey)
str_nety3 = str(nety3)

# datax = load_xml(filenamex)
# print_v("Loaded network specification from %s" % filenamex)
datax = load_xml(filenamex)
assert len(datax) > 0
print_v("Loaded network specification from %s" % filenamex)

# netx = NewNetwork.from_dict(datax)
# str_netx = str(netx)
Expand Down