[7248d75d] | 1 | import sys |
---|
| 2 | import unittest |
---|
| 3 | |
---|
| 4 | import numpy |
---|
[7fb471d] | 5 | from unittest.mock import MagicMock |
---|
[7248d75d] | 6 | |
---|
| 7 | # set up import paths |
---|
| 8 | import sas.qtgui.path_prepare |
---|
| 9 | |
---|
| 10 | # Local |
---|
[83eb5208] | 11 | from sas.qtgui.Utilities.GuiUtils import * |
---|
[7248d75d] | 12 | from sas.qtgui.Perspectives.Fitting.FittingWidget import * |
---|
[dc5ef15] | 13 | from sas.qtgui.Plotting.PlotterData import Data1D |
---|
[7248d75d] | 14 | |
---|
| 15 | |
---|
| 16 | class FittingLogicTest(unittest.TestCase): |
---|
| 17 | """Test the fitting logic class""" |
---|
| 18 | |
---|
| 19 | def setUp(self): |
---|
| 20 | """Create the component""" |
---|
| 21 | data = Data1D(x=[1,2,3],y=[3,4,5]) |
---|
| 22 | self.logic = FittingLogic(data=data) |
---|
| 23 | |
---|
| 24 | def testDefaults(self): |
---|
| 25 | """Test the component in its default state""" |
---|
| 26 | self.assertIsInstance(self.logic.data, Data1D) |
---|
| 27 | self.assertTrue(self.logic.data_is_loaded) |
---|
| 28 | self.assertEqual(self.logic.data, self.logic._data) |
---|
| 29 | |
---|
| 30 | def testComputeDataRange(self): |
---|
| 31 | """ |
---|
| 32 | Tests the data range calculator on Data1D/Data2D |
---|
| 33 | """ |
---|
| 34 | # Using the default data |
---|
| 35 | qmin, qmax, npts = self.logic.computeDataRange() |
---|
| 36 | |
---|
| 37 | self.assertEqual(qmin, 1) |
---|
| 38 | self.assertEqual(qmax, 3) |
---|
| 39 | self.assertEqual(npts, 3) |
---|
| 40 | |
---|
| 41 | # data with more points |
---|
| 42 | data = Data1D(x=[-10, 2, 10, 20],y=[-3, 4, 10, 50]) |
---|
| 43 | self.logic.data=data |
---|
| 44 | qmin, qmax, npts = self.logic.computeDataRange() |
---|
| 45 | |
---|
| 46 | self.assertEqual(qmin, -10) |
---|
| 47 | self.assertEqual(qmax, 20) |
---|
| 48 | self.assertEqual(npts, 4) |
---|
| 49 | |
---|
| 50 | def testCreateDefault1dData(self): |
---|
| 51 | """ |
---|
| 52 | Tests the default 1D set |
---|
| 53 | """ |
---|
| 54 | interval = numpy.linspace(start=1, stop=10, num=10, endpoint=True) |
---|
| 55 | self.logic.createDefault1dData(interval=interval) |
---|
| 56 | |
---|
| 57 | self.assertEqual(self.logic.data.id, ('0 data')) |
---|
| 58 | self.assertEqual(self.logic.data.group_id, ('0 Model1D')) |
---|
| 59 | self.assertFalse(self.logic.data.is_data) |
---|
| 60 | self.assertEqual(self.logic.data._xaxis, ('\\rm{Q}')) |
---|
| 61 | self.assertEqual(self.logic.data._xunit, ('A^{-1}')) |
---|
| 62 | self.assertEqual(self.logic.data._yaxis, ('\\rm{Intensity}')) |
---|
| 63 | self.assertEqual(self.logic.data._yunit, ('cm^{-1}')) |
---|
| 64 | |
---|
| 65 | def testCreateDefault2dData(self): |
---|
| 66 | """ |
---|
| 67 | Tests the default 2D set |
---|
| 68 | """ |
---|
| 69 | self.logic.createDefault2dData(qmax=0.35, qstep=50, tab_id=8) |
---|
| 70 | |
---|
| 71 | self.assertEqual(self.logic.data.id, ('8 data')) |
---|
| 72 | self.assertEqual(self.logic.data.group_id, ('8 Model2D')) |
---|
| 73 | self.assertFalse(self.logic.data.is_data) |
---|
| 74 | self.assertEqual(self.logic.data._xaxis, ('\\rm{Q_{x}}')) |
---|
| 75 | self.assertEqual(self.logic.data._xunit, ('A^{-1}')) |
---|
| 76 | self.assertEqual(self.logic.data._yaxis, ('\\rm{Q_{y}}')) |
---|
| 77 | self.assertEqual(self.logic.data._yunit, ('A^{-1}')) |
---|
| 78 | |
---|
| 79 | self.assertEqual(self.logic.data.xmin, -0.35) |
---|
| 80 | self.assertEqual(self.logic.data.xmax, 0.35) |
---|
| 81 | |
---|
| 82 | self.assertEqual(self.logic.data.ymin, -0.35) |
---|
| 83 | self.assertEqual(self.logic.data.ymax, 0.35) |
---|
| 84 | |
---|
| 85 | self.assertEqual(self.logic.data.data.sum(), 2500.0) # 50x50 array of 1's |
---|
| 86 | self.assertEqual(self.logic.data.err_data.sum(axis=0), 2500.0) |
---|
| 87 | self.assertAlmostEqual(self.logic.data.qx_data.sum(axis=0), 0.0) |
---|
| 88 | self.assertAlmostEqual(self.logic.data.qy_data.sum(), 0.0) |
---|
| 89 | self.assertAlmostEqual(self.logic.data.q_data.sum(), 683.106490, 6) |
---|
| 90 | self.assertTrue(numpy.all(self.logic.data.mask)) |
---|
| 91 | self.assertAlmostEqual(self.logic.data.x_bins.sum(), 0.0) |
---|
| 92 | self.assertAlmostEqual(self.logic.data.y_bins.sum(), 0.0) |
---|
| 93 | |
---|
| 94 | def testNew1DPlot(self): |
---|
| 95 | """ |
---|
| 96 | Test how the extra shells are presented |
---|
| 97 | """ |
---|
| 98 | data = Data1D(x=[1,2,3],y=[3,4,5]) |
---|
| 99 | data.name = "boop" |
---|
[80468f6] | 100 | data.id = "poop" |
---|
[bfb5d9e] | 101 | # Condensed return data (new1DPlot only uses these fields) |
---|
| 102 | return_data = dict(x = data.x, |
---|
| 103 | y = data.y, |
---|
| 104 | model = data, |
---|
| 105 | data = data) |
---|
| 106 | # return_data = (data.x,data.y, 7, None, None, |
---|
| 107 | # 0, True, 0.0, 1, data, |
---|
| 108 | # data, False, None, |
---|
| 109 | # None, None, None, |
---|
| 110 | # None, None) |
---|
[7248d75d] | 111 | |
---|
[2155824] | 112 | new_plot = self.logic.new1DPlot(return_data=return_data, tab_id=0) |
---|
[7248d75d] | 113 | |
---|
[6fd4e36] | 114 | self.assertIsInstance(new_plot, Data1D) |
---|
| 115 | self.assertFalse(new_plot.is_data) |
---|
| 116 | self.assertEqual(new_plot.dy.size, 3) |
---|
[80468f6] | 117 | self.assertEqual(new_plot.title, "boop [poop]") |
---|
| 118 | self.assertEqual(new_plot.name, "boop [poop]") |
---|
[7248d75d] | 119 | |
---|
| 120 | def testNew2DPlot(self): |
---|
| 121 | """ |
---|
| 122 | Test the additional rows added by modifying the shells combobox |
---|
| 123 | """ |
---|
[351b53e] | 124 | x_0 = 2.0*numpy.ones(25) |
---|
| 125 | dx_0 = 0.5*numpy.ones(25) |
---|
| 126 | qx_0 = numpy.arange(25) |
---|
| 127 | qy_0 = numpy.arange(25) |
---|
| 128 | mask_0 = numpy.zeros(25) |
---|
| 129 | dqx_0 = numpy.arange(25)/100 |
---|
| 130 | dqy_0 = numpy.arange(25)/100 |
---|
| 131 | q_0 = numpy.sqrt(qx_0 * qx_0 + qy_0 * qy_0) |
---|
| 132 | |
---|
| 133 | data = Data2D(image=x_0, err_image=dx_0, qx_data=qx_0, |
---|
| 134 | qy_data=qy_0, q_data=q_0, mask=mask_0, |
---|
| 135 | dqx_data=dqx_0, dqy_data=dqy_0) |
---|
[7248d75d] | 136 | |
---|
| 137 | data.name = "boop" |
---|
| 138 | data.ymin = numpy.amin(q_0) |
---|
| 139 | data.ymax = numpy.amax(q_0) |
---|
| 140 | data.xmin = numpy.amin(x_0) |
---|
| 141 | data.xmax = numpy.amax(x_0) |
---|
| 142 | self.logic.data = data |
---|
| 143 | |
---|
| 144 | qmin, qmax, npts = self.logic.computeDataRange() |
---|
| 145 | |
---|
[bfb5d9e] | 146 | # Condensed return data (new2DPlot only uses these fields) |
---|
| 147 | return_data = dict(image = x_0, |
---|
| 148 | data = data, |
---|
| 149 | page_id = 7, |
---|
| 150 | model = data) |
---|
| 151 | # return_data = (x_0, data, 7, data, None, |
---|
| 152 | # True, 0.0, 1, 0, qmin, qmax, |
---|
| 153 | # 0.1, False, None) |
---|
[7248d75d] | 154 | |
---|
[6fd4e36] | 155 | new_plot = self.logic.new2DPlot(return_data=return_data) |
---|
[7248d75d] | 156 | |
---|
[6fd4e36] | 157 | self.assertIsInstance(new_plot, Data2D) |
---|
| 158 | self.assertFalse(new_plot.is_data) |
---|
| 159 | self.assertEqual(new_plot.title, "Analytical model 2D ") |
---|
| 160 | self.assertEqual(new_plot.name, "boop [boop]") |
---|
[7248d75d] | 161 | |
---|
| 162 | |
---|
| 163 | |
---|
| 164 | if __name__ == "__main__": |
---|
| 165 | unittest.main() |
---|