[77d92cd] | 1 | """ |
---|
| 2 | This module provides a GUI for the file converter |
---|
| 3 | """ |
---|
| 4 | |
---|
| 5 | import wx |
---|
| 6 | import sys |
---|
[a58706d] | 7 | import numpy as np |
---|
[77d92cd] | 8 | from wx.lib.scrolledpanel import ScrolledPanel |
---|
| 9 | from sas.sasgui.guiframe.panel_base import PanelBase |
---|
| 10 | from sas.sasgui.perspectives.calculator import calculator_widgets as widget |
---|
[c9a519f] | 11 | from sas.sasgui.perspectives.file_converter.converter_widgets import VectorInput |
---|
[de0df2c] | 12 | from sas.sasgui.perspectives.file_converter.meta_panels import MetadataWindow |
---|
| 13 | from sas.sasgui.perspectives.file_converter.meta_panels import DetectorPanel |
---|
[2a7722b] | 14 | from sas.sasgui.perspectives.file_converter.meta_panels import SamplePanel |
---|
[a58706d] | 15 | from sas.sasgui.guiframe.events import StatusEvent |
---|
| 16 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
[fdbea3c] | 17 | from sas.sasgui.guiframe.utils import check_float |
---|
[a58706d] | 18 | from sas.sascalc.dataloader.readers.cansas_reader import Reader as CansasReader |
---|
[4b862c4] | 19 | from sas.sasgui.perspectives.file_converter.bsl_loader import BSLLoader |
---|
[f2b3f28] | 20 | from sas.sascalc.dataloader.data_info import Detector |
---|
[2a7722b] | 21 | from sas.sascalc.dataloader.data_info import Sample |
---|
[fdbea3c] | 22 | from sas.sascalc.dataloader.data_info import Vector |
---|
[77d92cd] | 23 | |
---|
| 24 | # Panel size |
---|
| 25 | if sys.platform.count("win32") > 0: |
---|
| 26 | PANEL_TOP = 0 |
---|
| 27 | _STATICBOX_WIDTH = 410 |
---|
| 28 | _BOX_WIDTH = 200 |
---|
[36f4debb] | 29 | PANEL_SIZE = 480 |
---|
[77d92cd] | 30 | FONT_VARIANT = 0 |
---|
| 31 | else: |
---|
| 32 | PANEL_TOP = 60 |
---|
| 33 | _STATICBOX_WIDTH = 430 |
---|
| 34 | _BOX_WIDTH = 200 |
---|
[36f4debb] | 35 | PANEL_SIZE = 500 |
---|
[77d92cd] | 36 | FONT_VARIANT = 1 |
---|
| 37 | |
---|
| 38 | class ConverterPanel(ScrolledPanel, PanelBase): |
---|
| 39 | |
---|
| 40 | def __init__(self, parent, base=None, *args, **kwargs): |
---|
| 41 | ScrolledPanel.__init__(self, parent, *args, **kwargs) |
---|
| 42 | PanelBase.__init__(self) |
---|
[a58706d] | 43 | self.SetupScrolling() |
---|
[77d92cd] | 44 | self.SetWindowVariant(variant=FONT_VARIANT) |
---|
| 45 | |
---|
| 46 | self.base = base |
---|
| 47 | self.parent = parent |
---|
[de0df2c] | 48 | self.meta_frames = [] |
---|
[77d92cd] | 49 | |
---|
[a58706d] | 50 | self.q_input = None |
---|
| 51 | self.iq_input = None |
---|
| 52 | self.output = None |
---|
[4b862c4] | 53 | self.data_type = "ascii" |
---|
[a58706d] | 54 | |
---|
[f2b3f28] | 55 | self.metadata = { |
---|
| 56 | 'title': None, |
---|
| 57 | 'run': None, |
---|
| 58 | 'run_name': None, |
---|
| 59 | 'instrument': None, |
---|
[2a7722b] | 60 | 'detector': [Detector()], |
---|
| 61 | 'sample': Sample() |
---|
[f2b3f28] | 62 | } |
---|
| 63 | |
---|
[4b862c4] | 64 | self.metadata['detector'][0].name = '' |
---|
| 65 | |
---|
[a58706d] | 66 | self._do_layout() |
---|
[77d92cd] | 67 | self.SetAutoLayout(True) |
---|
| 68 | self.Layout() |
---|
| 69 | |
---|
[a58706d] | 70 | def convert_to_cansas(self, data, filename): |
---|
| 71 | reader = CansasReader() |
---|
| 72 | reader.write(filename, data) |
---|
| 73 | |
---|
| 74 | def extract_data(self, filename): |
---|
| 75 | data = np.loadtxt(filename, dtype=str) |
---|
| 76 | |
---|
[ff790b3] | 77 | if len(data.shape) != 1: |
---|
| 78 | msg = "Error reading {}: Only one column of data is allowed" |
---|
| 79 | raise Exception(msg.format(filename.split('\\')[-1])) |
---|
| 80 | |
---|
[a58706d] | 81 | is_float = True |
---|
| 82 | try: |
---|
| 83 | float(data[0]) |
---|
| 84 | except: |
---|
| 85 | is_float = False |
---|
| 86 | |
---|
| 87 | if not is_float: |
---|
| 88 | end_char = data[0][-1] |
---|
| 89 | # If lines end with comma or semi-colon, trim the last character |
---|
| 90 | if end_char == ',' or end_char == ';': |
---|
| 91 | data = map(lambda s: s[0:-1], data) |
---|
| 92 | else: |
---|
| 93 | msg = ("Error reading {}: Lines must end with a digit, comma " |
---|
| 94 | "or semi-colon").format(filename.split('\\')[-1]) |
---|
| 95 | raise Exception(msg) |
---|
| 96 | |
---|
| 97 | return np.array(data, dtype=np.float32) |
---|
| 98 | |
---|
| 99 | def on_convert(self, event): |
---|
[fdbea3c] | 100 | if not self.validate_inputs(): |
---|
| 101 | return |
---|
| 102 | |
---|
[a58706d] | 103 | try: |
---|
[4b862c4] | 104 | if self.data_type == 'ascii': |
---|
| 105 | qdata = self.extract_data(self.q_input.GetPath()) |
---|
| 106 | iqdata = self.extract_data(self.iq_input.GetPath()) |
---|
| 107 | else: # self.data_type == 'bsl' |
---|
| 108 | loader = BSLLoader(self.q_input.GetPath(), |
---|
| 109 | self.iq_input.GetPath()) |
---|
| 110 | bsl_data = loader.load_bsl_data() |
---|
| 111 | qdata = bsl_data.q_axis.data[0] |
---|
| 112 | iqdata = bsl_data.data_axis.data[0] |
---|
[a58706d] | 113 | except Exception as ex: |
---|
| 114 | msg = str(ex) |
---|
| 115 | wx.PostEvent(self.parent.manager.parent, |
---|
| 116 | StatusEvent(status=msg, info='error')) |
---|
| 117 | return |
---|
| 118 | |
---|
[f2b3f28] | 119 | output_path = self.output.GetPath() |
---|
[a58706d] | 120 | data = Data1D(x=qdata, y=iqdata) |
---|
[f2b3f28] | 121 | data.filename = output_path.split('\\')[-1] |
---|
| 122 | |
---|
| 123 | if self.metadata['run'] is not None: |
---|
| 124 | run = self.metadata['run'] |
---|
| 125 | run_name = self.metadata['run_name'] |
---|
[3331e11] | 126 | |
---|
| 127 | if not isinstance(run, list) and run is not None: |
---|
| 128 | self.metadata['run'] = [run] |
---|
| 129 | else: |
---|
| 130 | run = run[0] |
---|
| 131 | |
---|
| 132 | if not isinstance(run_name, dict): |
---|
| 133 | if run_name is not None: |
---|
| 134 | self.metadata['run_name'] = { run: run_name } |
---|
| 135 | else: |
---|
| 136 | self.metadata['run_name'] = {} |
---|
[9318c4e] | 137 | elif run_name != {}: |
---|
[3331e11] | 138 | self.metadata['run_name'][run] = run_name.values()[0] |
---|
[fdbea3c] | 139 | else: |
---|
| 140 | self.metadata['run'] = [] |
---|
| 141 | self.metadata['run_name'] = {} |
---|
[f2b3f28] | 142 | |
---|
| 143 | for attr, value in self.metadata.iteritems(): |
---|
[fdbea3c] | 144 | if value is not None: |
---|
| 145 | setattr(data, attr, value) |
---|
[f2b3f28] | 146 | |
---|
| 147 | self.convert_to_cansas(data, output_path) |
---|
[a58706d] | 148 | wx.PostEvent(self.parent.manager.parent, |
---|
| 149 | StatusEvent(status="Conversion completed.")) |
---|
| 150 | |
---|
[fdbea3c] | 151 | def validate_inputs(self): |
---|
| 152 | msg = "You must select a" |
---|
| 153 | if self.q_input.GetPath() == '': |
---|
| 154 | msg += " Q Axis input file." |
---|
| 155 | elif self.iq_input.GetPath() == '': |
---|
| 156 | msg += "n Intensity input file." |
---|
| 157 | elif self.output.GetPath() == '': |
---|
| 158 | msg += "destination for the converted file." |
---|
| 159 | if msg != "You must select a": |
---|
| 160 | wx.PostEvent(self.parent.manager.parent, |
---|
| 161 | StatusEvent(status=msg, info='error')) |
---|
| 162 | return |
---|
| 163 | |
---|
| 164 | return True |
---|
| 165 | |
---|
[af84162] | 166 | def show_detector_window(self, event): |
---|
[de0df2c] | 167 | if self.meta_frames != []: |
---|
| 168 | for frame in self.meta_frames: |
---|
| 169 | frame.panel.on_close() |
---|
| 170 | detector_frame = MetadataWindow(DetectorPanel, |
---|
| 171 | parent=self.parent.manager.parent, manager=self, |
---|
| 172 | metadata=self.metadata['detector'][0], title='Detector Metadata') |
---|
| 173 | self.meta_frames.append(detector_frame) |
---|
| 174 | self.parent.manager.put_icon(detector_frame) |
---|
| 175 | detector_frame.Show(True) |
---|
[fdbea3c] | 176 | |
---|
[2a7722b] | 177 | def show_sample_window(self, event): |
---|
| 178 | if self.meta_frames != []: |
---|
| 179 | for frame in self.meta_frames: |
---|
| 180 | frame.panel.on_close() |
---|
| 181 | sample_frame = MetadataWindow(SamplePanel, |
---|
| 182 | parent=self.parent.manager.parent, manager=self, |
---|
| 183 | metadata=self.metadata['sample'], title='Sample Metadata') |
---|
| 184 | self.meta_frames.append(sample_frame) |
---|
| 185 | self.parent.manager.put_icon(sample_frame) |
---|
| 186 | sample_frame.Show(True) |
---|
| 187 | |
---|
[a027549] | 188 | def on_collapsible_pane(self, event): |
---|
| 189 | self.Freeze() |
---|
| 190 | self.SetupScrolling() |
---|
| 191 | self.parent.Layout() |
---|
| 192 | self.Thaw() |
---|
| 193 | |
---|
[4b862c4] | 194 | def datatype_changed(self, event): |
---|
| 195 | event.Skip() |
---|
| 196 | dtype = event.GetEventObject().GetName() |
---|
| 197 | self.data_type = dtype |
---|
| 198 | |
---|
[f2b3f28] | 199 | def metadata_changed(self, event): |
---|
| 200 | event.Skip() |
---|
| 201 | textbox = event.GetEventObject() |
---|
| 202 | attr = textbox.GetName() |
---|
| 203 | value = textbox.GetValue().strip() |
---|
[fdbea3c] | 204 | |
---|
[f2b3f28] | 205 | if value == '': |
---|
| 206 | self.metadata[attr] = None |
---|
| 207 | else: |
---|
| 208 | self.metadata[attr] = value |
---|
| 209 | |
---|
| 210 | |
---|
[77d92cd] | 211 | def _do_layout(self): |
---|
[f2b3f28] | 212 | vbox = wx.BoxSizer(wx.VERTICAL) |
---|
[a58706d] | 213 | |
---|
[4b862c4] | 214 | instructions = ("Select either 1 column ASCII files or BSL files " |
---|
| 215 | "containing the Q Axis and Intensity data, chose where to save " |
---|
| 216 | "the converted file, then click Convert to convert them to CanSAS " |
---|
| 217 | "XML format. If required, metadata can also be input below.") |
---|
[5afe77f] | 218 | instruction_label = wx.StaticText(self, -1, instructions, |
---|
| 219 | size=(_STATICBOX_WIDTH+40, -1)) |
---|
| 220 | instruction_label.Wrap(_STATICBOX_WIDTH+40) |
---|
[f2b3f28] | 221 | vbox.Add(instruction_label, flag=wx.TOP | wx.LEFT | wx.RIGHT, border=5) |
---|
[77d92cd] | 222 | |
---|
[a58706d] | 223 | section = wx.StaticBox(self, -1) |
---|
| 224 | section_sizer = wx.StaticBoxSizer(section, wx.VERTICAL) |
---|
| 225 | section_sizer.SetMinSize((_STATICBOX_WIDTH, -1)) |
---|
[77d92cd] | 226 | |
---|
| 227 | input_grid = wx.GridBagSizer(5, 5) |
---|
| 228 | |
---|
[4b862c4] | 229 | y = 0 |
---|
| 230 | |
---|
[a58706d] | 231 | q_label = wx.StaticText(self, -1, "Q Axis: ") |
---|
[4b862c4] | 232 | input_grid.Add(q_label, (y,0), (1,1), wx.ALIGN_CENTER_VERTICAL, 5) |
---|
[a58706d] | 233 | |
---|
| 234 | self.q_input = wx.FilePickerCtrl(self, -1, |
---|
| 235 | size=(_STATICBOX_WIDTH-80, -1), |
---|
| 236 | message="Chose the Q Axis data file.") |
---|
[4b862c4] | 237 | input_grid.Add(self.q_input, (y,1), (1,1), wx.ALL, 5) |
---|
| 238 | y += 1 |
---|
[a58706d] | 239 | |
---|
| 240 | iq_label = wx.StaticText(self, -1, "Intensity Data: ") |
---|
[4b862c4] | 241 | input_grid.Add(iq_label, (y,0), (1,1), wx.ALIGN_CENTER_VERTICAL, 5) |
---|
[a58706d] | 242 | |
---|
| 243 | self.iq_input = wx.FilePickerCtrl(self, -1, |
---|
| 244 | size=(_STATICBOX_WIDTH-80, -1), |
---|
| 245 | message="Chose the Intensity data file.") |
---|
[4b862c4] | 246 | input_grid.Add(self.iq_input, (y,1), (1,1), wx.ALL, 5) |
---|
| 247 | y += 1 |
---|
| 248 | |
---|
| 249 | data_type_label = wx.StaticText(self, -1, "Input Format: ") |
---|
| 250 | input_grid.Add(data_type_label, (y,0), (1,1), |
---|
| 251 | wx.ALIGN_CENTER_VERTICAL, 5) |
---|
| 252 | radio_sizer = wx.BoxSizer(wx.HORIZONTAL) |
---|
| 253 | ascii_btn = wx.RadioButton(self, -1, "ASCII", name="ascii", |
---|
| 254 | style=wx.RB_GROUP) |
---|
| 255 | ascii_btn.Bind(wx.EVT_RADIOBUTTON, self.datatype_changed) |
---|
| 256 | radio_sizer.Add(ascii_btn) |
---|
| 257 | bsl_btn = wx.RadioButton(self, -1, "BSL", name="bsl") |
---|
| 258 | bsl_btn.Bind(wx.EVT_RADIOBUTTON, self.datatype_changed) |
---|
| 259 | radio_sizer.Add(bsl_btn) |
---|
| 260 | input_grid.Add(radio_sizer, (y,1), (1,1), wx.ALL, 5) |
---|
| 261 | y += 1 |
---|
[a58706d] | 262 | |
---|
| 263 | output_label = wx.StaticText(self, -1, "Output File: ") |
---|
[4b862c4] | 264 | input_grid.Add(output_label, (y,0), (1,1), wx.ALIGN_CENTER_VERTICAL, 5) |
---|
[a58706d] | 265 | |
---|
| 266 | self.output = wx.FilePickerCtrl(self, -1, |
---|
| 267 | size=(_STATICBOX_WIDTH-80, -1), |
---|
| 268 | message="Chose the Intensity data file.", |
---|
| 269 | style=wx.FLP_SAVE | wx.FLP_OVERWRITE_PROMPT | wx.FLP_USE_TEXTCTRL, |
---|
| 270 | wildcard="*.xml") |
---|
[4b862c4] | 271 | input_grid.Add(self.output, (y,1), (1,1), wx.ALL, 5) |
---|
| 272 | y += 1 |
---|
[a58706d] | 273 | |
---|
[fdbea3c] | 274 | convert_btn = wx.Button(self, wx.ID_OK, "Convert") |
---|
[4b862c4] | 275 | input_grid.Add(convert_btn, (y,0), (1,1), wx.ALL, 5) |
---|
[a58706d] | 276 | convert_btn.Bind(wx.EVT_BUTTON, self.on_convert) |
---|
[77d92cd] | 277 | |
---|
[a58706d] | 278 | section_sizer.Add(input_grid) |
---|
[77d92cd] | 279 | |
---|
[f2b3f28] | 280 | vbox.Add(section_sizer, flag=wx.ALL, border=5) |
---|
| 281 | |
---|
| 282 | metadata_section = wx.CollapsiblePane(self, -1, "Metadata", |
---|
[fdbea3c] | 283 | size=(_STATICBOX_WIDTH+40, -1), style=wx.WS_EX_VALIDATE_RECURSIVELY) |
---|
| 284 | metadata_pane = metadata_section.GetPane() |
---|
[f2b3f28] | 285 | metadata_grid = wx.GridBagSizer(5, 5) |
---|
| 286 | |
---|
[a027549] | 287 | metadata_section.Bind(wx.EVT_COLLAPSIBLEPANE_CHANGED, |
---|
| 288 | self.on_collapsible_pane) |
---|
| 289 | |
---|
[f2b3f28] | 290 | y = 0 |
---|
| 291 | for item in self.metadata.keys(): |
---|
[9318c4e] | 292 | if item == 'detector' or item == 'sample': continue |
---|
[36f4debb] | 293 | label_txt = item.replace('_', ' ').capitalize() |
---|
[fdbea3c] | 294 | label = wx.StaticText(metadata_pane, -1, label_txt, |
---|
[f2b3f28] | 295 | style=wx.ALIGN_CENTER_VERTICAL) |
---|
[fdbea3c] | 296 | input_box = wx.TextCtrl(metadata_pane, name=item, |
---|
[f2b3f28] | 297 | size=(_STATICBOX_WIDTH-80, -1)) |
---|
| 298 | input_box.Bind(wx.EVT_TEXT, self.metadata_changed) |
---|
| 299 | metadata_grid.Add(label, (y,0), (1,1), |
---|
| 300 | wx.ALL | wx.ALIGN_CENTER_VERTICAL, 5) |
---|
[fdbea3c] | 301 | metadata_grid.Add(input_box, (y,1), (1,2), wx.EXPAND) |
---|
[f2b3f28] | 302 | y += 1 |
---|
| 303 | |
---|
[fdbea3c] | 304 | detector_label = wx.StaticText(metadata_pane, -1, |
---|
| 305 | "Detector:") |
---|
| 306 | metadata_grid.Add(detector_label, (y, 0), (1,1), wx.ALL | wx.EXPAND, 5) |
---|
[af84162] | 307 | detector_btn = wx.Button(metadata_pane, -1, "Enter Detector Metadata") |
---|
| 308 | metadata_grid.Add(detector_btn, (y, 1), (1,1), wx.ALL | wx.EXPAND, 5) |
---|
| 309 | detector_btn.Bind(wx.EVT_BUTTON, self.show_detector_window) |
---|
[fdbea3c] | 310 | y += 1 |
---|
| 311 | |
---|
[2a7722b] | 312 | sample_label = wx.StaticText(metadata_pane, -1, "Sample: ") |
---|
| 313 | metadata_grid.Add(sample_label, (y,0), (1,1), wx.ALL | wx.EXPAND, 5) |
---|
| 314 | sample_btn = wx.Button(metadata_pane, -1, "Enter Sample Metadata") |
---|
| 315 | metadata_grid.Add(sample_btn, (y,1), (1,1), wx.ALL | wx.EXPAND, 5) |
---|
| 316 | sample_btn.Bind(wx.EVT_BUTTON, self.show_sample_window) |
---|
| 317 | y += 1 |
---|
| 318 | |
---|
[fdbea3c] | 319 | metadata_pane.SetSizer(metadata_grid) |
---|
[f2b3f28] | 320 | |
---|
| 321 | vbox.Add(metadata_section, proportion=0, flag=wx.ALL, border=5) |
---|
[77d92cd] | 322 | |
---|
| 323 | vbox.Fit(self) |
---|
| 324 | self.SetSizer(vbox) |
---|
| 325 | |
---|
| 326 | class ConverterWindow(widget.CHILD_FRAME): |
---|
| 327 | |
---|
| 328 | def __init__(self, parent=None, title='File Converter', base=None, |
---|
[a027549] | 329 | manager=None, size=(PANEL_SIZE * 1.05, PANEL_SIZE / 1.25), |
---|
[77d92cd] | 330 | *args, **kwargs): |
---|
| 331 | kwargs['title'] = title |
---|
| 332 | kwargs['size'] = size |
---|
| 333 | widget.CHILD_FRAME.__init__(self, parent, *args, **kwargs) |
---|
| 334 | |
---|
| 335 | self.manager = manager |
---|
| 336 | self.panel = ConverterPanel(self, base=None) |
---|
| 337 | self.Bind(wx.EVT_CLOSE, self.on_close) |
---|
| 338 | self.SetPosition((wx.LEFT, PANEL_TOP)) |
---|
| 339 | self.Show(True) |
---|
| 340 | |
---|
| 341 | def on_close(self, event): |
---|
| 342 | if self.manager is not None: |
---|
| 343 | self.manager.converter_frame = None |
---|
| 344 | self.Destroy() |
---|