Changes in src/sas/qtgui/Utilities/GuiUtils.py [2eeda93:aed159f] in sasview
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
src/sas/qtgui/Utilities/GuiUtils.py
r2eeda93 raed159f 11 11 import webbrowser 12 12 import urllib.parse 13 import json14 from io import BytesIO15 13 16 14 import numpy as np … … 28 26 from sas.qtgui.Plotting.PlotterData import Data1D 29 27 from sas.qtgui.Plotting.PlotterData import Data2D 30 from sas.qtgui.Plotting.Plottables import Plottable31 from sas.sascalc.dataloader.data_info import Sample, Source, Vector32 from sas.qtgui.Plotting.Plottables import View33 from sas.qtgui.Plotting.Plottables import PlottableTheory1D34 from sas.qtgui.Plotting.Plottables import PlottableFit1D35 from sas.qtgui.Plotting.Plottables import Text36 from sas.qtgui.Plotting.Plottables import Chisq37 from sas.qtgui.MainWindow.DataState import DataState38 39 28 from sas.sascalc.dataloader.loader import Loader 40 29 from sas.qtgui.Utilities import CustomDir … … 106 95 except ImportError: 107 96 pass 108 #logging.error("Error loading %s/%s: %s" % (path, confg_file, sys.exc_value))109 97 except ValueError: 110 98 print("Value error") … … 113 101 if fObj is not None: 114 102 fObj.close() 115 #logging.info("GuiManager loaded %s/%s" % (path, confg_file))116 103 return config_module 117 104 … … 270 257 sendDataToGridSignal = QtCore.pyqtSignal(list) 271 258 259 # Action Save Analysis triggered 260 saveAnalysisSignal = QtCore.pyqtSignal() 261 272 262 # Mask Editor requested 273 263 maskEditorSignal = QtCore.pyqtSignal(Data2D) … … 294 284 changeDataExplorerTabSignal = QtCore.pyqtSignal(int) 295 285 296 def updateModelItemWithPlot(item, update_data, name="", checkbox_state=None): 286 # Plot fitting results (FittingWidget->GuiManager) 287 resultPlotUpdateSignal = QtCore.pyqtSignal(list) 288 289 def updateModelItemWithPlot(item, update_data, name=""): 297 290 """ 298 291 Adds a checkboxed row named "name" to QStandardItem … … 319 312 # Force redisplay 320 313 return 314 321 315 # Create the new item 322 316 checkbox_item = createModelItemWithPlot(update_data, name) 323 317 324 if checkbox_state is not None:325 checkbox_item.setCheckState(checkbox_state)326 318 # Append the new row to the main item 327 319 item.appendRow(checkbox_item) … … 574 566 if isinstance(data.process, list) and data.process: 575 567 for process in data.process: 576 if process is None:577 continue578 568 process_date = process.date 579 569 process_date_item = QtGui.QStandardItem("Date: " + process_date) … … 1150 1140 return result 1151 1141 1152 def saveData(fp, data):1153 """1154 save content of data to fp (a .write()-supporting file-like object)1155 """1156 1157 def add_type(dict, type):1158 dict['__type__'] = type.__name__1159 return dict1160 1161 def jdefault(o):1162 """1163 objects that can't otherwise be serialized need to be converted1164 """1165 # tuples and sets (TODO: default JSONEncoder converts tuples to lists, create custom Encoder that preserves tuples)1166 if isinstance(o, (tuple, set)):1167 content = { 'data': list(o) }1168 return add_type(content, type(o))1169 1170 # "simple" types1171 if isinstance(o, (Sample, Source, Vector)):1172 return add_type(o.__dict__, type(o))1173 if isinstance(o, (Plottable, View)):1174 return add_type(o.__dict__, type(o))1175 1176 # DataState1177 if isinstance(o, (Data1D, Data2D)):1178 # don't store parent1179 content = o.__dict__.copy()1180 #content.pop('parent')1181 return add_type(content, type(o))1182 1183 # ndarray1184 if isinstance(o, np.ndarray):1185 buffer = BytesIO()1186 np.save(buffer, o)1187 buffer.seek(0)1188 content = { 'data': buffer.read().decode('latin-1') }1189 return add_type(content, type(o))1190 1191 # not supported1192 logging.info("data cannot be serialized to json: %s" % type(o))1193 return None1194 1195 json.dump(data, fp, indent=2, sort_keys=True, default=jdefault)1196 1197 def readDataFromFile(fp):1198 '''1199 '''1200 supported = [1201 tuple, set,1202 Sample, Source, Vector,1203 Plottable, Data1D, Data2D, PlottableTheory1D, PlottableFit1D, Text, Chisq, View,1204 DataState, np.ndarray]1205 1206 lookup = dict((cls.__name__, cls) for cls in supported)1207 1208 class TooComplexException(Exception):1209 pass1210 1211 def simple_type(cls, data, level):1212 class Empty(object):1213 def __init__(self):1214 for key, value in data.items():1215 setattr(self, key, generate(value, level))1216 1217 # create target object1218 o = Empty()1219 o.__class__ = cls1220 1221 return o1222 1223 def construct(type, data, level):1224 try:1225 cls = lookup[type]1226 except KeyError:1227 logging.info('unknown type: %s' % type)1228 return None1229 1230 # tuples and sets1231 if cls in (tuple, set):1232 # convert list to tuple/set1233 return cls(generate(data['data'], level))1234 1235 # "simple" types1236 if cls in (Sample, Source, Vector):1237 return simple_type(cls, data, level)1238 if issubclass(cls, Plottable) or (cls == View):1239 return simple_type(cls, data, level)1240 1241 # DataState1242 if cls == DataState:1243 o = simple_type(cls, data, level)1244 o.parent = None # TODO: set to ???1245 return o1246 1247 # ndarray1248 if cls == np.ndarray:1249 buffer = BytesIO()1250 buffer.write(data['data'].encode('latin-1'))1251 buffer.seek(0)1252 return np.load(buffer)1253 1254 logging.info('not implemented: %s, %s' % (type, cls))1255 return None1256 1257 def generate(data, level):1258 if level > 16: # recursion limit (arbitrary number)1259 raise TooComplexException()1260 else:1261 level += 11262 1263 if isinstance(data, dict):1264 try:1265 type = data['__type__']1266 except KeyError:1267 # if dictionary doesn't have __type__ then it is assumed to be just an ordinary dictionary1268 o = {}1269 for key, value in data.items():1270 o[key] = generate(value, level)1271 return o1272 1273 return construct(type, data, level)1274 1275 if isinstance(data, list):1276 return [generate(item, level) for item in data]1277 1278 return data1279 1280 new_stored_data = {}1281 for id, data in json.load(fp).items():1282 try:1283 new_stored_data[id] = generate(data, 0)1284 except TooComplexException:1285 logging.info('unable to load %s' % id)1286 1287 return new_stored_data1288 1289 1142 1290 1143 def enum(*sequential, **named):
Note: See TracChangeset
for help on using the changeset viewer.