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