Skip to content

Commit

Permalink
Apply non esentail parts of better serialization PR #462 (#533)
Browse files Browse the repository at this point in the history
Co-authored-by: Sourcery AI <>
Co-authored-by: sourcery-ai[bot] <58596630+sourcery-ai[bot]@users.noreply.github.com>
  • Loading branch information
Czaki and sourcery-ai[bot] committed Feb 13, 2022
1 parent dd95a47 commit 31e0127
Show file tree
Hide file tree
Showing 26 changed files with 516 additions and 287 deletions.
13 changes: 1 addition & 12 deletions .github/workflows/test_prereleases.yml
Original file line number Diff line number Diff line change
Expand Up @@ -69,21 +69,10 @@ jobs:
- name: Test with tox linux
# run tests using pip install --pre
if: runner.os == 'Linux'
uses: GabrielBB/xvfb-action@v1
timeout-minutes: 60
with:
run: tox -v --pre
env:
PLATFORM: ${{ matrix.platform }}
PYVISTA_OFF_SCREEN: True # required for opengl on windows
NAPARI: None

- name: Test with tox linux
# run tests using pip install --pre
if: runner.os != 'Linux'
timeout-minutes: 60
run: tox -v --pre
run: python -m tox -v --pre
env:
PLATFORM: ${{ matrix.platform }}
PYVISTA_OFF_SCREEN: True # required for opengl on windows
Expand Down
19 changes: 4 additions & 15 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -74,21 +74,10 @@ jobs:
- name: Test with tox linux
# run tests using pip install --pre
if: runner.os == 'Linux'
uses: GabrielBB/xvfb-action@v1
timeout-minutes: 60
with:
run: tox
env:
PLATFORM: ${{ matrix.platform }}
PYVISTA_OFF_SCREEN: True # required for opengl on windows
NAPARI: None

- name: Test with tox linux
# run tests using pip install --pre
if: runner.os != 'Linux'
timeout-minutes: 60
run: tox
run: python -m tox
env:
PLATFORM: ${{ matrix.platform }}
PYVISTA_OFF_SCREEN: True # required for opengl on windows
Expand Down Expand Up @@ -136,7 +125,7 @@ jobs:
# run tests using pip install --pre
uses: GabrielBB/xvfb-action@v1
with:
run: tox -e py38-PyQt5-all
run: python -m tox -e py38-PyQt5-all
timeout-minutes: 60

test_coverage:
Expand Down Expand Up @@ -168,7 +157,7 @@ jobs:
- name: Test with tox
uses: GabrielBB/xvfb-action@v1
with:
run: tox -e py38-PyQt5-coverage
run: python -m tox -e py38-PyQt5-coverage
timeout-minutes: 60

- uses: codecov/codecov-action@v1
Expand Down Expand Up @@ -200,5 +189,5 @@ jobs:
- name: Test with tox
uses: GabrielBB/xvfb-action@v1
with:
run: tox -e py37-PyQt5-minimal
run: python -m tox -e py37-PyQt5-minimal
timeout-minutes: 60
1 change: 1 addition & 0 deletions MANIFEST.in
Original file line number Diff line number Diff line change
Expand Up @@ -38,3 +38,4 @@ exclude dist/**
exclude dist/**.swp
exclude cliff.toml
exclude runtime.txt
exclude .sourcery.yaml
2 changes: 1 addition & 1 deletion package/PartSeg/_roi_analysis/main_window.py
Original file line number Diff line number Diff line change
Expand Up @@ -278,7 +278,7 @@ def save_profile(self):
QMessageBox.No,
):
continue
resp = ROIExtractionProfile(text, widget.name, widget.get_values())
resp = ROIExtractionProfile(name=text, algorithm=widget.name, values=widget.get_values())
self._settings.roi_profiles[text] = resp
self._settings.dump()
break
Expand Down
15 changes: 9 additions & 6 deletions package/PartSeg/_roi_analysis/prepare_plan_widget.py
Original file line number Diff line number Diff line change
Expand Up @@ -468,9 +468,9 @@ def change_segmentation_table(self):
index = self.segment_stack.currentIndex()
text = self.segment_stack.tabText(index)
if self.update_element_chk.isChecked():
self.chose_profile_btn.setText("Replace " + text)
self.chose_profile_btn.setText(f"Replace {text}")
else:
self.chose_profile_btn.setText("Add " + text)
self.chose_profile_btn.setText(f"Add {text}")
self.segment_profile.setCurrentItem(None)
self.pipeline_profile.setCurrentItem(None)

Expand Down Expand Up @@ -800,9 +800,12 @@ def refresh_profiles(self, list_widget: QListWidget, new_values: typing.List[str

@contextmanager
def enable_protect(self):
previous = self.protect
self.protect = True
yield
self.protect = False
try:
yield
finally:
self.protect = previous

def _refresh_measurement(self):
new_measurements = list(sorted(self.settings.measurement_profiles.keys(), key=str.lower))
Expand Down Expand Up @@ -1125,7 +1128,7 @@ def delete_plan(self):
if self.calculate_plans.currentItem() is None:
return
text = str(self.calculate_plans.currentItem().text())
if text == "":
if not text:
return
if text in self.settings.batch_plans:
del self.settings.batch_plans[text]
Expand All @@ -1135,7 +1138,7 @@ def edit_plan(self):
if self.calculate_plans.currentItem() is None:
return
text = str(self.calculate_plans.currentItem().text())
if text == "":
if not text:
return
if text in self.settings.batch_plans:
self.plan_to_edit = self.settings.batch_plans[text]
Expand Down
2 changes: 1 addition & 1 deletion package/PartSeg/_roi_mask/simple_measurements.py
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,7 @@ def calculate(self):
QMessageBox.warning(self, "No measurement", "Select at least one measurement")
return

profile = MeasurementProfile("", [MeasurementEntry(x.name, x) for x in to_calculate])
profile = MeasurementProfile("", [MeasurementEntry(name=x.name, calculation_tree=x) for x in to_calculate])

dial = ExecuteFunctionDialog(
profile.calculate,
Expand Down
18 changes: 9 additions & 9 deletions package/PartSeg/common_backend/base_argparser.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,9 @@ def proper_suffix(val: str):
:raise argparse.ArgumentTypeError: on validation error
"""
if len(val) > 0 and not val.isalnum():
raise argparse.ArgumentTypeError(f"suffix '{val}' need to contains only alpha numeric characters")
return val
if not val or val.isalnum():
return val
raise argparse.ArgumentTypeError(f"suffix '{val}' need to contains only alpha numeric characters")


def proper_path(val: str):
Expand All @@ -40,8 +40,9 @@ def proper_path(val: str):
return val
try:
os.makedirs(val)
except OSError:
raise argparse.ArgumentTypeError(f" Path {val} is not a valid path in this system")
except OSError as e:
raise argparse.ArgumentTypeError(f" Path {val} is not a valid path in this system") from e

return val


Expand Down Expand Up @@ -105,8 +106,9 @@ def parse_args(self, args: Optional[Sequence[str]] = None, namespace: Optional[a
state_store.develop = args.develop
state_store.save_suffix = args.save_suffix[0]
state_store.save_folder = os.path.abspath(
args.save_directory[0] + ("_" + state_store.save_suffix if state_store.save_suffix else "")
args.save_directory[0] + (f"_{state_store.save_suffix}" if state_store.save_suffix else "")
)

if args.no_report and args.no_dialog:
_setup_sentry()
sys.excepthook = my_excepthook
Expand All @@ -129,6 +131,4 @@ def _setup_sentry(): # pragma: no cover


def safe_repr(val):
if isinstance(val, np.ndarray):
return numpy_repr(val)
return _safe_repr(val)
return numpy_repr(val) if isinstance(val, np.ndarray) else _safe_repr(val)
20 changes: 9 additions & 11 deletions package/PartSeg/common_backend/base_settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,8 +176,8 @@ def roi(self, val: Union[np.ndarray, ROIInfo]):
self._roi_info = ROIInfo(self.image.fit_array_to_image(val))
else:
self._roi_info = val.fit_to_image(self.image)
except ValueError:
raise ValueError(ROI_NOT_FIT)
except ValueError as e:
raise ValueError(ROI_NOT_FIT) from e
self._additional_layers = {}
self.roi_changed.emit(self._roi_info)

Expand Down Expand Up @@ -540,8 +540,8 @@ def set_segmentation_result(self, result: ROIExtractionResult):
# Fixme not use EventedDict here
try:
roi_info = result.roi_info.fit_to_image(self.image)
except ValueError: # pragma: no cover
raise ValueError(ROI_NOT_FIT)
except ValueError as e: # pragma: no cover
raise ValueError(ROI_NOT_FIT) from e
if result.points is not None:
self.points = result.points
self._roi_info = roi_info
Expand Down Expand Up @@ -601,8 +601,8 @@ def mask(self, value):
try:
self._image.set_mask(value)
self.mask_changed.emit()
except ValueError:
raise ValueError("mask do not fit to image")
except ValueError as e:
raise ValueError("mask do not fit to image") from e

def get_save_list(self) -> List[SaveSettingsDescription]:
"""List of files in which program save the state."""
Expand All @@ -618,7 +618,7 @@ def get_path_history(self) -> List[str]:
"""
res = self.get(DIR_HISTORY, [])[:]
for name in self.save_locations_keys:
val = self.get("io." + name, str(Path.home()))
val = self.get(f"io.{name}", str(Path.home()))
if val not in res:
res = res + [val]
return res
Expand Down Expand Up @@ -698,9 +698,7 @@ def load_part(cls, file_path):
data = cls.load_metadata(file_path)
bad_key = []
if isinstance(data, MutableMapping) and not check_loaded_dict(data):
for k, v in data.items():
if not check_loaded_dict(v):
bad_key.append(k)
bad_key.extend(k for k, v in data.items() if not check_loaded_dict(v))
for el in bad_key:
del data[el]
elif isinstance(data, ProfileDict) and not data.verify_data():
Expand Down Expand Up @@ -762,7 +760,7 @@ def load(self, folder_path: Union[Path, str, None] = None):
if error:
timestamp = datetime.today().strftime("%Y-%m-%d_%H_%M_%S")
base_path, ext = os.path.splitext(file_path)
os.rename(file_path, base_path + "_" + timestamp + ext)
os.rename(file_path, f"{base_path}_{timestamp}{ext}")

if errors_list:
logger.error(errors_list)
Expand Down
13 changes: 5 additions & 8 deletions package/PartSeg/common_gui/algorithms_description.py
Original file line number Diff line number Diff line change
Expand Up @@ -467,12 +467,9 @@ def algorithm_choose(self, name):
if name not in self.property.possible_values:
return
start_dict = {} if name not in self.starting_values else self.starting_values[name]
try:
self.widgets_dict[name] = FormWidget(
self.property.possible_values[name].get_fields(), start_values=start_dict
)
except KeyError as e:
raise e
self.widgets_dict[name] = FormWidget(
self.property.possible_values[name].get_fields(), start_values=start_dict
)
self.widgets_dict[name].layout().setContentsMargins(0, 0, 0, 0)
self.layout().addWidget(self.widgets_dict[name])
self.widgets_dict[name].value_changed.connect(self.values_changed)
Expand Down Expand Up @@ -633,7 +630,7 @@ def enable_selector(self):
el.setEnabled(True)

def get_segmentation_profile(self) -> ROIExtractionProfile:
return ROIExtractionProfile("", self.algorithm.get_name(), self.get_values())
return ROIExtractionProfile(name="", algorithm=self.algorithm.get_name(), values=self.get_values())


class AlgorithmChooseBase(QWidget):
Expand Down Expand Up @@ -738,7 +735,7 @@ def current_widget(self) -> InteractiveAlgorithmSettingsWidget:

def current_parameters(self) -> ROIExtractionProfile:
widget = self.current_widget()
return ROIExtractionProfile("", widget.name, widget.get_values())
return ROIExtractionProfile(name="", algorithm=widget.name, values=widget.get_values())

def get_info_text(self):
return self.current_widget().algorithm_thread.get_info_text()
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,7 +172,7 @@ def save_action(self):
QMessageBox.No,
):
break # pragma: no cover
resp = ROIExtractionProfile(text, widget.name, widget.get_values())
resp = ROIExtractionProfile(name=text, algorithm=widget.name, values=widget.get_values())
profiles[text] = resp
self.settings.dump()
self.profile_combo_box.addItem(text)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -67,7 +67,7 @@ def _calculate(self, event=None):
warnings.warn("No measurement. Select at least one measurement")
return

profile = MeasurementProfile("", [MeasurementEntry(x.name, x) for x in to_calculate])
profile = MeasurementProfile("", [MeasurementEntry(name=x.name, calculation_tree=x) for x in to_calculate])

data_layer = self.image_choice.value or self.labels_choice.value

Expand Down
8 changes: 4 additions & 4 deletions package/PartSegCore/analysis/calculation_plan.py
Original file line number Diff line number Diff line change
Expand Up @@ -705,9 +705,9 @@ def dict_load(cls, data_dict):
res_plan.current_pos = pos[:-1]
try:
res_plan.add_step(CalculationPlan.correct_name[el["type"]](**el["values"]))
except TypeError as e:
except TypeError:
logging.warning(el["type"])
raise e
raise
res_plan.changes = []
return res_plan

Expand Down Expand Up @@ -743,8 +743,8 @@ def get_el_name(el):
if isinstance(el, Save):
base = el.short_name
if el.directory:
return f"Save {base} in directory with name " + el.suffix
return "Save " + base + " with suffix " + el.suffix if el.suffix != "" else "Save " + base
return f"Save {base} in directory with name {el.suffix}"
return f"Save {base} with suffix {el.suffix}" if el.suffix != "" else f"Save {base}"

if isinstance(el, MaskIntersection):
if el.name == "":
Expand Down
18 changes: 8 additions & 10 deletions package/PartSegCore/analysis/load_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -228,7 +228,7 @@ def load(
metadata = {"default_spacing": (10**-6, 10**-6, 10**-6)}
if len(load_locations) == 1:
new_path, ext = os.path.splitext(load_locations[0])
new_path += "_mask" + ext
new_path += f"_mask{ext}"
if not os.path.exists(new_path):
raise ValueError("Cannot determine mask file. It need to have '_mask' suffix.")
load_locations.append(load_locations)
Expand All @@ -243,7 +243,7 @@ def load(
@classmethod
def get_next_file(cls, file_paths: typing.List[str]):
base, ext = os.path.splitext(file_paths[0])
return base + "_mask" + ext
return f"{base}_mask{ext}"


class LoadMask(LoadBase):
Expand Down Expand Up @@ -349,9 +349,9 @@ def update_segmentation_pipeline_element(cls, data: SegmentationPipelineElement)
@classmethod
def update_segmentation_pipeline(cls, data: SegmentationPipeline) -> SegmentationPipeline:
return SegmentationPipeline(
data.name,
cls.update_segmentation_profile(data.segmentation),
[cls.update_segmentation_pipeline_element(x) for x in data.mask_history],
name=data.name,
segmentaion=cls.update_segmentation_profile(data.segmentation),
mask_history=[cls.update_segmentation_pipeline_element(x) for x in data.mask_history],
)

@classmethod
Expand Down Expand Up @@ -426,9 +426,7 @@ def load(
data = load_metadata(load_locations[0])
bad_key = []
if isinstance(data, typing.MutableMapping) and not check_loaded_dict(data):
for k, v in data.items():
if not check_loaded_dict(v):
bad_key.append(k)
bad_key.extend(k for k, v in data.items() if not check_loaded_dict(v))
for el in bad_key:
del data[el]
elif isinstance(data, ProfileDict) and not data.verify_data():
Expand All @@ -452,9 +450,9 @@ def load_metadata(data: typing.Union[str, Path]):

def update_algorithm_dict(dkt):
if "name" in dkt:
profile = ROIExtractionProfile("", dkt["name"], dkt["values"])
profile = ROIExtractionProfile(name="", algorithm=dkt["name"], values=dkt["values"])
elif "algorithm_name" in dkt:
profile = ROIExtractionProfile("", dkt["algorithm_name"], dkt["values"])
profile = ROIExtractionProfile(name="", algorithm=dkt["algorithm_name"], values=dkt["values"])
else:
return dkt
profile = UpdateLoadedMetadataAnalysis.recursive_update(profile)
Expand Down

0 comments on commit 31e0127

Please sign in to comment.