@@ -24,25 +24,41 @@ class DaskImageDataStack:
24
24
"""
25
25
A Dask Image Data Stack Class to hold a delayed array of all the images in the Live Viewer Path
26
26
"""
27
- delayed_stack : dask .array .Array | None = None
27
+ delayed_stack : dask .array .Array
28
+ image_list : list [Image_Data ]
28
29
29
- def __init__ (self , image_list : list [Image_Data ] | None ):
30
- if image_list :
31
- if image_list [0 ].create_delayed_array :
30
+ def __init__ (self , image_list : list [Image_Data ], create_delayed_array : bool = True ):
31
+ self .image_list = image_list
32
+
33
+ if image_list and create_delayed_array :
34
+ arrays = self .get_delayed_arrays ()
35
+ if arrays :
32
36
if image_list [0 ].image_path .suffix .lower () in [".tif" , ".tiff" ]:
33
- arrays = [image_data .delayed_array for image_data in image_list ]
34
37
self .delayed_stack = dask .array .stack (dask .array .array (arrays ))
35
38
elif image_list [0 ].image_path .suffix .lower () in [".fits" ]:
36
39
with fits .open (image_list [0 ].image_path .__str__ ()) as fit :
37
40
sample = fit [0 ].data
38
- arrays = [image_data .delayed_array for image_data in image_list ]
39
41
lazy_arrays = [dask .array .from_delayed (x , shape = sample .shape , dtype = sample .dtype ) for x in arrays ]
40
42
self .delayed_stack = dask .array .stack (lazy_arrays )
41
43
42
44
@property
43
45
def shape (self ):
44
46
return self .delayed_stack .shape
45
47
48
+ def get_delayed_arrays (self ) -> list [dask .array .Array ] | None :
49
+ if self .image_list [0 ].image_path .suffix .lower () in [".tif" , ".tiff" ]:
50
+ return [dask_image .imread .imread (image_data .image_path )[0 ] for image_data in self .image_list ]
51
+ elif self .image_list [0 ].image_path .suffix .lower () == ".fits" :
52
+ return [dask .delayed (fits .open )(image_data .image_path )[0 ].data for image_data in self .image_list ]
53
+ else :
54
+ return None
55
+
56
+ def get_delayed_image (self , index ) -> dask .array .Array :
57
+ return self .delayed_stack [index ]
58
+
59
+ def get_image_data (self , index ) -> Image_Data :
60
+ return self .image_list [index ]
61
+
46
62
47
63
class Image_Data :
48
64
"""
@@ -66,7 +82,7 @@ class Image_Data:
66
82
delayed_array : dask .array .Array
67
83
create_delayed_array : bool
68
84
69
- def __init__ (self , image_path : Path , create_delayed_array : bool = True ):
85
+ def __init__ (self , image_path : Path ):
70
86
"""
71
87
Constructor for Image_Data class.
72
88
@@ -78,9 +94,6 @@ def __init__(self, image_path: Path, create_delayed_array: bool = True):
78
94
self .image_path = image_path
79
95
self .image_name = image_path .name
80
96
self ._stat = image_path .stat ()
81
- self .create_delayed_array = create_delayed_array
82
- if self .create_delayed_array :
83
- self .set_delayed_array ()
84
97
85
98
@property
86
99
def stat (self ) -> stat_result :
@@ -96,12 +109,6 @@ def image_modified_time_stamp(self) -> str:
96
109
"""Return the image modified time as a string"""
97
110
return time .strftime ("%Y-%m-%d %H:%M:%S" , time .localtime (self .image_modified_time ))
98
111
99
- def set_delayed_array (self ) -> None :
100
- if self .image_path .suffix .lower () in [".tif" , ".tiff" ]:
101
- self .delayed_array = dask_image .imread .imread (self .image_path )[0 ]
102
- elif self .image_path .suffix .lower () == ".fits" :
103
- self .delayed_array = dask .delayed (fits .open )(self .image_path )[0 ].data
104
-
105
112
106
113
class SubDirectory :
107
114
@@ -145,7 +152,7 @@ def __init__(self, presenter: LiveViewerWindowPresenter):
145
152
self ._dataset_path : Path | None = None
146
153
self .image_watcher : ImageWatcher | None = None
147
154
self .images : list [Image_Data ] = []
148
- self .image_stack : DaskImageDataStack | None
155
+ self .image_stack : DaskImageDataStack
149
156
150
157
@property
151
158
def path (self ) -> Path | None :
@@ -159,9 +166,8 @@ def path(self, path: Path) -> None:
159
166
self .image_watcher .recent_image_changed .connect (self .handle_image_modified )
160
167
self .image_watcher ._handle_notified_of_directry_change (str (path ))
161
168
162
- def _handle_image_changed_in_list (self ,
163
- image_files : list [Image_Data ],
164
- dask_image_stack : DaskImageDataStack | None = None ) -> None :
169
+ def _handle_image_changed_in_list (self , image_files : list [Image_Data ],
170
+ dask_image_stack : DaskImageDataStack ) -> None :
165
171
"""
166
172
Handle an image changed event. Update the image in the view.
167
173
This method is called when the image_watcher detects a change
@@ -243,7 +249,7 @@ def find_images(self, directory: Path) -> list[Image_Data]:
243
249
for file_path in directory .iterdir ():
244
250
if self ._is_image_file (file_path .name ):
245
251
try :
246
- image_obj = Image_Data (file_path , create_delayed_array = self . create_delayed_array )
252
+ image_obj = Image_Data (file_path )
247
253
image_files .append (image_obj )
248
254
except FileNotFoundError :
249
255
continue
@@ -315,7 +321,7 @@ def _handle_directory_change(self) -> None:
315
321
break
316
322
317
323
images = self .sort_images_by_modified_time (images )
318
- dask_image_stack = DaskImageDataStack (images )
324
+ dask_image_stack = DaskImageDataStack (images , create_delayed_array = self . create_delayed_array )
319
325
self .update_recent_watcher (images [- 1 :])
320
326
self .image_changed .emit (images , dask_image_stack )
321
327
0 commit comments