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

Adds passing of objects to transmission reader #157

Merged
merged 3 commits into from
Sep 12, 2023
Merged
Show file tree
Hide file tree
Changes from 2 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
19 changes: 4 additions & 15 deletions pynxtools/dataconverter/readers/rii_database/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@
from pynxtools.dataconverter.readers.rii_database.dispersion_reader import (
DispersionReader,
)
from pynxtools.dataconverter.readers.utils import parse_json
from pynxtools.dataconverter.readers.utils import parse_json, handle_objects


class RiiReader(YamlJsonReader):
Expand All @@ -40,7 +40,7 @@ def __init__(self, *args, **kwargs):
".yaml": self.read_dispersion,
".json": self.parse_json_w_fileinfo,
"default": lambda _: self.appdef_defaults(),
"objects": self.handle_objects,
"objects": self.handle_rii_objects,
}

def read_dispersion(self, filename: str):
Expand Down Expand Up @@ -86,20 +86,9 @@ def parse_json_w_fileinfo(self, filename: str) -> Dict[str, Any]:

return template

def handle_objects(self, objects: Tuple[Any]) -> Dict[str, Any]:
def handle_rii_objects(self, objects: Tuple[Any]) -> Dict[str, Any]:
"""Handle objects and generate template entries from them"""
if objects is None:
return {}

template = {}

for obj in objects:
if not isinstance(obj, dict):
logging.warning("Ignoring unknown object of type %s", type(obj))
continue

template.update(obj)

template = handle_objects(objects)
self.fill_dispersion_in(template)

return template
Expand Down
3 changes: 2 additions & 1 deletion pynxtools/dataconverter/readers/transmission/reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@

from pynxtools.dataconverter.readers.json_yml.reader import YamlJsonReader
import pynxtools.dataconverter.readers.transmission.metadata_parsers as mpars
from pynxtools.dataconverter.readers.utils import parse_json, parse_yml
from pynxtools.dataconverter.readers.utils import parse_json, parse_yml, handle_objects


# Dictionary mapping metadata in the asc file to the paths in the NeXus file.
Expand Down Expand Up @@ -254,6 +254,7 @@ class TransmissionReader(YamlJsonReader):
".yml": lambda fname: parse_yml(fname, CONVERT_DICT, REPLACE_NESTED),
".yaml": lambda fname: parse_yml(fname, CONVERT_DICT, REPLACE_NESTED),
"default": lambda _: add_def_info(),
"objects": handle_objects,
}


Expand Down
21 changes: 21 additions & 0 deletions pynxtools/dataconverter/readers/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,16 @@
# limitations under the License.
#
"""Utility functions for the NeXus reader classes."""
import logging
from typing import Tuple
from dataclasses import dataclass, replace
from typing import List, Any, Dict, Optional
from collections.abc import Mapping
import json
import yaml

logger = logging.getLogger(__name__)


@dataclass
class FlattenSettings():
Expand Down Expand Up @@ -201,3 +205,20 @@ def parse_json(file_path: str) -> Dict[str, Any]:
"""
with open(file_path, "r", encoding="utf-8") as file:
return json.load(file)


def handle_objects(objects: Tuple[Any]) -> Dict[str, Any]:
"""Handle objects and generate template entries from them"""
if objects is None:
return {}

template = {}

for obj in objects:
if not isinstance(obj, dict):
logger.warning("Ignoring unknown object of type %s", type(obj))
continue

template.update(obj)

return template
Loading