Skip to content

Commit

Permalink
Merge pull request #3 from DragaDoncila/sorting-timestamps
Browse files Browse the repository at this point in the history
Sorting timestamps correctly
  • Loading branch information
DragaDoncila authored Sep 28, 2020
2 parents dcfa450 + 5c7bf6a commit 8e2bd8f
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 7 deletions.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -79,3 +79,6 @@ target/

# written by setuptools_scm
*/_version.py

# misc
*/_tests/test_loading.py
12 changes: 7 additions & 5 deletions napari_sentinel_zip/_tests/test_napari_sentinel_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@

IM_SHAPES = [
(1000, 1000, 3), #QKL
(10980, 10980), #Mask
(5490, 5490), #Mask
(5490, 5490),
(5490, 5490),
(10980, 10980),
Expand Down Expand Up @@ -50,7 +52,7 @@ def check_layer_list_structure(layer_list, num_ims, num_layers):
# all elements in the layers must be tuples
assert isinstance(layer, tuple), f"Layer list element {i} is not tuple"

# each data in tuples must have correct shape
# each data in tuples must have correct shape
expected_shape = tuple([num_ims] + [im_axis_shape for im_axis_shape in IM_SHAPES[i]])
assert layer[0].shape == expected_shape,\
f"Layer {i} has shape {layer[0].shape}, expected {expected_shape}"
Expand All @@ -68,7 +70,7 @@ def check_viewer_open_structure(path, num_layers, num_dims):

def test_reader_with_list():
NUM_IMS = 10
NUM_LAYERS = 21
NUM_LAYERS = 23
NUM_DIMS = 3

reader = napari_get_reader(TEST_ZIP_PATH_LIST)
Expand All @@ -81,15 +83,15 @@ def test_reader_with_list():
for layer in layer_list:
v.add_image(layer[0], **layer[1])

# viewer must have 21 layers after opening
# viewer must have 23 layers after opening
assert len(v.layers) == NUM_LAYERS, f"Expected {NUM_LAYERS} layers after opening in napari viewer, got {len(v.layers)}"
# viewer should have 3 dimensions after opening
assert v.dims.ndim == NUM_DIMS, f"Expected {NUM_DIMS} dimensions after opening in napari viewer, got {v.dims.ndim}"


def test_reader_with_string_path():
NUM_IMS = 1
NUM_LAYERS = 21
NUM_LAYERS = 23
NUM_DIMS = 3

reader = napari_get_reader(TEST_ONE_ZIP_PATH)
Expand All @@ -103,7 +105,7 @@ def test_reader_with_string_path():

def test_reader_with_root_directory():
NUM_IMS = 10
NUM_LAYERS = 21
NUM_LAYERS = 23
NUM_DIMS = 3

reader = napari_get_reader(TEST_TILE_PATH)
Expand Down
18 changes: 16 additions & 2 deletions napari_sentinel_zip/napari_sentinel_zip.py
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,18 @@ def ziptiff2array(zip_filename, path_to_tiff):
image = tiff_f.pages[0].asarray()
return image

def sort_timestamps(path_list):
timestamp_regex = re.compile(".*([0-9]{8}-[0-9]{6}-[0-9]{3}).*.zip")
timestamp_dict = {}
for path in path_list:
match = timestamp_regex.match(path)
if match:
timestamp = match.groups()[0]
timestamp_dict[timestamp] = path
sorted_paths = []
for timestamp in sorted(timestamp_dict.keys()):
sorted_paths.append(timestamp_dict[timestamp])
return sorted_paths

@napari_hook_implementation
def napari_get_reader(path):
Expand Down Expand Up @@ -172,10 +184,12 @@ def reader_function(path):
paths = [path]
# list of sentinel zips
elif isinstance(path, list):
paths = sorted(path)
paths = path
# one root directory path with multiple sentinel zips inside
else:
paths = sorted(filter(SENTINEL_PATH_REGEX.match, glob(path + "/*.zip")))
paths = list(filter(SENTINEL_PATH_REGEX.match, glob(path + "/*.zip")))

paths = sort_timestamps(paths)

# stack all timepoints together for each band
images = {}
Expand Down

0 comments on commit 8e2bd8f

Please sign in to comment.