Skip to content

Commit 75e8a6a

Browse files
committed
enh: support mirroring the input directory structure in the data conversion tool (close #29)
1 parent b6b7ce5 commit 75e8a6a

File tree

3 files changed

+80
-15
lines changed

3 files changed

+80
-15
lines changed

CHANGELOG

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,3 +1,6 @@
1+
0.14.2
2+
- enh: support mirroring the input directory structure in the data
3+
conversion tool (#29)
14
0.14.1
25
- maintenance release
36
0.14.0

pyjibe/head/dlg_tool_convert.py

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,7 @@ def __init__(self, parent, *args, **kwargs):
2424
self.toolButton_clear.clicked.connect(self._file_list.clear)
2525

2626
def _convert_merge(self):
27+
"""Merge all curves into one HDF5 file"""
2728
file, _ = QtWidgets.QFileDialog.getSaveFileName(
2829
self.parent(),
2930
"Output file",
@@ -43,7 +44,55 @@ def _convert_merge(self):
4344
fmt="hdf5")
4445
return True
4546

47+
def _convert_mirror(self):
48+
"""Mirror input file and directory strucutre to output (HDF5 only)"""
49+
out_dir = QtWidgets.QFileDialog.getExistingDirectory(
50+
self.parent(), "Select output directory", "")
51+
if out_dir:
52+
out_dir = pathlib.Path(out_dir)
53+
# Determine the common input path of all files
54+
# TODO:
55+
# This will not work with files on different drives under
56+
# Windows.
57+
pc = str(self.file_list[0])
58+
for pp in self.file_list[1:]:
59+
pc = "".join(
60+
[a for (a, b) in zip(str(pp[:len(pc)]), pc) if a == b])
61+
pc = pathlib.Path(pc)
62+
if not pc.exists():
63+
# There is probably just a piece of a file name stem left
64+
pc = pc.parent
65+
assert pc.exists()
66+
# Create a output path list
67+
out_list = []
68+
for pp in self.file_list:
69+
pp = pathlib.Path(pp)
70+
relp = pp.relative_to(pc)
71+
outp = (out_dir / relp).with_suffix(".h5")
72+
if outp in out_list:
73+
ii = 2
74+
while True:
75+
new = outp.with_name(f"{outp.stem}-{ii}.h5")
76+
if new not in out_list:
77+
outp = new
78+
break
79+
ii += 1
80+
out_list.append(outp)
81+
# Perform the export
82+
for pin, pout in zip(self.file_list, out_list):
83+
pout.parent.mkdir(parents=True, exist_ok=True)
84+
with h5py.File(pout, mode="w") as h5:
85+
fdlist = afmformats.load_data(pin)
86+
for fdist in fdlist:
87+
fdist.export(h5,
88+
metadata=self.get_metadata_keys(fdist),
89+
fmt="hdf5")
90+
return True
91+
else:
92+
return False
93+
4694
def _convert_curve(self):
95+
"""Output all files into one directory, one file per curve"""
4796
out_dir = QtWidgets.QFileDialog.getExistingDirectory(
4897
self.parent(), "Select output directory", "")
4998
if out_dir:
@@ -64,6 +113,7 @@ def _convert_curve(self):
64113
return False # do not close the dialog
65114

66115
def _convert_unaltered(self):
116+
"""Output all files into one directory (HDF5 only)"""
67117
out_dir = QtWidgets.QFileDialog.getExistingDirectory(
68118
self.parent(), "Select output directory", "")
69119
if out_dir:
@@ -109,6 +159,8 @@ def convert(self):
109159
return self._convert_unaltered()
110160
elif self.radioButton_merge.isChecked():
111161
return self._convert_merge()
162+
elif self.radioButton_mirror.isChecked():
163+
return self._convert_mirror()
112164
else:
113165
return self._convert_curve()
114166

pyjibe/head/dlg_tool_convert.ui

Lines changed: 25 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@
66
<rect>
77
<x>0</x>
88
<y>0</y>
9-
<width>532</width>
10-
<height>477</height>
9+
<width>536</width>
10+
<height>535</height>
1111
</rect>
1212
</property>
1313
<property name="acceptDrops">
@@ -70,7 +70,7 @@
7070
<property name="topMargin">
7171
<number>0</number>
7272
</property>
73-
<item row="1" column="0">
73+
<item row="1" column="0" alignment="Qt::AlignTop">
7474
<widget class="QLabel" name="label_3">
7575
<property name="text">
7676
<string>File grouping:</string>
@@ -79,7 +79,7 @@
7979
</item>
8080
<item row="1" column="1">
8181
<widget class="QWidget" name="widget_group" native="true">
82-
<layout class="QHBoxLayout" name="horizontalLayout_2">
82+
<layout class="QVBoxLayout" name="verticalLayout_2">
8383
<property name="leftMargin">
8484
<number>0</number>
8585
</property>
@@ -92,36 +92,46 @@
9292
<property name="bottomMargin">
9393
<number>0</number>
9494
</property>
95+
<item>
96+
<widget class="QRadioButton" name="radioButton_mirror">
97+
<property name="text">
98+
<string>mirror original file names and directory structure</string>
99+
</property>
100+
<property name="checked">
101+
<bool>true</bool>
102+
</property>
103+
</widget>
104+
</item>
95105
<item>
96106
<widget class="QRadioButton" name="radioButton_unaltered">
97107
<property name="toolTip">
98108
<string>Each input file is converted to one output file</string>
99109
</property>
100110
<property name="text">
101-
<string>unaltered</string>
111+
<string>save all files into one directory</string>
102112
</property>
103113
<property name="checked">
104-
<bool>true</bool>
114+
<bool>false</bool>
105115
</property>
106116
</widget>
107117
</item>
108118
<item>
109-
<widget class="QRadioButton" name="radioButton_merge">
119+
<widget class="QRadioButton" name="radioButton_onefile">
110120
<property name="toolTip">
111-
<string>All input data are merged into one output file</string>
121+
<string>Input files with more than one dataset produce more output files</string>
112122
</property>
113123
<property name="text">
114-
<string>merge all</string>
124+
<string>save all files into one directory, one file per curve</string>
115125
</property>
116126
</widget>
117127
</item>
118128
<item>
119-
<widget class="QRadioButton" name="radioButton_onefile">
129+
<widget class="QRadioButton" name="radioButton_merge">
120130
<property name="toolTip">
121-
<string>Input files with more than one dataset produce more output files</string>
131+
<string>All input data are merged into one output file</string>
122132
</property>
123133
<property name="text">
124-
<string>one file per curve</string>
134+
<string>merge all files into one HDF5 file</string>
125135
</property>
126136
</widget>
127137
</item>
@@ -279,16 +289,16 @@
279289
<connection>
280290
<sender>radioButton_hdf5</sender>
281291
<signal>toggled(bool)</signal>
282-
<receiver>radioButton_unaltered</receiver>
292+
<receiver>radioButton_mirror</receiver>
283293
<slot>setChecked(bool)</slot>
284294
<hints>
285295
<hint type="sourcelabel">
286296
<x>375</x>
287297
<y>366</y>
288298
</hint>
289299
<hint type="destinationlabel">
290-
<x>166</x>
291-
<y>395</y>
300+
<x>299</x>
301+
<y>366</y>
292302
</hint>
293303
</hints>
294304
</connection>

0 commit comments

Comments
 (0)