Changeset 345b3b3 in sasview for src/sas/qtgui/Utilities


Ignore:
Timestamp:
Oct 9, 2018 4:08:54 PM (6 years ago)
Author:
Piotr Rozyczko <piotr.rozyczko@…>
Branches:
ESS_GUI, ESS_GUI_batch_fitting, ESS_GUI_bumps_abstraction, ESS_GUI_iss1116, ESS_GUI_opencl, ESS_GUI_ordering, ESS_GUI_sync_sascalc
Children:
b95d748
Parents:
f20ea3f
Message:

Save status of data explorer

File:
1 edited

Legend:

Unmodified
Added
Removed
  • src/sas/qtgui/Utilities/GuiUtils.py

    r63467b6 r345b3b3  
    1111import webbrowser 
    1212import urllib.parse 
     13import json 
     14from io import BytesIO 
    1315 
    1416import numpy as np 
     
    2628from sas.qtgui.Plotting.PlotterData import Data1D 
    2729from sas.qtgui.Plotting.PlotterData import Data2D 
     30from sas.qtgui.Plotting.Plottables import Plottable 
     31from sas.sascalc.dataloader.data_info import Sample, Source, Vector 
     32from sas.qtgui.Plotting.Plottables import View 
     33from sas.qtgui.Plotting.Plottables import PlottableTheory1D 
     34from sas.qtgui.Plotting.Plottables import PlottableFit1D 
     35from sas.qtgui.Plotting.Plottables import Text 
     36from sas.qtgui.Plotting.Plottables import Chisq 
     37from sas.qtgui.MainWindow.DataState import DataState 
     38 
    2839from sas.sascalc.dataloader.loader import Loader 
    2940from sas.qtgui.Utilities import CustomDir 
     
    286297    changeDataExplorerTabSignal = QtCore.pyqtSignal(int) 
    287298 
    288 def updateModelItemWithPlot(item, update_data, name=""): 
     299def updateModelItemWithPlot(item, update_data, name="", checkbox_state=None): 
    289300    """ 
    290301    Adds a checkboxed row named "name" to QStandardItem 
     
    311322            # Force redisplay 
    312323            return 
    313  
    314324    # Create the new item 
    315325    checkbox_item = createModelItemWithPlot(update_data, name) 
    316326 
     327    if checkbox_state is not None: 
     328        checkbox_item.setCheckState(checkbox_state) 
    317329    # Append the new row to the main item 
    318330    item.appendRow(checkbox_item) 
     
    11391151    return result 
    11401152 
     1153def saveData(fp, data): 
     1154    """ 
     1155    save content of data to fp (a .write()-supporting file-like object) 
     1156    """ 
     1157 
     1158    def add_type(dict, type): 
     1159        dict['__type__'] = type.__name__ 
     1160        return dict 
     1161 
     1162    def jdefault(o): 
     1163        """ 
     1164        objects that can't otherwise be serialized need to be converted 
     1165        """ 
     1166        # tuples and sets (TODO: default JSONEncoder converts tuples to lists, create custom Encoder that preserves tuples) 
     1167        if isinstance(o, (tuple, set)): 
     1168            content = { 'data': list(o) } 
     1169            return add_type(content, type(o)) 
     1170 
     1171        # "simple" types 
     1172        if isinstance(o, (Sample, Source, Vector)): 
     1173            return add_type(o.__dict__, type(o)) 
     1174        if isinstance(o, (Plottable, View)): 
     1175            return add_type(o.__dict__, type(o)) 
     1176 
     1177        # DataState 
     1178        if isinstance(o, (Data1D, Data2D)): 
     1179            # don't store parent 
     1180            content = o.__dict__.copy() 
     1181            #content.pop('parent') 
     1182            return add_type(content, type(o)) 
     1183 
     1184        # ndarray 
     1185        if isinstance(o, np.ndarray): 
     1186            buffer = BytesIO() 
     1187            np.save(buffer, o) 
     1188            buffer.seek(0) 
     1189            content = { 'data': buffer.read().decode('latin-1') } 
     1190            return add_type(content, type(o)) 
     1191 
     1192        # not supported 
     1193        logging.info("data cannot be serialized to json: %s" % type(o)) 
     1194        return None 
     1195 
     1196    json.dump(data, fp, indent=2, sort_keys=True, default=jdefault) 
     1197 
     1198def readDataFromFile(fp): 
     1199    ''' 
     1200    ''' 
     1201    supported = [ 
     1202        tuple, set, 
     1203        Sample, Source, Vector, 
     1204        Plottable, Data1D, Data2D, PlottableTheory1D, PlottableFit1D, Text, Chisq, View, 
     1205        DataState, np.ndarray] 
     1206 
     1207    lookup = dict((cls.__name__, cls) for cls in supported) 
     1208 
     1209    class TooComplexException(Exception): 
     1210        pass 
     1211 
     1212    def simple_type(cls, data, level): 
     1213        class Empty(object): 
     1214            def __init__(self): 
     1215                for key, value in data.items(): 
     1216                    setattr(self, key, generate(value, level)) 
     1217 
     1218        # create target object 
     1219        o = Empty() 
     1220        o.__class__ = cls 
     1221 
     1222        return o 
     1223 
     1224    def construct(type, data, level): 
     1225        try: 
     1226            cls = lookup[type] 
     1227        except KeyError: 
     1228            logging.info('unknown type: %s' % type) 
     1229            return None 
     1230 
     1231        # tuples and sets 
     1232        if cls in (tuple, set): 
     1233            # convert list to tuple/set 
     1234            return cls(generate(data['data'], level)) 
     1235 
     1236        # "simple" types 
     1237        if cls in (Sample, Source, Vector): 
     1238            return simple_type(cls, data, level) 
     1239        if issubclass(cls, Plottable) or (cls == View): 
     1240            return simple_type(cls, data, level) 
     1241 
     1242        # DataState 
     1243        if cls == DataState: 
     1244            o = simple_type(cls, data, level) 
     1245            o.parent = None # TODO: set to ??? 
     1246            return o 
     1247 
     1248        # ndarray 
     1249        if cls == np.ndarray: 
     1250            buffer = BytesIO() 
     1251            buffer.write(data['data'].encode('latin-1')) 
     1252            buffer.seek(0) 
     1253            return np.load(buffer) 
     1254 
     1255        logging.info('not implemented: %s, %s' % (type, cls)) 
     1256        return None 
     1257 
     1258    def generate(data, level): 
     1259        if level > 16: # recursion limit (arbitrary number) 
     1260            raise TooComplexException() 
     1261        else: 
     1262            level += 1 
     1263 
     1264        if isinstance(data, dict): 
     1265            try: 
     1266                type = data['__type__'] 
     1267            except KeyError: 
     1268                # if dictionary doesn't have __type__ then it is assumed to be just an ordinary dictionary 
     1269                o = {} 
     1270                for key, value in data.items(): 
     1271                    o[key] = generate(value, level) 
     1272                return o 
     1273 
     1274            return construct(type, data, level) 
     1275 
     1276        if isinstance(data, list): 
     1277            return [generate(item, level) for item in data] 
     1278 
     1279        return data 
     1280 
     1281    new_stored_data = {} 
     1282    for id, data in json.load(fp).items(): 
     1283        try: 
     1284            new_stored_data[id] = generate(data, 0) 
     1285        except TooComplexException: 
     1286            logging.info('unable to load %s' % id) 
     1287 
     1288    return new_stored_data 
     1289 
    11411290 
    11421291def enum(*sequential, **named): 
Note: See TracChangeset for help on using the changeset viewer.