Skip to content

Commit

Permalink
add method to get instrument loader information and include that in t…
Browse files Browse the repository at this point in the history
…he notebook
  • Loading branch information
jepegit committed Mar 3, 2024
1 parent 99f8069 commit abade4e
Show file tree
Hide file tree
Showing 6 changed files with 781 additions and 379 deletions.
118 changes: 116 additions & 2 deletions cellpy/readers/core.py
Expand Up @@ -7,6 +7,7 @@
import abc
import datetime
import importlib
import inspect
import logging
import os
import pathlib
Expand Down Expand Up @@ -37,6 +38,9 @@
HEADERS_SUMMARY = get_headers_summary() # TODO @jepe refactor this (not needed)
HEADERS_STEP_TABLE = get_headers_step_table() # TODO @jepe refactor this (not needed)

LOADERS_NOT_READY_FOR_PROD = [
"ext_nda_reader"
] # used by the instruments_configurations helper function (move?)

# pint (https://pint.readthedocs.io/en/stable/)
ureg = pint.UnitRegistry()
Expand Down Expand Up @@ -629,7 +633,17 @@ class InstrumentFactory:

def __init__(self):
self._builders = {}
self._kwargs = {}
self._kwargs = {} # stored kwargs for the builders (not used yet)

def __str__(self):
txt = "<InstrumentFactory>\n"
for key in self._builders:
txt += f" {key}\n"
return txt

@property
def builders(self):
return self._builders

def register_builder(self, key: str, builder: Tuple[str, Any], **kwargs) -> None:
"""register an instrument loader module.
Expand All @@ -645,6 +659,65 @@ def register_builder(self, key: str, builder: Tuple[str, Any], **kwargs) -> None
self._builders[key] = builder
self._kwargs[key] = kwargs

def unregister_builder(self, key: str) -> None:
"""unregister an instrument loader module.
Args:
key: instrument id
"""
logging.debug(f"Unregistering instrument {key}")
self._builders.pop(key, None)
self._kwargs.pop(key, None)

def get_registered_builders(self):
return list(self._builders.keys())

def get_registered_kwargs(self):
return self._kwargs

def get_registered_builder(self, key):
return self._builders.get(key, None)

def create_all(self, **kwargs):
"""Create all the instrument loader modules.
Args:
**kwargs: sent to the initializer of the loader class.
Returns:
dict of instances of loader classes.
"""
loaders = {}
for key in self._builders:
bargs = self._kwargs.get(key, {})
bargs.update(kwargs)
try:
models = {}
loader = self.create(key, **bargs)
models["default"] = loader

if available_models := self._get_models(loader):
for model in available_models:
bargs["model"] = model
models[model] = self.create(key, **bargs)

loaders[key] = models
except Exception as e:
logging.critical(f"Could not create loader for {key}: {e}")
return loaders

@staticmethod
def _get_models(loader):

try:
models = loader.get_params("supported_models")
return models

except Exception as e:
logging.debug(f"COULD NOT RETRIEVE supported_models for {loader}: {e}")

return

def create(self, key: Union[str, None], **kwargs):
"""Create the instrument loader module and initialize the loader class.
Expand All @@ -655,7 +728,6 @@ def create(self, key: Union[str, None], **kwargs):
Returns:
instance of loader class.
"""

module_name, module_path = self._builders.get(key, (None, None))

# constant:
Expand Down Expand Up @@ -693,9 +765,51 @@ def query(self, key: str, variable: str) -> Any:

except (AttributeError, NotImplementedError, KeyError):
logging.debug(f"COULD NOT RETRIEVE {variable} for {key}")

except Exception as e:
logging.debug(f"COULD NOT RETRIEVE {variable} for {key}: {e}")

return


def instrument_configurations(search_text: str = "") -> Dict[str, Any]:
"""This function returns a dictionary with information about the available
instrument loaders and their models.
Args:
search_text: string to search for in the instrument names.
Returns:
dict: nested dictionary with information about the available instrument loaders and their models.
"""
instruments = {}
_instruments = find_all_instruments(search_text)
factory = InstrumentFactory()

for instrument, instrument_settings in _instruments.items():
if instrument not in LOADERS_NOT_READY_FOR_PROD:
factory.register_builder(instrument, instrument_settings)

loaders = factory.create_all()

for loader, loader_instance in loaders.items():
_info = {"__all__": []}
for model, model_instance in loader_instance.items():
_info["__all__"].append(model)
_model_info = {}
if hasattr(model_instance, "config_params"):
_model_info["config_params"] = model_instance.config_params
else:
_model_info["config_params"] = None

_model_info["doc"] = inspect.getdoc(model_instance)

_info[model] = _model_info
instruments[loader] = _info
return instruments


def generate_default_factory():
"""This function searches for all available instrument readers
and registers them in an InstrumentFactory instance.
Expand Down
3 changes: 0 additions & 3 deletions cellpy/readers/instruments/local_instrument.py
Expand Up @@ -28,9 +28,6 @@ def __init__(self, instrument_file=None, **kwargs):

def pre_init(self):
self.auto_register_config = False
print("---------------------------")
print(f"{self.local_instrument_file}")
print("---------------------------")
self.config_params = register_local_configuration_from_yaml_file(
self.local_instrument_file
)
1 change: 1 addition & 0 deletions cellpy/readers/instruments/neware_txt.py
Expand Up @@ -52,6 +52,7 @@ class InstrumentsClass(CellPyConfig):


SUPPORTED_MODELS = {
"ONE": "neware_txt_zero",
"UIO": "neware_txt_zero",
}

Expand Down
2 changes: 1 addition & 1 deletion cellpy/utils/example_data.py
Expand Up @@ -236,7 +236,7 @@ def arbin_multi_file_path() -> Path:

def maccor_file_path() -> Path:
"""Get the path to an example maccor txt file"""
return _download_if_missing(ExampleData.MACCOR_TXT_TYPE_TWO.value)
return _download_if_missing(ExampleData.MACCOR_TXT_TYPE_THREE.value)


def maccor_file_path_type_one() -> Path:
Expand Down

0 comments on commit abade4e

Please sign in to comment.