Skip to content

Commit 66a001e

Browse files
committed
make Speroid file also work with relative paths
1 parent e179f82 commit 66a001e

File tree

6 files changed

+66
-25
lines changed

6 files changed

+66
-25
lines changed

saenopy/gui/common/gui_classes.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -302,7 +302,7 @@ def __init__(self, layout, editable=False, add_item_button=False, color_picker=F
302302
self.customContextMenuRequested.connect(self.list2_context_menu)
303303
self.itemChanged.connect(self.list2_checked_changed)
304304
self.itemChanged = self.itemChanged2
305-
self.act_delete = QtWidgets.QAction(qta.icon("fa.minus"), "Remove", self)
305+
self.act_delete = QtWidgets.QAction(qta.icon("fa5s.minus"), "Remove", self)
306306
self.act_delete.triggered.connect(self.delete_item)
307307

308308
self.act_color = None

saenopy/gui/spheroid/modules/DeformationDetector.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,7 @@ class DeformationDetector(PipelineModule):
2222
pipeline_name = "find deformations"
2323
use_thread = False
2424
progress_signal = QtCore.Signal(int, str)
25+
result: ResultSpheroid = None
2526

2627
def __init__(self, parent: "BatchEvaluate", layout):
2728
super().__init__(parent, layout)
@@ -232,7 +233,7 @@ def update_display(self, *, plotter=None):
232233
if self.show_seg.value():
233234
thresh_segmentation = self.thresh_segmentation.value()
234235
if not self.continuous_segmentation.value():
235-
im0 = imageio.v2.imread(self.result.images[0]).astype(float)
236+
im0 = imageio.v2.imread(self.result.get_image_data(0, return_filename=True)).astype(float)
236237
seg0 = jf.piv.segment_spheroid(im0, True, thresh_segmentation)
237238
from skimage import measure
238239
# Find contours at a constant value of 0.8

saenopy/gui/spheroid/modules/path_editor.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,10 @@
44

55
from saenopy.gui.common import QtShortCuts
66
from saenopy.gui.solver.modules.path_editor import PathChanger
7+
from saenopy.gui.spheroid.modules.result import ResultSpheroid
78

89

9-
def start_path_change(parent, result):
10+
def start_path_change(parent, result: ResultSpheroid):
1011
path_editor = PathEditor(parent, result)
1112
if not path_editor.exec():
1213
return

saenopy/gui/spheroid/modules/result.py

Lines changed: 28 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
import matplotlib.pyplot as plt
1010

1111
from saenopy.saveable import Saveable
12-
from saenopy.result_file import make_path_absolute
12+
from saenopy.result_file import make_path_absolute, make_path_relative
1313

1414

1515

@@ -70,11 +70,30 @@ def __init__(self, template, images, output, **kwargs):
7070

7171
super().__init__(**kwargs)
7272

73-
def save(self, file_name=None):
74-
if file_name is None:
75-
file_name = self.output
73+
def save(self, filename: str = None):
74+
if filename is None:
75+
self.template = make_path_absolute(self.template, Path(self.output).parent)
76+
for i in range(len(self.images)):
77+
self.images[i] = make_path_absolute(self.images[i], Path(self.output).parent)
78+
79+
filename = self.output
80+
81+
self.template = make_path_relative(self.template, Path(self.output).parent)
82+
for i in range(len(self.images)):
83+
self.images[i] = make_path_relative(self.images[i], Path(self.output).parent)
7684
Path(self.output).parent.mkdir(exist_ok=True, parents=True)
77-
super().save(file_name)
85+
super().save(filename)
86+
87+
def on_load(self, filename: str):
88+
self.template = make_path_relative(self.template, Path(self.output).parent)
89+
for i in range(len(self.images)):
90+
self.images[i] = make_path_relative(self.images[i], Path(self.output).parent)
91+
92+
self.output = str(Path(filename))
93+
94+
self.template = make_path_absolute(self.template, Path(self.output).parent)
95+
for i in range(len(self.images)):
96+
self.images[i] = make_path_absolute(self.images[i], Path(self.output).parent)
7897

7998
def get_absolute_path(self):
8099
return make_path_absolute(self.template, Path(self.output).parent)
@@ -101,9 +120,11 @@ def get_data_structure(self):
101120
}
102121
}
103122

104-
def get_image_data(self, time_point, channel="default", use_reference=False):
123+
def get_image_data(self, time_point, channel="default", use_reference=False, return_filename=False):
105124
try:
106-
im = imread(self.images[time_point])
125+
if return_filename:
126+
return make_path_absolute(self.images[time_point], Path(self.output).parent)
127+
im = imread(make_path_absolute(self.images[time_point], Path(self.output).parent))
107128
except FileNotFoundError as err:
108129
traceback.print_exception(err)
109130
h = 255

saenopy/gui/tfm2d/modules/result.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -144,6 +144,9 @@ def save(self, file_name=None):
144144
Path(self.output).parent.mkdir(exist_ok=True, parents=True)
145145
super().save(file_name)
146146

147+
def on_load(self, filename: str):
148+
self.output = str(Path(filename))
149+
147150
def get_mask(self):
148151
if self.mask is None:
149152
self.mask = get_mask_using_gui(self.bf)

saenopy/result_file.py

Lines changed: 30 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import glob
22
import re
33
from pathlib import Path
4+
from pathlib import PurePath, PureWindowsPath
45
import os
56
import natsort
67
from typing import List
@@ -227,21 +228,35 @@ def common_start(values):
227228
start = start[:-1]
228229

229230

230-
def make_path_relative(template, output):
231-
template = str(Path(template).absolute())
232-
output = str(Path(output).absolute())
233-
# relative and optionally go up to two folders up
234-
try:
235-
template = Path(template).relative_to(output)
236-
except ValueError:
237-
try:
238-
template = Path("..") / Path(template).relative_to(Path(output).parent)
239-
except ValueError:
240-
try:
241-
template = Path("../..") / Path(template).relative_to(Path(output).parent.parent)
242-
except ValueError:
243-
pass
244-
return str(template)
231+
def make_path_relative(filename, base):
232+
"""
233+
Returns the relative path of `filename` with respect to the `base` directory,
234+
working cross-platform for Windows paths on non-Windows systems.
235+
236+
:param filename: The path of the file.
237+
:param base: The base directory to which the path should be relative.
238+
:return: The relative path of the file.
239+
"""
240+
# Convert to PurePath for cross-platform compatibility
241+
base_path = PurePath(base)
242+
file_path = PurePath(filename)
243+
244+
# Use PureWindowsPath if on a non-Windows system handling Windows paths
245+
if not isinstance(base_path, PureWindowsPath) and '\\' in str(base):
246+
base_path = PureWindowsPath(base)
247+
if not isinstance(file_path, PureWindowsPath) and '\\' in str(filename):
248+
file_path = PureWindowsPath(filename)
249+
250+
if not file_path.is_absolute():
251+
return file_path
252+
253+
offset = ""
254+
for i in range(3):
255+
if file_path.is_relative_to(base_path):
256+
return str(offset / file_path.relative_to(base_path))
257+
base_path = base_path.parent
258+
offset /= PurePath("..")
259+
return file_path
245260

246261

247262
def make_path_absolute(template, output):

0 commit comments

Comments
 (0)