Skip to content

Commit

Permalink
enh: support mirroring the input directory structure in the data conv…
Browse files Browse the repository at this point in the history
…ersion tool (close #29)
  • Loading branch information
paulmueller committed Sep 18, 2023
1 parent b6b7ce5 commit 75e8a6a
Show file tree
Hide file tree
Showing 3 changed files with 80 additions and 15 deletions.
3 changes: 3 additions & 0 deletions CHANGELOG
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
0.14.2
- enh: support mirroring the input directory structure in the data
conversion tool (#29)
0.14.1
- maintenance release
0.14.0
Expand Down
52 changes: 52 additions & 0 deletions pyjibe/head/dlg_tool_convert.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ def __init__(self, parent, *args, **kwargs):
self.toolButton_clear.clicked.connect(self._file_list.clear)

def _convert_merge(self):
"""Merge all curves into one HDF5 file"""
file, _ = QtWidgets.QFileDialog.getSaveFileName(
self.parent(),
"Output file",
Expand All @@ -43,7 +44,55 @@ def _convert_merge(self):
fmt="hdf5")
return True

def _convert_mirror(self):
"""Mirror input file and directory strucutre to output (HDF5 only)"""
out_dir = QtWidgets.QFileDialog.getExistingDirectory(
self.parent(), "Select output directory", "")
if out_dir:
out_dir = pathlib.Path(out_dir)
# Determine the common input path of all files
# TODO:
# This will not work with files on different drives under
# Windows.
pc = str(self.file_list[0])
for pp in self.file_list[1:]:
pc = "".join(
[a for (a, b) in zip(str(pp[:len(pc)]), pc) if a == b])
pc = pathlib.Path(pc)
if not pc.exists():
# There is probably just a piece of a file name stem left
pc = pc.parent
assert pc.exists()
# Create a output path list
out_list = []
for pp in self.file_list:
pp = pathlib.Path(pp)
relp = pp.relative_to(pc)
outp = (out_dir / relp).with_suffix(".h5")
if outp in out_list:
ii = 2
while True:
new = outp.with_name(f"{outp.stem}-{ii}.h5")
if new not in out_list:
outp = new
break
ii += 1
out_list.append(outp)
# Perform the export
for pin, pout in zip(self.file_list, out_list):
pout.parent.mkdir(parents=True, exist_ok=True)
with h5py.File(pout, mode="w") as h5:
fdlist = afmformats.load_data(pin)
for fdist in fdlist:
fdist.export(h5,
metadata=self.get_metadata_keys(fdist),
fmt="hdf5")
return True
else:
return False

def _convert_curve(self):
"""Output all files into one directory, one file per curve"""
out_dir = QtWidgets.QFileDialog.getExistingDirectory(
self.parent(), "Select output directory", "")
if out_dir:
Expand All @@ -64,6 +113,7 @@ def _convert_curve(self):
return False # do not close the dialog

def _convert_unaltered(self):
"""Output all files into one directory (HDF5 only)"""
out_dir = QtWidgets.QFileDialog.getExistingDirectory(
self.parent(), "Select output directory", "")
if out_dir:
Expand Down Expand Up @@ -109,6 +159,8 @@ def convert(self):
return self._convert_unaltered()
elif self.radioButton_merge.isChecked():
return self._convert_merge()
elif self.radioButton_mirror.isChecked():
return self._convert_mirror()
else:
return self._convert_curve()

Expand Down
40 changes: 25 additions & 15 deletions pyjibe/head/dlg_tool_convert.ui
Original file line number Diff line number Diff line change
Expand Up @@ -6,8 +6,8 @@
<rect>
<x>0</x>
<y>0</y>
<width>532</width>
<height>477</height>
<width>536</width>
<height>535</height>
</rect>
</property>
<property name="acceptDrops">
Expand Down Expand Up @@ -70,7 +70,7 @@
<property name="topMargin">
<number>0</number>
</property>
<item row="1" column="0">
<item row="1" column="0" alignment="Qt::AlignTop">
<widget class="QLabel" name="label_3">
<property name="text">
<string>File grouping:</string>
Expand All @@ -79,7 +79,7 @@
</item>
<item row="1" column="1">
<widget class="QWidget" name="widget_group" native="true">
<layout class="QHBoxLayout" name="horizontalLayout_2">
<layout class="QVBoxLayout" name="verticalLayout_2">
<property name="leftMargin">
<number>0</number>
</property>
Expand All @@ -92,36 +92,46 @@
<property name="bottomMargin">
<number>0</number>
</property>
<item>
<widget class="QRadioButton" name="radioButton_mirror">
<property name="text">
<string>mirror original file names and directory structure</string>
</property>
<property name="checked">
<bool>true</bool>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_unaltered">
<property name="toolTip">
<string>Each input file is converted to one output file</string>
</property>
<property name="text">
<string>unaltered</string>
<string>save all files into one directory</string>
</property>
<property name="checked">
<bool>true</bool>
<bool>false</bool>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_merge">
<widget class="QRadioButton" name="radioButton_onefile">
<property name="toolTip">
<string>All input data are merged into one output file</string>
<string>Input files with more than one dataset produce more output files</string>
</property>
<property name="text">
<string>merge all</string>
<string>save all files into one directory, one file per curve</string>
</property>
</widget>
</item>
<item>
<widget class="QRadioButton" name="radioButton_onefile">
<widget class="QRadioButton" name="radioButton_merge">
<property name="toolTip">
<string>Input files with more than one dataset produce more output files</string>
<string>All input data are merged into one output file</string>
</property>
<property name="text">
<string>one file per curve</string>
<string>merge all files into one HDF5 file</string>
</property>
</widget>
</item>
Expand Down Expand Up @@ -279,16 +289,16 @@
<connection>
<sender>radioButton_hdf5</sender>
<signal>toggled(bool)</signal>
<receiver>radioButton_unaltered</receiver>
<receiver>radioButton_mirror</receiver>
<slot>setChecked(bool)</slot>
<hints>
<hint type="sourcelabel">
<x>375</x>
<y>366</y>
</hint>
<hint type="destinationlabel">
<x>166</x>
<y>395</y>
<x>299</x>
<y>366</y>
</hint>
</hints>
</connection>
Expand Down

0 comments on commit 75e8a6a

Please sign in to comment.