-
Notifications
You must be signed in to change notification settings - Fork 1
/
Dataformat.py
335 lines (284 loc) · 11.5 KB
/
Dataformat.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
# -*- coding: utf-8 -*-
##################################
# Reduction panel
##################################
__authors__ = ["Shun Yu"]
__license__ = "MIT"
__date__ = "23/04/2020"
import logging
import sys
import tempfile
import numpy
import h5py
import os
import fabio
import pyFAI
import silx
import glob
import re
import json
#from pathlib import Path, PureWindowsPath
#from pyFAI.app import calib2
logging.basicConfig()
_logger = logging.getLogger("customHdf5TreeModel")
"""Module logger"""
from silx.gui import qt
from silx.gui import icons
import silx.gui.hdf5
from silx.gui.data.DataViewerFrame import DataViewerFrame
from silx.gui.data.DataViewer import DataViewer
from silx.gui.plot.StackView import StackViewMainWindow
from silx.gui.plot.PlotWindow import PlotWindow
from silx.gui.widgets.ThreadPoolPushButton import ThreadPoolPushButton
from silx.gui.widgets.WaitingPushButton import WaitingPushButton
from silx.gui.hdf5.Hdf5TreeModel import Hdf5TreeModel
from silx.gui.widgets.TableWidget import TableWidget
from silx.io.nxdata import save_NXdata
class inputdialogdemo(qt.QDialog):
def __init__(self, parent = None):
super(inputdialogdemo, self).__init__(parent)
self.datapath = []
self.rownumber = []
self.columnumber = []
self.savefilename = []
layout_main = qt.QVBoxLayout()
self.setLayout(layout_main)
layout_select_file = qt.QFormLayout()
self.btn = qt.QPushButton("Choose from list")
layout_select_file.addRow(qt.QLabel("Select data for the model structure"), self.btn)
self.btn.clicked.connect(self.select_data)
layout_main.addLayout(layout_select_file)
layout_handling = qt.QHBoxLayout()
self.treeview = silx.gui.hdf5.Hdf5TreeView(self)
self.treeview.activated.connect(self.assignpath)
layout_handling.addWidget(self.treeview)
layout_button = qt.QVBoxLayout()
self.extractbutton = qt.QPushButton("Extract")
self.extractbutton.clicked.connect(self.extractpath)
layout_button.addWidget(self.extractbutton)
self.withdrawbutton = qt.QPushButton("Withdraw")
self.withdrawbutton.clicked.connect(self.withdrawpath)
layout_button.addWidget(self.withdrawbutton)
layout_handling.addLayout(layout_button)
self.table = TableWidget(self)
self.table.setColumnCount(2)
self.table.setRowCount(15)
self.table.cellClicked.connect(self.getcell)
self.init_table()
layout_handling.addWidget(self.table)
layout_save = qt.QHBoxLayout()
layout_save.setAlignment(qt.Qt.AlignRight)
self.btnreset = qt.QPushButton("Reset")
self.btnreset.clicked.connect(lambda: self.init_table())
self.btnedit = qt.QPushButton("Edit")
self.btnedit.clicked.connect(lambda: self.editfile())
self.btnsave = qt.QPushButton("Save")
self.btnsave.clicked.connect(lambda: self.savefile())
self.btnclose = qt.QPushButton("Close")
self.btnclose.clicked.connect(lambda: self.close())
layout_main.addLayout(layout_handling)
layout_save.addWidget(self.btnreset)
layout_save.addWidget(self.btnedit)
layout_save.addWidget(self.btnsave)
layout_save.addWidget(self.btnclose)
layout_main.addLayout(layout_save)
self.setWindowTitle("Add new Data format")
self.exec_()
def select_data(self):
file = qt.QFileDialog.getOpenFileName(self, 'Choose Model Data Structure')
if file != ('', ''):
print(file[0])
self.treeview.findHdf5TreeModel().appendFile(file[0])
#self.ExtractDataformat(file[0])
def init_table(self):
format = {
"saxs_det": "entry/data/data",
"saxs_master": "",
"waxs_det": "entry/data/data",
"waxs_master":"",
"I_0": "",
"I_t": "",
"dt": "",
"Motor_x": "",
"Motor_y": "",
"Motor_z": "",
"Rot": "",
"T": ""
}
self.readdict(format)
'''
for count, dict in enumerate(zip(format.keys(), format.values())):
key_name = qt.QTableWidgetItem(dict[0])
path = qt.QTableWidgetItem(dict[1])
self.table.setItem(count, 0, key_name)
self.table.setItem(count, 1, path)
'''
def assignpath(self):
selected = list(self.treeview.selectedH5Nodes())
#print(selected[0])
if len(selected) == 1:
# Update the viewer for a single selection
self.datapath = selected[0].data_url.data_path()[1:]
print(self.datapath)
def extractpath(self):
try:
setpath = qt.QTableWidgetItem(self.datapath)
print(self.rownumber)
self.table.setItem(self.rownumber, self.columnumber, setpath)
except:
print("Assignment is failed!!")
def withdrawpath(self):
try:
self.table.setItem(self.rownumber, self.columnumber, None)
except:
print("withdraw is failed!!")
def getcell(self):
self.rownumber = self.table.currentRow()
self.columnumber = self.table.currentColumn()
def savefile(self):
name = qt.QFileDialog.getSaveFileName(self, 'Save Data Format', 'Data format/', "txt files (*.txt)")
data = self.convertdict()
if name != ('', ''):
with open(name[0],'w') as file:
file.write(json.dumps(data))
self.savefilename = name[0]
else:
self.savefilename = []
print(self.savefilename)
def convertdict(self):
dict = {}
n = 0
while self.table.item(n, 0):
dict.update({self.table.item(n, 0).text(): self.table.item(n, 1).text()})
n += 1
print(dict)
return dict
def editfile(self):
file = qt.QFileDialog.getOpenFileName(self, 'Choose Model Data Structure', 'Data format/', "txt files (*.txt)")
if file != ('', ''):
with open(file[0],'r') as f:
format = json.loads(f.read())
self.readdict(format)
def readdict(self, format):
for count, dict in enumerate(zip(format.keys(), format.values())):
key_name = qt.QTableWidgetItem(dict[0])
path = qt.QTableWidgetItem(dict[1])
self.table.setItem(count, 0, key_name)
self.table.setItem(count, 1, path)
'''
def ExtractDataformat(self, obj):
with h5py.File(obj,'r') as h5:
full_file_keys = []
Data_keys = []
h5.visit(full_file_keys.append)
for h5_key in full_file_keys:
if isinstance(h5[h5_key], h5py.Dataset):
#if isinstance(h5[h5_key], h5py.Group):
#tmp = np.array(h5obj[h5_key])[mask]
# There is no way to simply change the dataset because its
# shape is fixed, causing a broadcast error, so it is
# necessary to delete and then recreate it.
#del h5obj[h5_key]
#h5obj.create_dataset(h5_key, data=tmp)
Data_keys.append(h5_key)
print(Data_keys)
self.Data_keys = Data_keys
del full_file_keys, Data_keys
#tmp = h5_key.split('/')
#print(tmp[-1])
def ExtractDataformat(self, event):
for obj in event:
if obj.ntype == h5py.File:
full_file_keys = []
Data_keys = []
h5 = obj.h5py_object
h5.visit(full_file_keys.append)
for h5_key in full_file_keys:
if isinstance(h5[h5_key], h5py.Dataset):
#if isinstance(h5[h5_key], h5py.Group):
#tmp = np.array(h5obj[h5_key])[mask]
# There is no way to simply change the dataset because its
# shape is fixed, causing a broadcast error, so it is
# necessary to delete and then recreate it.
#del h5obj[h5_key]
#h5obj.create_dataset(h5_key, data=tmp)
Data_keys.append(h5_key)
print(Data_keys)
self.Data_keys = Data_keys
del full_file_keys, Data_keys
#tmp = h5_key.split('/')
#print(tmp[-1])
'''
class D_format(qt.QComboBox):
def __init__(self, parent=None):
super(D_format, self).__init__(parent)
#layout = qt.QHBoxLayout()
#layout.addStretch(1)
#self.dropbox = qt.QComboBox()
self.loadfile()
#layout.addWidget(qt.QLabel("File Format"))
#layout.addWidget(self.dropbox)
#self.setLayout(layout)
self.dataformat = {
"saxs_det": "",
"saxs_master": "",
"waxs_det": "",
"waxs_master":"",
"I_0": "",
"I_t": "",
"dt": "",
"Motor_x": "",
"Motor_y": "",
"Motor_z": "",
"Rot": "",
"T": ""
}
self.setdata("MAX IV - CoSAXS")
#self.dropbox.currentIndexChanged.connect(self.onchange)
self.activated.connect(self.onchange)
def setdata(self, filename):
self.setCurrentText(filename)
self.dataformat = self.readfile(self.currentText())
print(self.currentText() + " is selected")
'''
if self.currentText() == "MAX IV - CoSAXS":
self.dataformat = self.readfile(self.currentText())
#print(self.dataformat)
print(self.currentText() + " is selected")
#self.output()
else:
self.onchange()
'''
def onchange(self):
if self.currentText() != "User defined":
self.dataformat = self.readfile(self.currentText())
print(self.currentText() + " is selected")
#self.output(self.dataformat)
#print(self.dropbox.currentText())
#print(self.dataformat)
else:
self.userdefine()
def userdefine(self):
print("you're in charge")
inputfile = inputdialogdemo()
self.clear()
self.loadfile()
if inputfile.savefilename != []:
print(inputfile.savefilename)
filename = os.path.splitext(os.path.basename(inputfile.savefilename))[0]
print(filename)
self.setdata(filename)
#self.output(self.dataformat)
def readfile(self, filename):
with open('Data format/'+filename+'.txt') as f:
#return dict(re.findall(r'(\S+)\s+(.+)', f.read()))
#return dict(line.strip().split(1) for line in f)
#return ast.literal_eval(f.read())
return json.loads(f.read())
def loadfile(self):
for file in glob.glob("Data format/*"):
fileName = os.path.splitext(os.path.basename(file))[0]
self.addItem(fileName)
self.addItem("User defined")
def output(self, data):
return data