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