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

Save selected layers to folder #57

Merged
merged 6 commits into from
Nov 22, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
41 changes: 28 additions & 13 deletions src/box_manager/_qt/OrganizeLayer.py
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ def _save_ui(self):
"dummyf": QLabel(self),
"suffix": QLineEdit(self),
}
self.save_layers["layer"].addItems([""])
self.save_layers["layer"].addItems(["Selected"])
self.save_layers["dimension"].addItems(["", "2D", "3D"])
self.save_layers["type"].addItems(["", "Particles", "Filaments"])
self.save_layers["suffix"].setPlaceholderText("e.g., _suffix")
Expand Down Expand Up @@ -333,10 +333,7 @@ def _add_ui(self):

@Slot()
def _run_save(self,path=None):
cur_format = self.save_layers["format"].currentText().split(" ", 1)[0]
cur_layer = self.napari_viewer.layers[
self.save_layers["layer"].currentText()
]


cur_spacing = self.save_layers["inter-box distance"].text()
cur_type = self.save_layers["type"].currentText()
Expand All @@ -356,13 +353,28 @@ def _run_save(self,path=None):
self.saved_dir_path = os.getcwd()
return

napari_get_writer(
self.saved_dir_path,
[cur_layer.as_layer_data_tuple()],
cur_format,
self.save_layers["suffix"].text() + cur_format,
cur_spacing=int(cur_spacing),
)

cur_format = self.save_layers["format"].currentText().split(" ", 1)[0]
sel_layer = self.save_layers["layer"].currentText()
layers_to_write = []
if sel_layer == "Selected":
for cur_layer in self.napari_viewer.layers.selection:
if len(cur_layer.data) == 0:
continue
layers_to_write.append(cur_layer)
else:
cur_layer = self.napari_viewer.layers[sel_layer]
layers_to_write.append(cur_layer)

for cur_layer in layers_to_write:
napari_get_writer(
self.saved_dir_path,
[cur_layer.as_layer_data_tuple()],
cur_format,
self.save_layers["suffix"].text() + cur_format,
cur_spacing=int(cur_spacing),
)


@Slot(object)
def _update_link_combo(self, *_):
Expand Down Expand Up @@ -393,7 +405,7 @@ def _update_layer_combo(self, *_):
if isinstance(entry, (napari.layers.Shapes, napari.layers.Points))
]
self.save_layers["layer"].clear()
self.save_layers["layer"].addItems([""] + names)
self.save_layers["layer"].addItems(["Selected"] + names)
try:
self.save_layers["layer"].setCurrentText(
self.napari_viewer.layers.selection.active.name
Expand All @@ -413,6 +425,9 @@ def _save_layer_changed(self, _):
def _update_format(self, _=None, is_layer=False):
cur_layer = self.save_layers["layer"].currentText()

if cur_layer == "Selected":
return

if not cur_layer:
self.save_run_btn.setEnabled(False)
for name, widget in self.save_layers.items():
Expand Down
6 changes: 6 additions & 0 deletions src/box_manager/io/cbox.py
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,9 @@ def _make_df_data_filament(
feature_map["angle"] = "_Angle"
other_interpolation_cols.append("_Angle")

if coordinates.size == 0:
return {"cryolo": pd.DataFrame(columns=coord_columns), "filament_vertices": pd.DataFrame(columns=coord_columns)}

empty_data = copy.deepcopy(data)
filaments = []
entry = 0
Expand Down Expand Up @@ -220,12 +223,15 @@ def _make_df_data_filament(
else:
coordinates[:, [0, 1]] = coordinates[:, [1, 0]]


verts = pd.DataFrame(
coordinates, columns=coord_columns+["_filamentid"]
)
verts["_Width"] = box_size
verts["_Height"] = box_size

result = {"cryolo": pd.concat(filaments), "filament_vertices": verts}

return result


Expand Down
9 changes: 6 additions & 3 deletions src/box_manager/io/io_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -251,7 +251,7 @@ def to_napari_image(
original_path = path
path = sorted(glob.glob(path)) # type: ignore

if not os.path.exists(original_path):
if not os.path.exists(original_path) and is_2d_stack==False:
raise FileNotFoundError(original_path)

else:
Expand All @@ -272,6 +272,7 @@ def to_napari_image(
file_size = (
sum(os.stat(file_name).st_size for file_name in path) / 1024**3
)

if len(path) > 1 and file_size > PROXY_THRESHOLD_GB:
data = LoaderProxy(path, load_image, do_normalize)
else:
Expand Down Expand Up @@ -570,8 +571,10 @@ def convert_shape_filament_layer_to_boxlayer(
[[filid] * len(d) for filid, d in enumerate(data)]
)
)

data = np.concatenate(data)
if len(data) > 0:
data = np.concatenate(data)
else:
data = np.empty((0,0))

return data, meta

Expand Down
Loading