[646b896] | 1 | """ |
---|
| 2 | Corfunc perspective |
---|
| 3 | """ |
---|
| 4 | import wx |
---|
[9c2eeb1] | 5 | import sys |
---|
[646b896] | 6 | import logging |
---|
[8d79a87] | 7 | import copy |
---|
[646b896] | 8 | from sas.sasgui.guiframe.plugin_base import PluginBase |
---|
| 9 | from sas.sasgui.guiframe.gui_manager import MDIFrame |
---|
[9c2eeb1] | 10 | from sas.sasgui.guiframe.events import StatusEvent |
---|
| 11 | from sas.sasgui.guiframe.events import NewPlotEvent |
---|
| 12 | from sas.sasgui.guiframe.gui_style import GUIFRAME_ID |
---|
[646b896] | 13 | from sas.sasgui.perspectives.corfunc.corfunc_panel import CorfuncPanel |
---|
[9c2eeb1] | 14 | from sas.sasgui.guiframe.dataFitting import Data1D |
---|
| 15 | from sas.sasgui.perspectives.pr.pr_widgets import DataDialog |
---|
[c68b8b5] | 16 | from sas.sasgui.perspectives.corfunc.corfunc_state import Reader |
---|
| 17 | from sas.sascalc.dataloader.loader import Loader |
---|
| 18 | import sas.sascalc.dataloader |
---|
[9c2eeb1] | 19 | |
---|
| 20 | |
---|
[8d79a87] | 21 | GROUP_ID_IQ_DATA = r"$I(q)$" |
---|
[9c2eeb1] | 22 | IQ_DATA_LABEL = r"$I_{obs}(q)$" |
---|
[8d79a87] | 23 | IQ_EXTRAPOLATED_DATA_LABEL = r"$I_{extrap}(q)$" |
---|
[9c2eeb1] | 24 | |
---|
[bd9f10e] | 25 | GROUP_ID_TRANSFORM = r"$\Gamma(x)$" |
---|
| 26 | TRANSFORM_LABEL = r"$\Gamma(x)$" |
---|
| 27 | |
---|
[646b896] | 28 | |
---|
| 29 | class Plugin(PluginBase): |
---|
| 30 | """ |
---|
| 31 | This class defines the interface for a plugin class for a correlation |
---|
| 32 | function perspective |
---|
| 33 | """ |
---|
| 34 | |
---|
| 35 | def __init__(self): |
---|
| 36 | PluginBase.__init__(self, name="Correlation Function") |
---|
| 37 | logging.info("Correlation function plug-in started") |
---|
| 38 | self._always_active = True |
---|
[c68b8b5] | 39 | self.state_reader = Reader(self.set_state) |
---|
| 40 | self._extensions = '.cor' |
---|
| 41 | l = Loader() |
---|
| 42 | l.associate_file_reader('.cor', self.state_reader) |
---|
| 43 | |
---|
[646b896] | 44 | def get_panels(self, parent): |
---|
| 45 | """ |
---|
| 46 | Define the GUI panels |
---|
| 47 | """ |
---|
| 48 | self.parent = parent |
---|
| 49 | self.frame = MDIFrame(self.parent, None, 'None', (100,200)) |
---|
[9c2eeb1] | 50 | self.data_id = IQ_DATA_LABEL |
---|
[646b896] | 51 | self.corfunc_panel = CorfuncPanel(parent=self.frame) |
---|
| 52 | self.frame.set_panel(self.corfunc_panel) |
---|
| 53 | self.corfunc_panel.set_manager(self) |
---|
| 54 | self.perspective.append(self.corfunc_panel.window_name) |
---|
| 55 | |
---|
| 56 | return [self.corfunc_panel] |
---|
[9c2eeb1] | 57 | |
---|
[c68b8b5] | 58 | def set_state(self, state=None, datainfo=None): |
---|
| 59 | """ |
---|
| 60 | Callback for CorfuncState reader. Called when a .cor file is loaded |
---|
| 61 | """ |
---|
| 62 | self.corfunc_panel.set_state(state=state, data=datainfo) |
---|
| 63 | |
---|
[9c2eeb1] | 64 | def set_data(self, data_list=None): |
---|
| 65 | """ |
---|
| 66 | Load the data that's been selected |
---|
| 67 | |
---|
| 68 | :param data_list: The data to load in |
---|
| 69 | """ |
---|
| 70 | if data_list is None: |
---|
| 71 | data_list = [] |
---|
| 72 | if len(data_list) >= 1: |
---|
[689e5ff] | 73 | msg = "" |
---|
[9c2eeb1] | 74 | if len(data_list) == 1: |
---|
| 75 | data = data_list[0] |
---|
| 76 | else: |
---|
| 77 | data_1d_list = [] |
---|
| 78 | data_2d_list = [] |
---|
| 79 | err_msg = "" |
---|
| 80 | |
---|
| 81 | for data in data_list: |
---|
| 82 | if data is not None: |
---|
| 83 | if issubclass(data.__class__, Data1D): |
---|
| 84 | data_1d_list.append(data) |
---|
| 85 | else: |
---|
| 86 | err_msg += "{} type {} \n".format(str(data.name), |
---|
| 87 | str(data.__class__)) |
---|
| 88 | data_2d_list.append(data) |
---|
| 89 | if len(data_2d_list) > 0: |
---|
| 90 | msg = "Corfunc doesn't support the following data types:\n" |
---|
| 91 | msg += err_msg |
---|
| 92 | if len(data_1d_list) == 0: |
---|
| 93 | msg += "No data recieved" |
---|
| 94 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
| 95 | info='error')) |
---|
| 96 | return |
---|
| 97 | elif len(data_list) > 1: |
---|
| 98 | msg = "Corfunc does not allow multiple data\n" |
---|
| 99 | msg += "Please select one.\n" |
---|
| 100 | dlg = DataDialog(data_list=data_1d_list, text=msg) |
---|
| 101 | if dlg.ShowModal() == wx.ID_OK: |
---|
| 102 | data = dlg.get_data() |
---|
| 103 | else: |
---|
| 104 | data = None |
---|
| 105 | dlg.Destroy() |
---|
| 106 | |
---|
| 107 | if data is None: |
---|
| 108 | msg += "Corfunc recieved no data\n" |
---|
| 109 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
| 110 | info='error')) |
---|
| 111 | return |
---|
| 112 | if issubclass(data.__class__, Data1D): |
---|
| 113 | try: |
---|
| 114 | wx.PostEvent(self.parent, NewPlotEvent(action='remove', |
---|
| 115 | group_id=GROUP_ID_IQ_DATA, |
---|
| 116 | id=self.data_id)) |
---|
| 117 | self.data_id = data.id |
---|
[c68b8b5] | 118 | self.corfunc_panel.set_data(data) |
---|
[9c2eeb1] | 119 | except: |
---|
| 120 | msg = "Corfunc set_data: " + str(sys.exc_value) |
---|
| 121 | wx.PostEvent(self.parent, StatusEvent(status=msg, |
---|
| 122 | info='error')) |
---|
| 123 | |
---|
[8d79a87] | 124 | def show_data(self, data, label, path=None, reset=False): |
---|
[9c2eeb1] | 125 | """ |
---|
| 126 | Show data read from a file |
---|
| 127 | |
---|
| 128 | :param path: The path to the file |
---|
| 129 | :param data: The data to plot (Data1D) |
---|
[fd5cfaf] | 130 | :param label: What to label the plot. Also used as the plot ID |
---|
[9c2eeb1] | 131 | :param reset: If True, all other plottables will be cleared |
---|
| 132 | """ |
---|
[8d79a87] | 133 | new_plot = Data1D(data.x, data.y, dy=data.dy) |
---|
| 134 | |
---|
[bd9f10e] | 135 | group_id = "" |
---|
[8d79a87] | 136 | if label == IQ_DATA_LABEL or label == IQ_EXTRAPOLATED_DATA_LABEL: |
---|
| 137 | new_plot.xaxis("\\rm{Q}", 'A^{-1}') |
---|
| 138 | new_plot.yaxis("\\rm{Intensity} ", "cm^{-1}") |
---|
[bd9f10e] | 139 | group_id = GROUP_ID_IQ_DATA |
---|
| 140 | elif label == TRANSFORM_LABEL: |
---|
| 141 | new_plot.xaxis("{x}", 'A') |
---|
| 142 | new_plot.yaxis("{\\Gamma}", '') |
---|
| 143 | group_id = GROUP_ID_TRANSFORM |
---|
[9c2eeb1] | 144 | new_plot.symbol = GUIFRAME_ID.CURVE_SYMBOL_NUM |
---|
[8d79a87] | 145 | new_plot.id = label |
---|
| 146 | new_plot.name = label |
---|
[bd9f10e] | 147 | new_plot.group_id = group_id |
---|
[9c2eeb1] | 148 | new_plot.interactive = True |
---|
[bd9f10e] | 149 | new_plot.title = group_id.replace('$', '').replace('\\', '') |
---|
[31aa3a3] | 150 | # Show data on a linear scale |
---|
[a51fed16] | 151 | new_plot.xtransform = 'x' |
---|
| 152 | new_plot.ytransform = 'y' |
---|
[9c2eeb1] | 153 | wx.PostEvent(self.parent, |
---|
[bd9f10e] | 154 | NewPlotEvent(plot=new_plot, title=new_plot.title, |
---|
| 155 | reset=reset)) |
---|