Skip to content

Commit

Permalink
Isolate changes to base class in a new function (avoid function compl…
Browse files Browse the repository at this point in the history
…exity threshold)
  • Loading branch information
annehaley committed Oct 10, 2024
1 parent a3e0da1 commit 4e1ed1a
Showing 1 changed file with 42 additions and 32 deletions.
74 changes: 42 additions & 32 deletions large_image/tilesource/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -1315,6 +1315,47 @@ def getMetadata(self) -> JSONDict:
def metadata(self) -> JSONDict:
return self.getMetadata()

def _getFrameValueInformation(self, frames: List[Dict]):
refvalues: Dict[str, Dict[str, List]] = {}
for idx, frame in enumerate(frames):
for key, value in frame.items():
if 'Value' in key:
if key not in refvalues:
refvalues[key] = {}
value_index = frame.get(key.replace('Value', 'Index'))
if value_index not in refvalues[key]:
refvalues[key][value_index] = [value]
else:
refvalues[key][value_index].append(value)
frame_value_info = {}
for key, value_mapping in refvalues.items():
axis_name = key.replace('Value', '').lower()
units = None
if hasattr(self, 'frameUnits') and self.frameUnits is not None:
units = self.frameUnits.get(axis_name)
uniform = all(len(set(value_list)) <= 1 for value_list in value_mapping.values())
# after evaluating uniform, continue with only
# the first value for each index along this axis
first_values = [
value_list[0] for value_list in value_mapping.values() if len(value_list)
]
try:
min_val = min(first_values)
max_val = max(first_values)
except TypeError:
min_val = None
max_val = None
frame_value_info[key] = dict(
values=first_values,
uniform=uniform,
units=units,
min=min_val,
max=max_val,
datatype=np.array(first_values).dtype.name,
)
return frame_value_info


def _addMetadataFrameInformation(
self, metadata: JSONDict, channels: Optional[List[str]] = None) -> None:
"""
Expand All @@ -1332,18 +1373,8 @@ def _addMetadataFrameInformation(
return
maxref: Dict[str, int] = {}
refkeys = {'IndexC'}
refvalues = {}
index = 0
for idx, frame in enumerate(metadata['frames']):
for key, value in frame.items():
if 'Value' in key:
if key not in refvalues:
refvalues[key] = {}
value_index = frame.get(key.replace('Value', 'Index'))
if value_index not in refvalues[key]:
refvalues[key][value_index] = [value]
else:
refvalues[key][value_index].append(value)
refkeys |= {key for key in frame
if key.startswith('Index') and len(key.split('Index', 1)[1])}
for key in refkeys:
Expand All @@ -1356,28 +1387,7 @@ def _addMetadataFrameInformation(
metadata['frames'][idx].get(key) for key in refkeys)):
index += 1
frame['Index'] = index
for key, value_mapping in refvalues.items():
axis_name = key.replace('Value', '').lower()
units = None
if hasattr(self, 'frameUnits') and self.frameUnits is not None:
units = self.frameUnits.get(axis_name)
uniform = all(len(set(value_list)) <= 1 for value_list in value_mapping.values())
# after evaluating uniform, continue with only the first value for each index along this axis
first_values = [value_list[0] for value_list in value_mapping.values() if len(value_list)]
try:
min_val = min(first_values)
max_val = max(first_values)
except TypeError:
min_val = None
max_val = None
metadata[key] = dict(
values=first_values,
uniform=uniform,
units=units,
min=min_val,
max=max_val,
datatype=np.array(first_values).dtype.name,
)
metadata.update(self._getFrameValueInformation(metadata['frames']))
if any(val > 1 for val in maxref.values()):
metadata['IndexRange'] = {key: value for key, value in maxref.items() if value > 1}
metadata['IndexStride'] = {
Expand Down

0 comments on commit 4e1ed1a

Please sign in to comment.