[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" |
---|
| 100 | return_data = (data.x,data.y, 7, None, None, |
---|
| 101 | 0, True, 0.0, 1, data, |
---|
| 102 | data, False, None) |
---|
| 103 | |
---|
[2155824] | 104 | new_plot = self.logic.new1DPlot(return_data=return_data, tab_id=0) |
---|
[7248d75d] | 105 | |
---|
[6fd4e36] | 106 | self.assertIsInstance(new_plot, Data1D) |
---|
| 107 | self.assertFalse(new_plot.is_data) |
---|
| 108 | self.assertEqual(new_plot.dy.size, 3) |
---|
[0268aed] | 109 | self.assertEqual(new_plot.title, "boop [boop]") |
---|
[6fd4e36] | 110 | self.assertEqual(new_plot.name, "boop [boop]") |
---|
[7248d75d] | 111 | |
---|
| 112 | def testNew2DPlot(self): |
---|
| 113 | """ |
---|
| 114 | Test the additional rows added by modifying the shells combobox |
---|
| 115 | """ |
---|
[351b53e] | 116 | x_0 = 2.0*numpy.ones(25) |
---|
| 117 | dx_0 = 0.5*numpy.ones(25) |
---|
| 118 | qx_0 = numpy.arange(25) |
---|
| 119 | qy_0 = numpy.arange(25) |
---|
| 120 | mask_0 = numpy.zeros(25) |
---|
| 121 | dqx_0 = numpy.arange(25)/100 |
---|
| 122 | dqy_0 = numpy.arange(25)/100 |
---|
| 123 | q_0 = numpy.sqrt(qx_0 * qx_0 + qy_0 * qy_0) |
---|
| 124 | |
---|
| 125 | data = Data2D(image=x_0, err_image=dx_0, qx_data=qx_0, |
---|
| 126 | qy_data=qy_0, q_data=q_0, mask=mask_0, |
---|
| 127 | dqx_data=dqx_0, dqy_data=dqy_0) |
---|
[7248d75d] | 128 | |
---|
| 129 | data.name = "boop" |
---|
| 130 | data.ymin = numpy.amin(q_0) |
---|
| 131 | data.ymax = numpy.amax(q_0) |
---|
| 132 | data.xmin = numpy.amin(x_0) |
---|
| 133 | data.xmax = numpy.amax(x_0) |
---|
| 134 | self.logic.data = data |
---|
| 135 | |
---|
| 136 | qmin, qmax, npts = self.logic.computeDataRange() |
---|
| 137 | |
---|
| 138 | return_data = (x_0, data, 7, data, None, |
---|
| 139 | True, 0.0, 1, 0, qmin, qmax, |
---|
| 140 | 0.1, False, None) |
---|
| 141 | |
---|
[6fd4e36] | 142 | new_plot = self.logic.new2DPlot(return_data=return_data) |
---|
[7248d75d] | 143 | |
---|
[6fd4e36] | 144 | self.assertIsInstance(new_plot, Data2D) |
---|
| 145 | self.assertFalse(new_plot.is_data) |
---|
| 146 | self.assertEqual(new_plot.title, "Analytical model 2D ") |
---|
| 147 | self.assertEqual(new_plot.name, "boop [boop]") |
---|
[7248d75d] | 148 | |
---|
| 149 | |
---|
| 150 | |
---|
| 151 | if __name__ == "__main__": |
---|
| 152 | unittest.main() |
---|