[f721030] | 1 | """ |
---|
[28a84e9] | 2 | Global defaults and various utility functions usable by the general GUI |
---|
[f721030] | 3 | """ |
---|
| 4 | |
---|
| 5 | import os |
---|
[dc5ef15] | 6 | import re |
---|
[f721030] | 7 | import sys |
---|
| 8 | import imp |
---|
| 9 | import warnings |
---|
[f82ab8c] | 10 | import webbrowser |
---|
[b3e8629] | 11 | import urllib.parse |
---|
[f82ab8c] | 12 | |
---|
[3b8cc00] | 13 | import numpy as np |
---|
| 14 | |
---|
[f721030] | 15 | warnings.simplefilter("ignore") |
---|
| 16 | import logging |
---|
| 17 | |
---|
[4992ff2] | 18 | from PyQt5 import QtCore |
---|
| 19 | from PyQt5 import QtGui |
---|
| 20 | from PyQt5 import QtWidgets |
---|
[f721030] | 21 | |
---|
[e4676c8] | 22 | from periodictable import formula as Formula |
---|
[dc5ef15] | 23 | from sas.qtgui.Plotting import DataTransform |
---|
| 24 | from sas.qtgui.Plotting.ConvertUnits import convertUnit |
---|
| 25 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
| 26 | from sas.qtgui.Plotting.PlotterData import Data2D |
---|
[28a84e9] | 27 | from sas.sascalc.dataloader.loader import Loader |
---|
[dc5ef15] | 28 | from sas.qtgui.Utilities import CustomDir |
---|
[f721030] | 29 | |
---|
[fef38e8] | 30 | ## TODO: CHANGE FOR SHIPPED PATH IN RELEASE |
---|
| 31 | if os.path.splitext(sys.argv[0])[1].lower() != ".py": |
---|
| 32 | HELP_DIRECTORY_LOCATION = "doc" |
---|
| 33 | else: |
---|
| 34 | HELP_DIRECTORY_LOCATION = "docs/sphinx-docs/build/html" |
---|
| 35 | IMAGES_DIRECTORY_LOCATION = HELP_DIRECTORY_LOCATION + "/_images" |
---|
[f721030] | 36 | |
---|
| 37 | def get_app_dir(): |
---|
| 38 | """ |
---|
| 39 | The application directory is the one where the default custom_config.py |
---|
| 40 | file resides. |
---|
| 41 | |
---|
| 42 | :returns: app_path - the path to the applicatin directory |
---|
| 43 | """ |
---|
| 44 | # First, try the directory of the executable we are running |
---|
| 45 | app_path = sys.path[0] |
---|
| 46 | if os.path.isfile(app_path): |
---|
| 47 | app_path = os.path.dirname(app_path) |
---|
| 48 | if os.path.isfile(os.path.join(app_path, "custom_config.py")): |
---|
| 49 | app_path = os.path.abspath(app_path) |
---|
[31c5b58] | 50 | #logging.info("Using application path: %s", app_path) |
---|
[f721030] | 51 | return app_path |
---|
| 52 | |
---|
| 53 | # Next, try the current working directory |
---|
| 54 | if os.path.isfile(os.path.join(os.getcwd(), "custom_config.py")): |
---|
[31c5b58] | 55 | #logging.info("Using application path: %s", os.getcwd()) |
---|
[f721030] | 56 | return os.path.abspath(os.getcwd()) |
---|
| 57 | |
---|
| 58 | # Finally, try the directory of the sasview module |
---|
| 59 | # TODO: gui_manager will have to know about sasview until we |
---|
| 60 | # clean all these module variables and put them into a config class |
---|
| 61 | # that can be passed by sasview.py. |
---|
[6cb305a] | 62 | # logging.info(sys.executable) |
---|
| 63 | # logging.info(str(sys.argv)) |
---|
[f721030] | 64 | from sas import sasview as sasview |
---|
| 65 | app_path = os.path.dirname(sasview.__file__) |
---|
[6cb305a] | 66 | # logging.info("Using application path: %s", app_path) |
---|
[f721030] | 67 | return app_path |
---|
| 68 | |
---|
| 69 | def get_user_directory(): |
---|
| 70 | """ |
---|
| 71 | Returns the user's home directory |
---|
| 72 | """ |
---|
| 73 | userdir = os.path.join(os.path.expanduser("~"), ".sasview") |
---|
| 74 | if not os.path.isdir(userdir): |
---|
| 75 | os.makedirs(userdir) |
---|
| 76 | return userdir |
---|
| 77 | |
---|
[481ff26] | 78 | def _find_local_config(confg_file, path): |
---|
[f721030] | 79 | """ |
---|
| 80 | Find configuration file for the current application |
---|
| 81 | """ |
---|
| 82 | config_module = None |
---|
| 83 | fObj = None |
---|
| 84 | try: |
---|
[481ff26] | 85 | fObj, path_config, descr = imp.find_module(confg_file, [path]) |
---|
| 86 | config_module = imp.load_module(confg_file, fObj, path_config, descr) |
---|
[e540cd2] | 87 | except ImportError: |
---|
[31c5b58] | 88 | pass |
---|
| 89 | #logging.error("Error loading %s/%s: %s" % (path, confg_file, sys.exc_value)) |
---|
[83eb5208] | 90 | except ValueError: |
---|
[b3e8629] | 91 | print("Value error") |
---|
[83eb5208] | 92 | pass |
---|
[f721030] | 93 | finally: |
---|
| 94 | if fObj is not None: |
---|
| 95 | fObj.close() |
---|
[31c5b58] | 96 | #logging.info("GuiManager loaded %s/%s" % (path, confg_file)) |
---|
[f721030] | 97 | return config_module |
---|
| 98 | |
---|
| 99 | |
---|
[3c8242c] | 100 | # Get APP folder |
---|
| 101 | PATH_APP = get_app_dir() |
---|
| 102 | DATAPATH = PATH_APP |
---|
[b0c5e8c] | 103 | |
---|
[3c8242c] | 104 | # Read in the local config, which can either be with the main |
---|
| 105 | # application or in the installation directory |
---|
| 106 | config = _find_local_config('local_config', PATH_APP) |
---|
| 107 | |
---|
| 108 | if config is None: |
---|
| 109 | config = _find_local_config('local_config', os.getcwd()) |
---|
| 110 | else: |
---|
| 111 | pass |
---|
[f721030] | 112 | |
---|
[3c8242c] | 113 | c_conf_dir = CustomDir.setup_conf_dir(PATH_APP) |
---|
[83eb5208] | 114 | |
---|
[3c8242c] | 115 | custom_config = _find_local_config('custom_config', c_conf_dir) |
---|
| 116 | if custom_config is None: |
---|
| 117 | custom_config = _find_local_config('custom_config', os.getcwd()) |
---|
[f721030] | 118 | if custom_config is None: |
---|
[3c8242c] | 119 | msgConfig = "Custom_config file was not imported" |
---|
| 120 | |
---|
| 121 | #read some constants from config |
---|
| 122 | APPLICATION_STATE_EXTENSION = config.APPLICATION_STATE_EXTENSION |
---|
| 123 | APPLICATION_NAME = config.__appname__ |
---|
| 124 | SPLASH_SCREEN_PATH = config.SPLASH_SCREEN_PATH |
---|
| 125 | WELCOME_PANEL_ON = config.WELCOME_PANEL_ON |
---|
| 126 | SPLASH_SCREEN_WIDTH = config.SPLASH_SCREEN_WIDTH |
---|
| 127 | SPLASH_SCREEN_HEIGHT = config.SPLASH_SCREEN_HEIGHT |
---|
| 128 | SS_MAX_DISPLAY_TIME = config.SS_MAX_DISPLAY_TIME |
---|
| 129 | if not WELCOME_PANEL_ON: |
---|
| 130 | WELCOME_PANEL_SHOW = False |
---|
| 131 | else: |
---|
| 132 | WELCOME_PANEL_SHOW = True |
---|
| 133 | try: |
---|
| 134 | DATALOADER_SHOW = custom_config.DATALOADER_SHOW |
---|
| 135 | TOOLBAR_SHOW = custom_config.TOOLBAR_SHOW |
---|
| 136 | FIXED_PANEL = custom_config.FIXED_PANEL |
---|
| 137 | if WELCOME_PANEL_ON: |
---|
| 138 | WELCOME_PANEL_SHOW = custom_config.WELCOME_PANEL_SHOW |
---|
| 139 | PLOPANEL_WIDTH = custom_config.PLOPANEL_WIDTH |
---|
| 140 | DATAPANEL_WIDTH = custom_config.DATAPANEL_WIDTH |
---|
| 141 | GUIFRAME_WIDTH = custom_config.GUIFRAME_WIDTH |
---|
| 142 | GUIFRAME_HEIGHT = custom_config.GUIFRAME_HEIGHT |
---|
| 143 | CONTROL_WIDTH = custom_config.CONTROL_WIDTH |
---|
| 144 | CONTROL_HEIGHT = custom_config.CONTROL_HEIGHT |
---|
| 145 | DEFAULT_PERSPECTIVE = custom_config.DEFAULT_PERSPECTIVE |
---|
| 146 | CLEANUP_PLOT = custom_config.CLEANUP_PLOT |
---|
| 147 | # custom open_path |
---|
| 148 | open_folder = custom_config.DEFAULT_OPEN_FOLDER |
---|
[f0bb711] | 149 | if open_folder is not None and os.path.isdir(open_folder): |
---|
[3c8242c] | 150 | DEFAULT_OPEN_FOLDER = os.path.abspath(open_folder) |
---|
[f721030] | 151 | else: |
---|
| 152 | DEFAULT_OPEN_FOLDER = PATH_APP |
---|
[3c8242c] | 153 | except AttributeError: |
---|
| 154 | DATALOADER_SHOW = True |
---|
| 155 | TOOLBAR_SHOW = True |
---|
| 156 | FIXED_PANEL = True |
---|
| 157 | WELCOME_PANEL_SHOW = False |
---|
| 158 | PLOPANEL_WIDTH = config.PLOPANEL_WIDTH |
---|
| 159 | DATAPANEL_WIDTH = config.DATAPANEL_WIDTH |
---|
| 160 | GUIFRAME_WIDTH = config.GUIFRAME_WIDTH |
---|
| 161 | GUIFRAME_HEIGHT = config.GUIFRAME_HEIGHT |
---|
| 162 | CONTROL_WIDTH = -1 |
---|
| 163 | CONTROL_HEIGHT = -1 |
---|
| 164 | DEFAULT_PERSPECTIVE = None |
---|
| 165 | CLEANUP_PLOT = False |
---|
| 166 | DEFAULT_OPEN_FOLDER = PATH_APP |
---|
| 167 | |
---|
| 168 | #DEFAULT_STYLE = config.DEFAULT_STYLE |
---|
| 169 | |
---|
| 170 | PLUGIN_STATE_EXTENSIONS = config.PLUGIN_STATE_EXTENSIONS |
---|
| 171 | OPEN_SAVE_MENU = config.OPEN_SAVE_PROJECT_MENU |
---|
| 172 | VIEW_MENU = config.VIEW_MENU |
---|
| 173 | EDIT_MENU = config.EDIT_MENU |
---|
| 174 | extension_list = [] |
---|
| 175 | if APPLICATION_STATE_EXTENSION is not None: |
---|
| 176 | extension_list.append(APPLICATION_STATE_EXTENSION) |
---|
| 177 | EXTENSIONS = PLUGIN_STATE_EXTENSIONS + extension_list |
---|
| 178 | try: |
---|
| 179 | PLUGINS_WLIST = '|'.join(config.PLUGINS_WLIST) |
---|
| 180 | except AttributeError: |
---|
| 181 | PLUGINS_WLIST = '' |
---|
| 182 | APPLICATION_WLIST = config.APPLICATION_WLIST |
---|
| 183 | IS_WIN = True |
---|
| 184 | IS_LINUX = False |
---|
| 185 | CLOSE_SHOW = True |
---|
| 186 | TIME_FACTOR = 2 |
---|
| 187 | NOT_SO_GRAPH_LIST = ["BoxSum"] |
---|
[fef38e8] | 188 | |
---|
[f721030] | 189 | |
---|
| 190 | class Communicate(QtCore.QObject): |
---|
| 191 | """ |
---|
| 192 | Utility class for tracking of the Qt signals |
---|
| 193 | """ |
---|
| 194 | # File got successfully read |
---|
| 195 | fileReadSignal = QtCore.pyqtSignal(list) |
---|
| 196 | |
---|
| 197 | # Open File returns "list" of paths |
---|
| 198 | fileDataReceivedSignal = QtCore.pyqtSignal(dict) |
---|
| 199 | |
---|
| 200 | # Update Main window status bar with "str" |
---|
[f82ab8c] | 201 | # Old "StatusEvent" |
---|
[f721030] | 202 | statusBarUpdateSignal = QtCore.pyqtSignal(str) |
---|
| 203 | |
---|
| 204 | # Send data to the current perspective |
---|
| 205 | updatePerspectiveWithDataSignal = QtCore.pyqtSignal(list) |
---|
[5032ea68] | 206 | |
---|
| 207 | # New data in current perspective |
---|
[a281ab8] | 208 | updateModelFromPerspectiveSignal = QtCore.pyqtSignal(QtGui.QStandardItem) |
---|
| 209 | |
---|
[5236449] | 210 | # New theory data in current perspective |
---|
| 211 | updateTheoryFromPerspectiveSignal = QtCore.pyqtSignal(QtGui.QStandardItem) |
---|
| 212 | |
---|
[f82ab8c] | 213 | # New plot requested from the GUI manager |
---|
| 214 | # Old "NewPlotEvent" |
---|
[d48cc19] | 215 | plotRequestedSignal = QtCore.pyqtSignal(list) |
---|
[f82ab8c] | 216 | |
---|
[3b3b40b] | 217 | # Plot from file names |
---|
| 218 | plotFromFilenameSignal = QtCore.pyqtSignal(str) |
---|
| 219 | |
---|
[7d077d1] | 220 | # Plot update requested from a perspective |
---|
| 221 | plotUpdateSignal = QtCore.pyqtSignal(list) |
---|
| 222 | |
---|
[e540cd2] | 223 | # Progress bar update value |
---|
| 224 | progressBarUpdateSignal = QtCore.pyqtSignal(int) |
---|
| 225 | |
---|
[8cb6cd6] | 226 | # Workspace charts added/removed |
---|
| 227 | activeGraphsSignal = QtCore.pyqtSignal(list) |
---|
| 228 | |
---|
[27313b7] | 229 | # Current workspace chart's name changed |
---|
| 230 | activeGraphName = QtCore.pyqtSignal(tuple) |
---|
| 231 | |
---|
[83d6249] | 232 | # Current perspective changed |
---|
| 233 | perspectiveChangedSignal = QtCore.pyqtSignal(str) |
---|
| 234 | |
---|
[38eb433] | 235 | # File/dataset got deleted |
---|
| 236 | dataDeletedSignal = QtCore.pyqtSignal(list) |
---|
| 237 | |
---|
[d5c5d3d] | 238 | # Send data to Data Operation Utility panel |
---|
[f0bb711] | 239 | sendDataToPanelSignal = QtCore.pyqtSignal(dict) |
---|
[d5c5d3d] | 240 | |
---|
| 241 | # Send result of Data Operation Utility panel to Data Explorer |
---|
| 242 | updateModelFromDataOperationPanelSignal = QtCore.pyqtSignal(QtGui.QStandardItem, dict) |
---|
[f82ab8c] | 243 | |
---|
[3b3b40b] | 244 | # Notify about a new custom plugin being written/deleted/modified |
---|
| 245 | customModelDirectoryChanged = QtCore.pyqtSignal() |
---|
| 246 | |
---|
[d4dac80] | 247 | # Notify the gui manager about new data to be added to the grid view |
---|
| 248 | sendDataToGridSignal = QtCore.pyqtSignal(list) |
---|
| 249 | |
---|
| 250 | |
---|
[8548d739] | 251 | def updateModelItemWithPlot(item, update_data, name=""): |
---|
[a281ab8] | 252 | """ |
---|
[1042dba] | 253 | Adds a checkboxed row named "name" to QStandardItem |
---|
[cee5c78] | 254 | Adds 'update_data' to that row. |
---|
[a281ab8] | 255 | """ |
---|
[e540cd2] | 256 | assert isinstance(item, QtGui.QStandardItem) |
---|
[3bdbfcc] | 257 | |
---|
| 258 | # Check if data with the same ID is already present |
---|
| 259 | for index in range(item.rowCount()): |
---|
| 260 | plot_item = item.child(index) |
---|
| 261 | if plot_item.isCheckable(): |
---|
[fbfc488] | 262 | plot_data = plot_item.child(0).data() |
---|
[6cb305a] | 263 | if plot_data.id is not None and \ |
---|
| 264 | (plot_data.name == update_data.name or plot_data.id == update_data.id): |
---|
| 265 | # if plot_data.id is not None and plot_data.id == update_data.id: |
---|
[7d077d1] | 266 | # replace data section in item |
---|
| 267 | plot_item.child(0).setData(update_data) |
---|
| 268 | plot_item.setText(name) |
---|
[cb4d219] | 269 | # Plot title if any |
---|
| 270 | if plot_item.child(1).hasChildren(): |
---|
| 271 | plot_item.child(1).child(0).setText("Title: %s"%name) |
---|
[7d077d1] | 272 | # Force redisplay |
---|
| 273 | return |
---|
[a281ab8] | 274 | |
---|
[5236449] | 275 | # Create the new item |
---|
| 276 | checkbox_item = createModelItemWithPlot(update_data, name) |
---|
| 277 | |
---|
| 278 | # Append the new row to the main item |
---|
| 279 | item.appendRow(checkbox_item) |
---|
| 280 | |
---|
[6a3e1fe] | 281 | class HashableStandardItem(QtGui.QStandardItem): |
---|
| 282 | """ |
---|
| 283 | Subclassed standard item with reimplemented __hash__ |
---|
| 284 | to allow for use as an index. |
---|
| 285 | """ |
---|
| 286 | def __init__(self, parent=None): |
---|
[0261bc1] | 287 | super(HashableStandardItem, self).__init__() |
---|
[6a3e1fe] | 288 | |
---|
| 289 | def __hash__(self): |
---|
| 290 | ''' just a random hash value ''' |
---|
| 291 | #return hash(self.__init__) |
---|
| 292 | return 0 |
---|
| 293 | |
---|
[0261bc1] | 294 | def clone(self): |
---|
| 295 | ''' Assure __hash__ is cloned as well''' |
---|
| 296 | clone = super(HashableStandardItem, self).clone() |
---|
| 297 | clone.__hash__ = self.__hash__ |
---|
| 298 | return clone |
---|
| 299 | |
---|
[6a3e1fe] | 300 | |
---|
[5236449] | 301 | def createModelItemWithPlot(update_data, name=""): |
---|
| 302 | """ |
---|
| 303 | Creates a checkboxed QStandardItem named "name" |
---|
[cee5c78] | 304 | Adds 'update_data' to that row. |
---|
[5236449] | 305 | """ |
---|
[b3e8629] | 306 | py_update_data = update_data |
---|
[5236449] | 307 | |
---|
[6a3e1fe] | 308 | checkbox_item = HashableStandardItem() |
---|
[a281ab8] | 309 | checkbox_item.setCheckable(True) |
---|
| 310 | checkbox_item.setCheckState(QtCore.Qt.Checked) |
---|
| 311 | checkbox_item.setText(name) |
---|
| 312 | |
---|
| 313 | # Add "Info" item |
---|
[5236449] | 314 | if isinstance(py_update_data, (Data1D, Data2D)): |
---|
[1042dba] | 315 | # If Data1/2D added - extract Info from it |
---|
| 316 | info_item = infoFromData(py_update_data) |
---|
| 317 | else: |
---|
| 318 | # otherwise just add a naked item |
---|
[481ff26] | 319 | info_item = QtGui.QStandardItem("Info") |
---|
[a281ab8] | 320 | |
---|
| 321 | # Add the actual Data1D/Data2D object |
---|
| 322 | object_item = QtGui.QStandardItem() |
---|
| 323 | object_item.setData(update_data) |
---|
| 324 | |
---|
[1042dba] | 325 | # Set the data object as the first child |
---|
[a281ab8] | 326 | checkbox_item.setChild(0, object_item) |
---|
| 327 | |
---|
[1042dba] | 328 | # Set info_item as the second child |
---|
[a281ab8] | 329 | checkbox_item.setChild(1, info_item) |
---|
| 330 | |
---|
[5236449] | 331 | # And return the newly created item |
---|
| 332 | return checkbox_item |
---|
[1042dba] | 333 | |
---|
[8548d739] | 334 | def updateModelItem(item, update_data, name=""): |
---|
| 335 | """ |
---|
| 336 | Adds a simple named child to QStandardItem |
---|
| 337 | """ |
---|
| 338 | assert isinstance(item, QtGui.QStandardItem) |
---|
| 339 | |
---|
| 340 | # Add the actual Data1D/Data2D object |
---|
| 341 | object_item = QtGui.QStandardItem() |
---|
| 342 | object_item.setText(name) |
---|
[b3e8629] | 343 | object_item.setData(update_data) |
---|
[8548d739] | 344 | |
---|
| 345 | # Append the new row to the main item |
---|
| 346 | item.appendRow(object_item) |
---|
| 347 | |
---|
[f1f3e6a] | 348 | def updateModelItemStatus(model_item, filename="", name="", status=2): |
---|
| 349 | """ |
---|
| 350 | Update status of checkbox related to high- and low-Q extrapolation |
---|
| 351 | choice in Invariant Panel |
---|
| 352 | """ |
---|
| 353 | assert isinstance(model_item, QtGui.QStandardItemModel) |
---|
| 354 | |
---|
| 355 | # Iterate over model looking for items with checkboxes |
---|
| 356 | for index in range(model_item.rowCount()): |
---|
| 357 | item = model_item.item(index) |
---|
[6cb305a] | 358 | if item.text() == filename and item.isCheckable() \ |
---|
| 359 | and item.checkState() == QtCore.Qt.Checked: |
---|
[f1f3e6a] | 360 | # Going 1 level deeper only |
---|
| 361 | for index_2 in range(item.rowCount()): |
---|
| 362 | item_2 = item.child(index_2) |
---|
| 363 | if item_2 and item_2.isCheckable() and item_2.text() == name: |
---|
| 364 | item_2.setCheckState(status) |
---|
| 365 | |
---|
| 366 | return |
---|
| 367 | |
---|
[88e1f57] | 368 | def itemFromFilename(filename, model_item): |
---|
| 369 | """ |
---|
| 370 | Returns the model item text=filename in the model |
---|
| 371 | """ |
---|
| 372 | assert isinstance(model_item, QtGui.QStandardItemModel) |
---|
[b3e8629] | 373 | assert isinstance(filename, str) |
---|
[88e1f57] | 374 | |
---|
| 375 | # Iterate over model looking for named items |
---|
[6cb305a] | 376 | item = list([i for i in [model_item.item(index) |
---|
| 377 | for index in range(model_item.rowCount())] |
---|
| 378 | if str(i.text()) == filename]) |
---|
[88e1f57] | 379 | return item[0] if len(item)>0 else None |
---|
| 380 | |
---|
[d48cc19] | 381 | def plotsFromFilename(filename, model_item): |
---|
| 382 | """ |
---|
| 383 | Returns the list of plots for the item with text=filename in the model |
---|
| 384 | """ |
---|
| 385 | assert isinstance(model_item, QtGui.QStandardItemModel) |
---|
[b3e8629] | 386 | assert isinstance(filename, str) |
---|
[d48cc19] | 387 | |
---|
| 388 | plot_data = [] |
---|
| 389 | # Iterate over model looking for named items |
---|
| 390 | for index in range(model_item.rowCount()): |
---|
| 391 | item = model_item.item(index) |
---|
| 392 | if str(item.text()) == filename: |
---|
| 393 | # TODO: assure item type is correct (either data1/2D or Plotter) |
---|
[fbfc488] | 394 | plot_data.append(item.child(0).data()) |
---|
[56b22f9] | 395 | # Going 1 level deeper only |
---|
| 396 | for index_2 in range(item.rowCount()): |
---|
| 397 | item_2 = item.child(index_2) |
---|
| 398 | if item_2 and item_2.isCheckable(): |
---|
| 399 | # TODO: assure item type is correct (either data1/2D or Plotter) |
---|
[fbfc488] | 400 | plot_data.append(item_2.child(0).data()) |
---|
[d48cc19] | 401 | |
---|
| 402 | return plot_data |
---|
[8548d739] | 403 | |
---|
[1042dba] | 404 | def plotsFromCheckedItems(model_item): |
---|
| 405 | """ |
---|
| 406 | Returns the list of plots for items in the model which are checked |
---|
| 407 | """ |
---|
[e540cd2] | 408 | assert isinstance(model_item, QtGui.QStandardItemModel) |
---|
[1042dba] | 409 | |
---|
| 410 | plot_data = [] |
---|
| 411 | # Iterate over model looking for items with checkboxes |
---|
[9e426c1] | 412 | for index in range(model_item.rowCount()): |
---|
| 413 | item = model_item.item(index) |
---|
[6cb305a] | 414 | |
---|
[9e426c1] | 415 | # Going 1 level deeper only |
---|
| 416 | for index_2 in range(item.rowCount()): |
---|
| 417 | item_2 = item.child(index_2) |
---|
| 418 | if item_2 and item_2.isCheckable() and item_2.checkState() == QtCore.Qt.Checked: |
---|
| 419 | # TODO: assure item type is correct (either data1/2D or Plotter) |
---|
[fbfc488] | 420 | plot_data.append((item_2, item_2.child(0).data())) |
---|
[481ff26] | 421 | |
---|
[6cb305a] | 422 | if item.isCheckable() and item.checkState() == QtCore.Qt.Checked: |
---|
| 423 | # TODO: assure item type is correct (either data1/2D or Plotter) |
---|
| 424 | plot_data.append((item, item.child(0).data())) |
---|
| 425 | |
---|
[9e426c1] | 426 | return plot_data |
---|
| 427 | |
---|
| 428 | def infoFromData(data): |
---|
| 429 | """ |
---|
| 430 | Given Data1D/Data2D object, extract relevant Info elements |
---|
| 431 | and add them to a model item |
---|
| 432 | """ |
---|
[e540cd2] | 433 | assert isinstance(data, (Data1D, Data2D)) |
---|
[9e426c1] | 434 | |
---|
[1042dba] | 435 | info_item = QtGui.QStandardItem("Info") |
---|
[9e426c1] | 436 | |
---|
[481ff26] | 437 | title_item = QtGui.QStandardItem("Title: " + data.title) |
---|
[1042dba] | 438 | info_item.appendRow(title_item) |
---|
[481ff26] | 439 | run_item = QtGui.QStandardItem("Run: " + str(data.run)) |
---|
[1042dba] | 440 | info_item.appendRow(run_item) |
---|
[481ff26] | 441 | type_item = QtGui.QStandardItem("Type: " + str(data.__class__.__name__)) |
---|
[1042dba] | 442 | info_item.appendRow(type_item) |
---|
| 443 | |
---|
| 444 | if data.path: |
---|
[481ff26] | 445 | path_item = QtGui.QStandardItem("Path: " + data.path) |
---|
[1042dba] | 446 | info_item.appendRow(path_item) |
---|
| 447 | |
---|
| 448 | if data.instrument: |
---|
[481ff26] | 449 | instr_item = QtGui.QStandardItem("Instrument: " + data.instrument) |
---|
[1042dba] | 450 | info_item.appendRow(instr_item) |
---|
| 451 | |
---|
| 452 | process_item = QtGui.QStandardItem("Process") |
---|
| 453 | if isinstance(data.process, list) and data.process: |
---|
| 454 | for process in data.process: |
---|
| 455 | process_date = process.date |
---|
| 456 | process_date_item = QtGui.QStandardItem("Date: " + process_date) |
---|
| 457 | process_item.appendRow(process_date_item) |
---|
| 458 | |
---|
| 459 | process_descr = process.description |
---|
| 460 | process_descr_item = QtGui.QStandardItem("Description: " + process_descr) |
---|
| 461 | process_item.appendRow(process_descr_item) |
---|
| 462 | |
---|
| 463 | process_name = process.name |
---|
| 464 | process_name_item = QtGui.QStandardItem("Name: " + process_name) |
---|
| 465 | process_item.appendRow(process_name_item) |
---|
| 466 | |
---|
| 467 | info_item.appendRow(process_item) |
---|
[9e426c1] | 468 | |
---|
| 469 | return info_item |
---|
| 470 | |
---|
[d4dac80] | 471 | def dataFromItem(item): |
---|
| 472 | """ |
---|
| 473 | Retrieve Data1D/2D component from QStandardItem. |
---|
| 474 | The assumption - data stored in SasView standard, in child 0 |
---|
| 475 | """ |
---|
| 476 | return item.child(0).data() |
---|
| 477 | |
---|
[f82ab8c] | 478 | def openLink(url): |
---|
| 479 | """ |
---|
| 480 | Open a URL in an external browser. |
---|
| 481 | Check the URL first, though. |
---|
| 482 | """ |
---|
[b3e8629] | 483 | parsed_url = urllib.parse.urlparse(url) |
---|
[f82ab8c] | 484 | if parsed_url.scheme: |
---|
| 485 | webbrowser.open(url) |
---|
| 486 | else: |
---|
| 487 | msg = "Attempt at opening an invalid URL" |
---|
[b3e8629] | 488 | raise AttributeError(msg) |
---|
[4b71e91] | 489 | |
---|
| 490 | def retrieveData1d(data): |
---|
| 491 | """ |
---|
| 492 | Retrieve 1D data from file and construct its text |
---|
| 493 | representation |
---|
| 494 | """ |
---|
[28a84e9] | 495 | if not isinstance(data, Data1D): |
---|
| 496 | msg = "Incorrect type passed to retrieveData1d" |
---|
[b3e8629] | 497 | raise AttributeError(msg) |
---|
[4b71e91] | 498 | try: |
---|
| 499 | xmin = min(data.x) |
---|
| 500 | ymin = min(data.y) |
---|
| 501 | except: |
---|
| 502 | msg = "Unable to find min/max of \n data named %s" % \ |
---|
| 503 | data.filename |
---|
[31c5b58] | 504 | #logging.error(msg) |
---|
[b3e8629] | 505 | raise ValueError(msg) |
---|
[4b71e91] | 506 | |
---|
| 507 | text = data.__str__() |
---|
| 508 | text += 'Data Min Max:\n' |
---|
| 509 | text += 'X_min = %s: X_max = %s\n' % (xmin, max(data.x)) |
---|
| 510 | text += 'Y_min = %s: Y_max = %s\n' % (ymin, max(data.y)) |
---|
[d5c5d3d] | 511 | if data.dy is not None: |
---|
[4b71e91] | 512 | text += 'dY_min = %s: dY_max = %s\n' % (min(data.dy), max(data.dy)) |
---|
| 513 | text += '\nData Points:\n' |
---|
| 514 | x_st = "X" |
---|
| 515 | for index in range(len(data.x)): |
---|
[d5c5d3d] | 516 | if data.dy is not None and len(data.dy) > index: |
---|
[4b71e91] | 517 | dy_val = data.dy[index] |
---|
| 518 | else: |
---|
| 519 | dy_val = 0.0 |
---|
[d5c5d3d] | 520 | if data.dx is not None and len(data.dx) > index: |
---|
[4b71e91] | 521 | dx_val = data.dx[index] |
---|
| 522 | else: |
---|
| 523 | dx_val = 0.0 |
---|
[d5c5d3d] | 524 | if data.dxl is not None and len(data.dxl) > index: |
---|
[4b71e91] | 525 | if index == 0: |
---|
| 526 | x_st = "Xl" |
---|
| 527 | dx_val = data.dxl[index] |
---|
[d5c5d3d] | 528 | elif data.dxw is not None and len(data.dxw) > index: |
---|
[4b71e91] | 529 | if index == 0: |
---|
| 530 | x_st = "Xw" |
---|
| 531 | dx_val = data.dxw[index] |
---|
| 532 | |
---|
| 533 | if index == 0: |
---|
| 534 | text += "<index> \t<X> \t<Y> \t<dY> \t<d%s>\n" % x_st |
---|
| 535 | text += "%s \t%s \t%s \t%s \t%s\n" % (index, |
---|
| 536 | data.x[index], |
---|
| 537 | data.y[index], |
---|
| 538 | dy_val, |
---|
| 539 | dx_val) |
---|
| 540 | return text |
---|
| 541 | |
---|
| 542 | def retrieveData2d(data): |
---|
| 543 | """ |
---|
[39551a68] | 544 | Retrieve 2D data from file and construct its text |
---|
[4b71e91] | 545 | representation |
---|
| 546 | """ |
---|
[39551a68] | 547 | if not isinstance(data, Data2D): |
---|
| 548 | msg = "Incorrect type passed to retrieveData2d" |
---|
[b3e8629] | 549 | raise AttributeError(msg) |
---|
[39551a68] | 550 | |
---|
[4b71e91] | 551 | text = data.__str__() |
---|
| 552 | text += 'Data Min Max:\n' |
---|
| 553 | text += 'I_min = %s\n' % min(data.data) |
---|
| 554 | text += 'I_max = %s\n\n' % max(data.data) |
---|
| 555 | text += 'Data (First 2501) Points:\n' |
---|
| 556 | text += 'Data columns include err(I).\n' |
---|
| 557 | text += 'ASCII data starts here.\n' |
---|
| 558 | text += "<index> \t<Qx> \t<Qy> \t<I> \t<dI> \t<dQparal> \t<dQperp>\n" |
---|
| 559 | di_val = 0.0 |
---|
| 560 | dx_val = 0.0 |
---|
| 561 | dy_val = 0.0 |
---|
| 562 | len_data = len(data.qx_data) |
---|
[b3e8629] | 563 | for index in range(0, len_data): |
---|
[4b71e91] | 564 | x_val = data.qx_data[index] |
---|
| 565 | y_val = data.qy_data[index] |
---|
| 566 | i_val = data.data[index] |
---|
[d5c5d3d] | 567 | if data.err_data is not None: |
---|
[4b71e91] | 568 | di_val = data.err_data[index] |
---|
[d5c5d3d] | 569 | if data.dqx_data is not None: |
---|
[4b71e91] | 570 | dx_val = data.dqx_data[index] |
---|
[d5c5d3d] | 571 | if data.dqy_data is not None: |
---|
[4b71e91] | 572 | dy_val = data.dqy_data[index] |
---|
| 573 | |
---|
| 574 | text += "%s \t%s \t%s \t%s \t%s \t%s \t%s\n" % (index, |
---|
| 575 | x_val, |
---|
| 576 | y_val, |
---|
| 577 | i_val, |
---|
| 578 | di_val, |
---|
| 579 | dx_val, |
---|
| 580 | dy_val) |
---|
| 581 | # Takes too long time for typical data2d: Break here |
---|
| 582 | if index >= 2500: |
---|
| 583 | text += ".............\n" |
---|
| 584 | break |
---|
| 585 | |
---|
| 586 | return text |
---|
[28a84e9] | 587 | |
---|
[39551a68] | 588 | def onTXTSave(data, path): |
---|
[28a84e9] | 589 | """ |
---|
| 590 | Save file as formatted txt |
---|
| 591 | """ |
---|
| 592 | with open(path,'w') as out: |
---|
| 593 | has_errors = True |
---|
[0261bc1] | 594 | if data.dy is None or not data.dy.any(): |
---|
[28a84e9] | 595 | has_errors = False |
---|
| 596 | # Sanity check |
---|
| 597 | if has_errors: |
---|
| 598 | try: |
---|
| 599 | if len(data.y) != len(data.dy): |
---|
| 600 | has_errors = False |
---|
| 601 | except: |
---|
| 602 | has_errors = False |
---|
| 603 | if has_errors: |
---|
[0261bc1] | 604 | if data.dx is not None and data.dx.any(): |
---|
[28a84e9] | 605 | out.write("<X> <Y> <dY> <dX>\n") |
---|
| 606 | else: |
---|
| 607 | out.write("<X> <Y> <dY>\n") |
---|
| 608 | else: |
---|
| 609 | out.write("<X> <Y>\n") |
---|
| 610 | |
---|
| 611 | for i in range(len(data.x)): |
---|
| 612 | if has_errors: |
---|
[0261bc1] | 613 | if data.dx is not None and data.dx.any(): |
---|
[cee5c78] | 614 | if data.dx[i] is not None: |
---|
[28a84e9] | 615 | out.write("%g %g %g %g\n" % (data.x[i], |
---|
| 616 | data.y[i], |
---|
| 617 | data.dy[i], |
---|
| 618 | data.dx[i])) |
---|
| 619 | else: |
---|
| 620 | out.write("%g %g %g\n" % (data.x[i], |
---|
| 621 | data.y[i], |
---|
| 622 | data.dy[i])) |
---|
| 623 | else: |
---|
| 624 | out.write("%g %g %g\n" % (data.x[i], |
---|
| 625 | data.y[i], |
---|
| 626 | data.dy[i])) |
---|
| 627 | else: |
---|
| 628 | out.write("%g %g\n" % (data.x[i], |
---|
| 629 | data.y[i])) |
---|
| 630 | |
---|
| 631 | def saveData1D(data): |
---|
| 632 | """ |
---|
| 633 | Save 1D data points |
---|
| 634 | """ |
---|
| 635 | default_name = os.path.basename(data.filename) |
---|
| 636 | default_name, extension = os.path.splitext(default_name) |
---|
[e694f0f] | 637 | if not extension: |
---|
| 638 | extension = ".txt" |
---|
[28a84e9] | 639 | default_name += "_out" + extension |
---|
| 640 | |
---|
| 641 | wildcard = "Text files (*.txt);;"\ |
---|
| 642 | "CanSAS 1D files(*.xml)" |
---|
| 643 | kwargs = { |
---|
| 644 | 'caption' : 'Save As', |
---|
| 645 | 'directory' : default_name, |
---|
| 646 | 'filter' : wildcard, |
---|
| 647 | 'parent' : None, |
---|
| 648 | } |
---|
| 649 | # Query user for filename. |
---|
[d6b8a1d] | 650 | filename_tuple = QtWidgets.QFileDialog.getSaveFileName(**kwargs) |
---|
| 651 | filename = filename_tuple[0] |
---|
[28a84e9] | 652 | |
---|
| 653 | # User cancelled. |
---|
| 654 | if not filename: |
---|
| 655 | return |
---|
| 656 | |
---|
| 657 | #Instantiate a loader |
---|
| 658 | loader = Loader() |
---|
| 659 | if os.path.splitext(filename)[1].lower() == ".txt": |
---|
[39551a68] | 660 | onTXTSave(data, filename) |
---|
[28a84e9] | 661 | if os.path.splitext(filename)[1].lower() == ".xml": |
---|
| 662 | loader.save(filename, data, ".xml") |
---|
| 663 | |
---|
| 664 | def saveData2D(data): |
---|
| 665 | """ |
---|
| 666 | Save data2d dialog |
---|
| 667 | """ |
---|
| 668 | default_name = os.path.basename(data.filename) |
---|
| 669 | default_name, _ = os.path.splitext(default_name) |
---|
| 670 | ext_format = ".dat" |
---|
| 671 | default_name += "_out" + ext_format |
---|
| 672 | |
---|
| 673 | wildcard = "IGOR/DAT 2D file in Q_map (*.dat)" |
---|
| 674 | kwargs = { |
---|
| 675 | 'caption' : 'Save As', |
---|
| 676 | 'directory' : default_name, |
---|
| 677 | 'filter' : wildcard, |
---|
| 678 | 'parent' : None, |
---|
| 679 | } |
---|
| 680 | # Query user for filename. |
---|
[d6b8a1d] | 681 | filename_tuple = QtWidgets.QFileDialog.getSaveFileName(**kwargs) |
---|
| 682 | filename = filename_tuple[0] |
---|
[28a84e9] | 683 | |
---|
| 684 | # User cancelled. |
---|
| 685 | if not filename: |
---|
| 686 | return |
---|
[d6b8a1d] | 687 | |
---|
[28a84e9] | 688 | #Instantiate a loader |
---|
| 689 | loader = Loader() |
---|
| 690 | |
---|
| 691 | if os.path.splitext(filename)[1].lower() == ext_format: |
---|
| 692 | loader.save(filename, data, ext_format) |
---|
[e4676c8] | 693 | |
---|
| 694 | class FormulaValidator(QtGui.QValidator): |
---|
| 695 | def __init__(self, parent=None): |
---|
| 696 | super(FormulaValidator, self).__init__(parent) |
---|
| 697 | |
---|
| 698 | def validate(self, input, pos): |
---|
| 699 | |
---|
[fbfc488] | 700 | self._setStyleSheet("") |
---|
| 701 | return QtGui.QValidator.Acceptable, pos |
---|
| 702 | |
---|
| 703 | #try: |
---|
| 704 | # Formula(str(input)) |
---|
| 705 | # self._setStyleSheet("") |
---|
| 706 | # return QtGui.QValidator.Acceptable, pos |
---|
| 707 | |
---|
| 708 | #except Exception as e: |
---|
| 709 | # self._setStyleSheet("background-color:pink;") |
---|
| 710 | # return QtGui.QValidator.Intermediate, pos |
---|
[e4676c8] | 711 | |
---|
| 712 | def _setStyleSheet(self, value): |
---|
| 713 | try: |
---|
| 714 | if self.parent(): |
---|
| 715 | self.parent().setStyleSheet(value) |
---|
| 716 | except: |
---|
| 717 | pass |
---|
[8548d739] | 718 | |
---|
[aadf0af1] | 719 | def xyTransform(data, xLabel="", yLabel=""): |
---|
| 720 | """ |
---|
| 721 | Transforms x and y in View and set the scale |
---|
| 722 | """ |
---|
| 723 | # Changing the scale might be incompatible with |
---|
| 724 | # currently displayed data (for instance, going |
---|
| 725 | # from ln to log when all plotted values have |
---|
| 726 | # negative natural logs). |
---|
| 727 | # Go linear and only change the scale at the end. |
---|
| 728 | xscale = 'linear' |
---|
| 729 | yscale = 'linear' |
---|
| 730 | # Local data is either 1D or 2D |
---|
| 731 | if data.id == 'fit': |
---|
| 732 | return |
---|
| 733 | |
---|
[bb57068] | 734 | # make sure we have some function to operate on |
---|
| 735 | if xLabel is None: |
---|
| 736 | xLabel = 'log10(x)' |
---|
| 737 | if yLabel is None: |
---|
| 738 | yLabel = 'log10(y)' |
---|
| 739 | |
---|
[aadf0af1] | 740 | # control axis labels from the panel itself |
---|
| 741 | yname, yunits = data.get_yaxis() |
---|
| 742 | xname, xunits = data.get_xaxis() |
---|
| 743 | |
---|
| 744 | # Goes through all possible scales |
---|
| 745 | # self.x_label is already wrapped with Latex "$", so using the argument |
---|
| 746 | |
---|
| 747 | # X |
---|
| 748 | if xLabel == "x": |
---|
[dc5ef15] | 749 | data.transformX(DataTransform.toX, DataTransform.errToX) |
---|
[aadf0af1] | 750 | xLabel = "%s(%s)" % (xname, xunits) |
---|
| 751 | if xLabel == "x^(2)": |
---|
[dc5ef15] | 752 | data.transformX(DataTransform.toX2, DataTransform.errToX2) |
---|
| 753 | xunits = convertUnit(2, xunits) |
---|
[aadf0af1] | 754 | xLabel = "%s^{2}(%s)" % (xname, xunits) |
---|
| 755 | if xLabel == "x^(4)": |
---|
[dc5ef15] | 756 | data.transformX(DataTransform.toX4, DataTransform.errToX4) |
---|
| 757 | xunits = convertUnit(4, xunits) |
---|
[aadf0af1] | 758 | xLabel = "%s^{4}(%s)" % (xname, xunits) |
---|
| 759 | if xLabel == "ln(x)": |
---|
[dc5ef15] | 760 | data.transformX(DataTransform.toLogX, DataTransform.errToLogX) |
---|
[aadf0af1] | 761 | xLabel = "\ln{(%s)}(%s)" % (xname, xunits) |
---|
| 762 | if xLabel == "log10(x)": |
---|
[dc5ef15] | 763 | data.transformX(DataTransform.toX_pos, DataTransform.errToX_pos) |
---|
[aadf0af1] | 764 | xscale = 'log' |
---|
| 765 | xLabel = "%s(%s)" % (xname, xunits) |
---|
| 766 | if xLabel == "log10(x^(4))": |
---|
[dc5ef15] | 767 | data.transformX(DataTransform.toX4, DataTransform.errToX4) |
---|
| 768 | xunits = convertUnit(4, xunits) |
---|
[aadf0af1] | 769 | xLabel = "%s^{4}(%s)" % (xname, xunits) |
---|
| 770 | xscale = 'log' |
---|
| 771 | |
---|
| 772 | # Y |
---|
| 773 | if yLabel == "ln(y)": |
---|
[dc5ef15] | 774 | data.transformY(DataTransform.toLogX, DataTransform.errToLogX) |
---|
[aadf0af1] | 775 | yLabel = "\ln{(%s)}(%s)" % (yname, yunits) |
---|
| 776 | if yLabel == "y": |
---|
[dc5ef15] | 777 | data.transformY(DataTransform.toX, DataTransform.errToX) |
---|
[aadf0af1] | 778 | yLabel = "%s(%s)" % (yname, yunits) |
---|
| 779 | if yLabel == "log10(y)": |
---|
[dc5ef15] | 780 | data.transformY(DataTransform.toX_pos, DataTransform.errToX_pos) |
---|
[aadf0af1] | 781 | yscale = 'log' |
---|
| 782 | yLabel = "%s(%s)" % (yname, yunits) |
---|
| 783 | if yLabel == "y^(2)": |
---|
[dc5ef15] | 784 | data.transformY(DataTransform.toX2, DataTransform.errToX2) |
---|
| 785 | yunits = convertUnit(2, yunits) |
---|
[aadf0af1] | 786 | yLabel = "%s^{2}(%s)" % (yname, yunits) |
---|
| 787 | if yLabel == "1/y": |
---|
[dc5ef15] | 788 | data.transformY(DataTransform.toOneOverX, DataTransform.errOneOverX) |
---|
| 789 | yunits = convertUnit(-1, yunits) |
---|
[aadf0af1] | 790 | yLabel = "1/%s(%s)" % (yname, yunits) |
---|
| 791 | if yLabel == "y*x^(2)": |
---|
[dc5ef15] | 792 | data.transformY(DataTransform.toYX2, DataTransform.errToYX2) |
---|
| 793 | xunits = convertUnit(2, xunits) |
---|
[aadf0af1] | 794 | yLabel = "%s \ \ %s^{2}(%s%s)" % (yname, xname, yunits, xunits) |
---|
| 795 | if yLabel == "y*x^(4)": |
---|
[dc5ef15] | 796 | data.transformY(DataTransform.toYX4, DataTransform.errToYX4) |
---|
| 797 | xunits = convertUnit(4, xunits) |
---|
[aadf0af1] | 798 | yLabel = "%s \ \ %s^{4}(%s%s)" % (yname, xname, yunits, xunits) |
---|
| 799 | if yLabel == "1/sqrt(y)": |
---|
[dc5ef15] | 800 | data.transformY(DataTransform.toOneOverSqrtX, DataTransform.errOneOverSqrtX) |
---|
| 801 | yunits = convertUnit(-0.5, yunits) |
---|
[aadf0af1] | 802 | yLabel = "1/\sqrt{%s}(%s)" % (yname, yunits) |
---|
| 803 | if yLabel == "ln(y*x)": |
---|
[dc5ef15] | 804 | data.transformY(DataTransform.toLogXY, DataTransform.errToLogXY) |
---|
[aadf0af1] | 805 | yLabel = "\ln{(%s \ \ %s)}(%s%s)" % (yname, xname, yunits, xunits) |
---|
| 806 | if yLabel == "ln(y*x^(2))": |
---|
[dc5ef15] | 807 | data.transformY(DataTransform.toLogYX2, DataTransform.errToLogYX2) |
---|
| 808 | xunits = convertUnit(2, xunits) |
---|
[aadf0af1] | 809 | yLabel = "\ln (%s \ \ %s^{2})(%s%s)" % (yname, xname, yunits, xunits) |
---|
| 810 | if yLabel == "ln(y*x^(4))": |
---|
[dc5ef15] | 811 | data.transformY(DataTransform.toLogYX4, DataTransform.errToLogYX4) |
---|
| 812 | xunits = convertUnit(4, xunits) |
---|
[aadf0af1] | 813 | yLabel = "\ln (%s \ \ %s^{4})(%s%s)" % (yname, xname, yunits, xunits) |
---|
| 814 | if yLabel == "log10(y*x^(4))": |
---|
[dc5ef15] | 815 | data.transformY(DataTransform.toYX4, DataTransform.errToYX4) |
---|
| 816 | xunits = convertUnit(4, xunits) |
---|
[aadf0af1] | 817 | yscale = 'log' |
---|
| 818 | yLabel = "%s \ \ %s^{4}(%s%s)" % (yname, xname, yunits, xunits) |
---|
| 819 | |
---|
| 820 | # Perform the transformation of data in data1d->View |
---|
| 821 | data.transformView() |
---|
| 822 | |
---|
| 823 | return (xLabel, yLabel, xscale, yscale) |
---|
| 824 | |
---|
[570a58f9] | 825 | def formatNumber(value, high=False): |
---|
| 826 | """ |
---|
| 827 | Return a float in a standardized, human-readable formatted string. |
---|
| 828 | This is used to output readable (e.g. x.xxxe-y) values to the panel. |
---|
| 829 | """ |
---|
| 830 | try: |
---|
| 831 | value = float(value) |
---|
| 832 | except: |
---|
| 833 | output = "NaN" |
---|
| 834 | return output.lstrip().rstrip() |
---|
| 835 | |
---|
| 836 | if high: |
---|
[f7f5796] | 837 | output = "%-7.5g" % value |
---|
[570a58f9] | 838 | |
---|
| 839 | else: |
---|
| 840 | output = "%-5.3g" % value |
---|
| 841 | return output.lstrip().rstrip() |
---|
[ad6b4e2] | 842 | |
---|
| 843 | def convertUnitToHTML(unit): |
---|
| 844 | """ |
---|
| 845 | Convert ASCII unit display into well rendering HTML |
---|
| 846 | """ |
---|
| 847 | if unit == "1/A": |
---|
| 848 | return "Å<sup>-1</sup>" |
---|
| 849 | elif unit == "1/cm": |
---|
| 850 | return "cm<sup>-1</sup>" |
---|
| 851 | elif unit == "Ang": |
---|
| 852 | return "Å" |
---|
| 853 | elif unit == "1e-6/Ang^2": |
---|
| 854 | return "10<sup>-6</sup>/Å<sup>2</sup>" |
---|
| 855 | elif unit == "inf": |
---|
| 856 | return "∞" |
---|
| 857 | elif unit == "-inf": |
---|
| 858 | return "-∞" |
---|
| 859 | else: |
---|
| 860 | return unit |
---|
[dc5ef15] | 861 | |
---|
| 862 | def parseName(name, expression): |
---|
| 863 | """ |
---|
| 864 | remove "_" in front of a name |
---|
| 865 | """ |
---|
| 866 | if re.match(expression, name) is not None: |
---|
| 867 | word = re.split(expression, name, 1) |
---|
| 868 | for item in word: |
---|
| 869 | if item.lstrip().rstrip() != '': |
---|
| 870 | return item |
---|
| 871 | else: |
---|
| 872 | return name |
---|
[fbfc488] | 873 | |
---|
| 874 | def toDouble(value_string): |
---|
| 875 | """ |
---|
| 876 | toFloat conversion which cares deeply about user's locale |
---|
| 877 | """ |
---|
| 878 | # Holy shit this escalated quickly in Qt5. |
---|
| 879 | # No more float() cast on general locales. |
---|
| 880 | value = QtCore.QLocale().toFloat(value_string) |
---|
| 881 | if value[1]: |
---|
| 882 | return value[0] |
---|
| 883 | |
---|
| 884 | # Try generic locale |
---|
| 885 | value = QtCore.QLocale(QtCore.QLocale('en_US')).toFloat(value_string) |
---|
| 886 | if value[1]: |
---|
| 887 | return value[0] |
---|
| 888 | else: |
---|
[0261bc1] | 889 | raise TypeError |
---|
[d6b8a1d] | 890 | |
---|
[3b3b40b] | 891 | def findNextFilename(filename, directory): |
---|
| 892 | """ |
---|
| 893 | Finds the next available (non-existing) name for 'filename' in 'directory'. |
---|
| 894 | plugin.py -> plugin (n).py - for first 'n' for which the file doesn't exist |
---|
| 895 | """ |
---|
| 896 | basename, ext = os.path.splitext(filename) |
---|
| 897 | # limit the number of copies |
---|
| 898 | MAX_FILENAMES = 1000 |
---|
| 899 | # Start with (1) |
---|
| 900 | number_ext = 1 |
---|
| 901 | proposed_filename = "" |
---|
| 902 | found_filename = False |
---|
| 903 | # Find the next available filename or exit if too many copies |
---|
| 904 | while not found_filename or number_ext > MAX_FILENAMES: |
---|
| 905 | proposed_filename = basename + " ("+str(number_ext)+")" + ext |
---|
| 906 | if os.path.exists(os.path.join(directory, proposed_filename)): |
---|
| 907 | number_ext += 1 |
---|
| 908 | else: |
---|
| 909 | found_filename = True |
---|
| 910 | |
---|
| 911 | return proposed_filename |
---|
| 912 | |
---|
| 913 | |
---|
[d6b8a1d] | 914 | class DoubleValidator(QtGui.QDoubleValidator): |
---|
| 915 | """ |
---|
| 916 | Allow only dots as decimal separator |
---|
| 917 | """ |
---|
| 918 | def validate(self, input, pos): |
---|
| 919 | """ |
---|
| 920 | Return invalid for commas |
---|
| 921 | """ |
---|
[0261bc1] | 922 | if input is not None and ',' in input: |
---|
[d6b8a1d] | 923 | return (QtGui.QValidator.Invalid, input, pos) |
---|
| 924 | return super(DoubleValidator, self).validate(input, pos) |
---|
| 925 | |
---|
| 926 | def fixup(self, input): |
---|
| 927 | """ |
---|
| 928 | Correct (remove) potential preexisting content |
---|
| 929 | """ |
---|
[0261bc1] | 930 | super(DoubleValidator, self).fixup(input) |
---|
[d6b8a1d] | 931 | input = input.replace(",", "") |
---|
| 932 | |
---|
[3b8cc00] | 933 | def checkModel(path): |
---|
| 934 | """ |
---|
| 935 | Check that the model save in file 'path' can run. |
---|
| 936 | """ |
---|
[27689dc] | 937 | # The following return needs to be removed once |
---|
| 938 | # the unittest related changes in Sasmodels are commited |
---|
| 939 | return True |
---|
[3b8cc00] | 940 | # try running the model |
---|
| 941 | from sasmodels.sasview_model import load_custom_model |
---|
| 942 | Model = load_custom_model(path) |
---|
| 943 | model = Model() |
---|
| 944 | q = np.array([0.01, 0.1]) |
---|
| 945 | _ = model.evalDistribution(q) |
---|
| 946 | qx, qy = np.array([0.01, 0.01]), np.array([0.1, 0.1]) |
---|
| 947 | _ = model.evalDistribution([qx, qy]) |
---|
| 948 | |
---|
| 949 | # check the model's unit tests run |
---|
| 950 | from sasmodels.model_test import run_one |
---|
| 951 | # TestSuite module in Qt5 now deletes tests in the suite after running, |
---|
| 952 | # so suite[0] in run_one() in sasmodels/model_test.py will contain [None] and |
---|
| 953 | # test.info.tests will raise. |
---|
| 954 | # Not sure how to change the behaviour here, most likely sasmodels will have to |
---|
| 955 | # be modified |
---|
| 956 | result = run_one(path) |
---|
| 957 | |
---|
| 958 | return result |
---|
| 959 | |
---|
[cb4d219] | 960 | |
---|
[d4881f6a] | 961 | def enum(*sequential, **named): |
---|
| 962 | """Create an enumeration object from a list of strings""" |
---|
| 963 | enums = dict(zip(sequential, range(len(sequential))), **named) |
---|
| 964 | return type('Enum', (), enums) |
---|