Skip to content

Commit

Permalink
Merge branch 'refactor_24' into 88-make-readers-return-sasdata-objects
Browse files Browse the repository at this point in the history
  • Loading branch information
jamescrake-merani committed Oct 23, 2024
2 parents b618a15 + d0564ab commit 7c906e8
Show file tree
Hide file tree
Showing 4 changed files with 106 additions and 36 deletions.
50 changes: 37 additions & 13 deletions sasdata/metadata.py
Original file line number Diff line number Diff line change
Expand Up @@ -338,10 +338,10 @@ def summary(self) -> str:

class Instrument:
def __init__(self, target: AccessorTarget):
self.aperture = Aperture(target.with_path_prefix("sasaperture"))
self.collimation = Collimation(target.with_path_prefix("sascollimation"))
self.detector = Detector(target.with_path_prefix("sasdetector"))
self.source = Source(target.with_path_prefix("sassource"))
self.aperture = Aperture(target.with_path_prefix("sasaperture|aperture"))
self.collimation = Collimation(target.with_path_prefix("sascollimation|collimation"))
self.detector = Detector(target.with_path_prefix("sasdetector|detector"))
self.source = Source(target.with_path_prefix("sassource|source"))

def summary(self):
return (
Expand All @@ -350,27 +350,51 @@ def summary(self):
self.detector.summary() +
self.source.summary())

def decode_string(data):
""" This is some crazy stuff"""

if isinstance(data, str):
return data

elif isinstance(data, np.ndarray):

if data.dtype == object:

data = data.reshape(-1)
data = data[0]

if isinstance(data, bytes):
return data.decode("utf-8")

return str(data)

else:
return data.tobytes().decode("utf-8")

else:
return str(data)

class Metadata:
def __init__(self, target: AccessorTarget):
self._target = target

self.instrument = Instrument(target.with_path_prefix("sasinstrument"))
self.process = Process(target.with_path_prefix("sasprocess"))
self.sample = Sample(target.with_path_prefix("sassample"))
self.transmission_spectrum = TransmissionSpectrum(target.with_path_prefix("sastransmission_spectrum"))
self.instrument = Instrument(target.with_path_prefix("sasinstrument|instrument"))
self.process = Process(target.with_path_prefix("sasprocess|process"))
self.sample = Sample(target.with_path_prefix("sassample|sample"))
self.transmission_spectrum = TransmissionSpectrum(target.with_path_prefix("sastransmission_spectrum|transmission_spectrum"))

self._title = StringAccessor(target, "title")
self._run = StringAccessor(target, "run")
self._definitiion = StringAccessor(target, "definition")
self._definition = StringAccessor(target, "definition")

self.title: str = self._title.value
self.run: str = self._run.value
self.definitiion: str = self._definitiion.value
self.title: str = decode_string(self._title.value)
self.run: str = decode_string(self._run.value)
self.definition: str = decode_string(self._definition.value)

def summary(self):
return (
f" {self.title}, Run: {self.run}\n" + " " + "="*len(self.title) +
f" {self.title}, Run: {self.run}\n" +
" " + "="*len(self.title) +
"=======" +
"="*len(self.run) + "\n\n" +
f"Definition: {self.title}\n" +
Expand Down
35 changes: 27 additions & 8 deletions sasdata/quantities/_accessor_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,21 +47,40 @@ def get_value(self, path: str):
current_tree_position: Group | Dataset = self._data

for token in tokens:

options = token.split("|")

if isinstance(current_tree_position, Group):
if token in current_tree_position.children:
current_tree_position = current_tree_position.children[token]
else:

found = False
for option in options:
if option in current_tree_position.children:
current_tree_position = current_tree_position.children[option]
found = True

if self.verbose:
logger.info(f"Found option: {option}")

if not found:
if self.verbose:
logger.info(f"Failed at token {token} on group {current_tree_position.name}. Options: " +
logger.info(f"Failed to find any of {options} on group {current_tree_position.name}. Options: " +
",".join([key for key in current_tree_position.children]))
return None

elif isinstance(current_tree_position, Dataset):
if token in current_tree_position.attributes:
current_tree_position = current_tree_position.attributes[token]
else:

found = False
for option in options:
if option in current_tree_position.attributes:
current_tree_position = current_tree_position.attributes[option]
found = True

if self.verbose:
logger.info(f"Found option: {option}")

if not found:
if self.verbose:
logger.info(f"Failed at token {token} on attribute {current_tree_position.name}. Options: " +
logger.info(f"Failed to find any of {options} on attribute {current_tree_position.name}. Options: " +
",".join([key for key in current_tree_position.attributes]))
return None

Expand Down
35 changes: 27 additions & 8 deletions sasdata/quantities/accessors.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,21 +127,40 @@ def get_value(self, path: str):
current_tree_position: Group | Dataset = self._data

for token in tokens:

options = token.split("|")

if isinstance(current_tree_position, Group):
if token in current_tree_position.children:
current_tree_position = current_tree_position.children[token]
else:

found = False
for option in options:
if option in current_tree_position.children:
current_tree_position = current_tree_position.children[option]
found = True

if self.verbose:
logger.info(f"Found option: {option}")

if not found:
if self.verbose:
logger.info(f"Failed at token {token} on group {current_tree_position.name}. Options: " +
logger.info(f"Failed to find any of {options} on group {current_tree_position.name}. Options: " +
",".join([key for key in current_tree_position.children]))
return None

elif isinstance(current_tree_position, Dataset):
if token in current_tree_position.attributes:
current_tree_position = current_tree_position.attributes[token]
else:

found = False
for option in options:
if option in current_tree_position.attributes:
current_tree_position = current_tree_position.attributes[option]
found = True

if self.verbose:
logger.info(f"Found option: {option}")

if not found:
if self.verbose:
logger.info(f"Failed at token {token} on attribute {current_tree_position.name}. Options: " +
logger.info(f"Failed to find any of {options} on attribute {current_tree_position.name}. Options: " +
",".join([key for key in current_tree_position.attributes]))
return None

Expand Down
22 changes: 15 additions & 7 deletions sasdata/temp_hdf5_reader.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,10 @@
from sasdata.quantities.unit_parser import parse

# test_file = "./example_data/1d_data/33837rear_1D_1.75_16.5_NXcanSAS_v3.h5"
test_file = "./example_data/1d_data/33837rear_1D_1.75_16.5_NXcanSAS.h5"
# test_file = "./example_data/1d_data/33837rear_1D_1.75_16.5_NXcanSAS.h5"
# test_file = "./example_data/2d_data/BAM_2D.h5"
test_file = "./example_data/2d_data/14250_2D_NoDetInfo_NXcanSAS_v3.h5"
# test_file = "./example_data/2d_data/33837rear_2D_1.75_16.5_NXcanSAS_v3.h5"

logger = logging.getLogger(__name__)

Expand Down Expand Up @@ -79,8 +82,12 @@ def connected_data(node: SASDataGroup, name_prefix="") -> list[NamedQuantity]:
value=child.data,
units=units)

if "uncertainty" in child.attributes:
uncertainty_name = child.attributes["uncertainty"]
# Turns out people can't be trusted to use the same keys here
if "uncertainty" in child.attributes or "uncertainties" in child.attributes:
try:
uncertainty_name = child.attributes["uncertainty"]
except:
uncertainty_name = child.attributes["uncertainties"]
uncertainty_map[name] = uncertainty_name
uncertainties.add(uncertainty_name)

Expand Down Expand Up @@ -114,12 +121,13 @@ def load_data(filename) -> list[SasData]:

entry_keys = [key for key in entry.keys()]

if "sasdata" not in entry_keys:
logger.warning("No sasdata key")
if "sasdata" not in entry_keys and "data" not in entry_keys:
logger.warning("No sasdata or data key")

for key in entry_keys:
component = entry[key]
if key.lower() == "sasdata":
lower_key = key.lower()
if lower_key == "sasdata" or lower_key == "data":
datum = recurse_hdf5(component)
# TODO: Use named identifier
data_contents = connected_data(datum, "FILE_ID_HERE")
Expand All @@ -143,4 +151,4 @@ def load_data(filename) -> list[SasData]:
data = load_data(test_file)

for dataset in data:
print(dataset.summary(include_raw=True))
print(dataset.summary(include_raw=False))

0 comments on commit 7c906e8

Please sign in to comment.