Skip to content

Commit

Permalink
working on units import, fixed traceback
Browse files Browse the repository at this point in the history
  • Loading branch information
eagmon committed Dec 7, 2024
1 parent 2f50e81 commit 1d9b0d9
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 28 deletions.
49 changes: 48 additions & 1 deletion bigraph_schema/type_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@
from typing import NewType, Union, Mapping, List, Dict, Optional, Callable
from dataclasses import field, make_dataclass

from bigraph_schema.units import units
from bigraph_schema.units import units, render_units_type
from bigraph_schema.registry import (
NONE_SYMBOL, type_schema_keys,
is_schema_key, non_schema_keys, type_parameter_key,
Expand Down Expand Up @@ -2520,11 +2520,57 @@ def read_shape(shape):
shape)])


# handling unit registration
# TODO -- can this be done onto the dicts, rather than using the core?
def register_units(core, units):
for unit_name in units._units:
try:
unit = getattr(units, unit_name)
except:
# print(f'no unit named {unit_name}')
continue

dimensionality = unit.dimensionality
type_key = render_units_type(dimensionality)
if not core.exists(type_key):
core.register(type_key, {
'_default': '',
'_apply': apply_units,
'_check': check_units,
'_serialize': serialize_units,
'_deserialize': deserialize_units,
'_description': 'type to represent values with scientific units'})

return core


# function to add the unit types to the type library
def add_units_to_library(units, type_library):
for unit_name in units._units:
try:
unit = getattr(units, unit_name)
except AttributeError:
continue

dimensionality = unit.dimensionality
type_key = render_units_type(dimensionality)
if type_key not in type_library:
type_library[type_key] = {
'_type': 'unit',
'name': unit_name,
'dimensionality': str(dimensionality)
}

return type_library

# ===============================
# Types with their type functions
# ===============================
# These dictionaries define the types and their corresponding type functions.

unit_types = {}
unit_types = add_units_to_library(units, unit_types)

registry_types = {
'any': {
'_type': 'any',
Expand Down Expand Up @@ -2735,3 +2781,4 @@ def read_shape(shape):
'_description': 'hyperedges in the bigraph, with inputs and outputs as type parameters',
'inputs': 'wires',
'outputs': 'wires'}}

33 changes: 6 additions & 27 deletions bigraph_schema/type_system.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,15 +8,15 @@
import functools
import inspect
import random
import traceback
from pprint import pformat as pf

from bigraph_schema import Registry, non_schema_keys, is_schema_key, deep_merge, type_parameter_key
from bigraph_schema.parse import parse_expression
from bigraph_schema.registry import type_schema_keys, remove_omitted, set_path, transform_path
from bigraph_schema.type_functions import set_apply, registry_types, base_type_library, register_base_reactions, \
union_keys, is_empty, \
apply_schema, apply_units, check_units, serialize_units, deserialize_units
from bigraph_schema.units import units, render_units_type
from bigraph_schema.type_functions import set_apply, registry_types, base_type_library, unit_types, register_base_reactions, \
union_keys, is_empty, apply_schema, register_units
from bigraph_schema.units import units


TYPE_FUNCTION_KEYS = [
Expand Down Expand Up @@ -107,7 +107,9 @@ def __init__(self):

self.register_types(registry_types)
self.register_types(base_type_library)
# self.register_types(unit_types)

# # TODO -- add a proper registration into registry
register_units(self, units)
register_base_reactions(self)

Expand Down Expand Up @@ -211,7 +213,6 @@ def register(self, key, schema, alternate_keys=tuple(), force=False):
else:
lookup = self.find(subschema)
if lookup is None:
import ipdb; ipdb.set_trace()
raise Exception(
f'trying to register a new type ({key}), '
f'but it depends on a type ({subkey}) which is not in the registry')
Expand Down Expand Up @@ -2060,25 +2061,3 @@ def compose(self, a, b):
def query(self, schema, instance, redex):
subschema = {}
return subschema


def register_units(core, units):
for unit_name in units._units:
try:
unit = getattr(units, unit_name)
except:
# print(f'no unit named {unit_name}')
continue

dimensionality = unit.dimensionality
type_key = render_units_type(dimensionality)
if not core.exists(type_key):
core.register(type_key, {
'_default': '',
'_apply': apply_units,
'_check': check_units,
'_serialize': serialize_units,
'_deserialize': deserialize_units,
'_description': 'type to represent values with scientific units'})

return core

0 comments on commit 1d9b0d9

Please sign in to comment.