add numpy type to language element types

This commit is contained in:
sneakers-the-rat 2024-07-19 20:50:48 -07:00
parent 2e7670a2bd
commit 43187bfa3e
Signed by untrusted user who does not match committer: jonny
GPG key ID: 6DCB96EF1E4D232D
3 changed files with 51 additions and 14 deletions

View file

@ -2,7 +2,7 @@
Language elements in nwb schema language that have a fixed, alternative representation
in LinkML. These are exported as an nwb.language.yml file along with every generated namespace
"""
from typing import List
from linkml_runtime.linkml_model import (
ClassDefinition,
EnumDefinition,
@ -12,7 +12,7 @@ from linkml_runtime.linkml_model import (
TypeDefinition,
)
from nwb_linkml.maps import flat_to_linkml
from nwb_linkml.maps import flat_to_linkml, flat_to_np
from nwb_schema_language.datamodel.nwb_schema_pydantic import FlatDtype as FlatDtype_source
FlatDType = EnumDefinition(
@ -20,19 +20,28 @@ FlatDType = EnumDefinition(
permissible_values=[PermissibleValue(p) for p in FlatDtype_source.__members__],
)
DTypeTypes = []
for nwbtype, linkmltype in flat_to_linkml.items():
# skip the dtypes that are the same as the builtin linkml types (which should already exist)
# to avoid a recursion error
if linkmltype == nwbtype:
continue
amin = None
if nwbtype.startswith("uint"):
amin = 0
def _make_dtypes() -> List[TypeDefinition]:
DTypeTypes = []
for nwbtype, linkmltype in flat_to_linkml.items():
# skip the dtypes that are the same as the builtin linkml types (which should already exist)
# to avoid a recursion error
if linkmltype == nwbtype:
continue
atype = TypeDefinition(name=nwbtype, minimum_value=amin, typeof=linkmltype)
DTypeTypes.append(atype)
amin = None
if nwbtype.startswith("uint"):
amin = 0
np_type = flat_to_np[nwbtype]
repr_string = f'np.{np_type.__name__}' if np_type.__module__ == 'numpy' else None
atype = TypeDefinition(name=nwbtype, minimum_value=amin, typeof=linkmltype, repr=repr_string)
DTypeTypes.append(atype)
return DTypeTypes
DTypeTypes = _make_dtypes()
AnyType = ClassDefinition(
name="AnyType",

View file

@ -2,7 +2,7 @@
Mapping from one domain to another
"""
from nwb_linkml.maps.dtype import flat_to_linkml, flat_to_nptyping
from nwb_linkml.maps.dtype import flat_to_linkml, flat_to_nptyping, flat_to_np
from nwb_linkml.maps.map import Map
from nwb_linkml.maps.postload import MAP_HDMF_DATATYPE_DEF, MAP_HDMF_DATATYPE_INC
from nwb_linkml.maps.quantity import QUANTITY_MAP
@ -13,5 +13,6 @@ __all__ = [
"QUANTITY_MAP",
"Map",
"flat_to_linkml",
"flat_to_np",
"flat_to_nptyping",
]

View file

@ -69,6 +69,33 @@ flat_to_nptyping = {
"object": "Object",
}
flat_to_np = {
"float": float,
"float32": np.float32,
"double": np.double,
"float64": np.float64,
"long": np.longlong,
"int64": np.int64,
"int": int,
"int32": np.int32,
"int16": np.int16,
"short": np.short,
"int8": np.int8,
"uint": np.uint,
"uint32": np.uint32,
"uint16": np.uint16,
"uint8": np.uint8,
"uint64": np.uint64,
"numeric": np.number,
"text": str,
"utf": str,
"utf8": str,
"utf_8": str,
"ascii": str,
"bool": bool,
"isodatetime": np.datetime64,
}
np_to_python = {
Any: Any,
np.number: float,