def test_sdsmooth_fail04(self): """test_sdsmooth_fail04 --- outfile exists (overwrite=False)""" shutil.copytree(self.infile, self.outfile) self.result = sdsmooth(infile=self.infile, kernel='gaussian', outfile=self.outfile, overwrite=False)
def test000(self): # testing kwidth from 1 to 5. for kwidth in range(1,6): result = sdsmooth(infile=self.infile, outfile=self.outfile, datacolumn=self.datacolumn, overwrite=True, kernel='boxcar', kwidth = kwidth) with sdutil.tbmanager(self.outfile) as tb: for irow in range(tb.nrows()): spec = tb.getcell(self.datacolumn.upper(), irow) for ipol in range(len(spec)): center = self.centers[str(irow)+str(ipol)] self._checkResult(spec[ipol], kwidth, center)
def test000(self): # testing kwidth from 1 to 5. for kwidth in range(1, 6): result = sdsmooth(infile=self.infile, outfile=self.outfile, datacolumn=self.datacolumn, overwrite=True, kernel='boxcar', kwidth=kwidth) with sdutil.tbmanager(self.outfile) as tb: for irow in range(tb.nrows()): spec = tb.getcell(self.datacolumn.upper(), irow) for ipol in range(len(spec)): center = self.centers[str(irow) + str(ipol)] self._checkResult(spec[ipol], kwidth, center)
def test_sdsmooth_fail05(self): """test_sdsmooth_fail05 --- empty outfile""" self.result = sdsmooth(infile=self.infile, kernel='gaussian', outfile='')
def test_sdsmooth_fail03(self): """test_sdsmooth_fail03 --- invalid selection (empty selection result)""" self.result = sdsmooth(infile=self.infile, kernel='gaussian', outfile=self.outfile, spw='3')
def test_sdsmooth_fail02(self): """test_sdsmooth_fail02 --- invalid kernel type""" self.result = sdsmooth(infile=self.infile, kernel='normal', outfile=self.outfile)
def test_sdsmooth_fail01(self): """test_sdsmooth_fail01 --- default parameters (raises an error)""" self.result = sdsmooth()
def test_sdsmooth_float_fail01(self): """test_sdsmooth_complex_fail01 --- non-existing data column (DATA)""" self.result = sdsmooth(infile=self.infile, outfile=self.outfile, kernel='gaussian', datacolumn='data')
def test_sdsmooth_fail06(self): """test_sdsmooth_fail06 --- invalid data column name""" self.result = sdsmooth(infile=self.infile, outfile=self.outfile, kernel='gaussian', datacolumn='spectra')
def run_test(self, sel_param, datacolumn, reindex=True): inparams = self._get_selection_string(sel_param) inparams.update(self.common_param) sdsmooth(datacolumn=datacolumn, reindex=reindex, **inparams) self._test_result(inparams["outfile"], sel_param, datacolumn)
def run_test(self, *args, **kwargs): datacol_name = self.datacolumn.upper() weight_mode = hasattr(self, 'weight_propagation') and getattr(self, 'weight_propagation') is True if kwargs.has_key('kwidth'): kwidth = kwargs['kwidth'] else: kwidth = 5 self.result = sdsmooth(infile=self.infile, outfile=self.outfile, kernel='gaussian', datacolumn=self.datacolumn, **kwargs) # sanity check self.assertIsNone(self.result, msg='The task must complete without error') self.assertTrue(os.path.exists(self.outfile), msg='Output file is not properly created.') if kwargs.has_key('spw'): spw = kwargs['spw'] else: spw = '' dd_selection = None if len(spw) == 0: expected_nrow = 2 with sdutil.tbmanager(self.infile) as tb: data_in = tb.getvarcol(datacol_name) flag_in = tb.getvarcol('FLAG') if weight_mode is True: weight_in = tb.getvarcol('WEIGHT_SPECTRUM') else: myms = gentools(['ms'])[0] a = myms.msseltoindex(self.infile, spw=spw) spw_selection = a['spw'] dd_selection = a['dd'] expected_nrow = len(spw_selection) with sdutil.tbmanager(self.infile) as tb: try: tsel = tb.query('DATA_DESC_ID IN %s'%(dd_selection.tolist())) data_in = tsel.getvarcol(datacol_name) flag_in = tsel.getvarcol('FLAG') if weight_mode is True: weight_in = tsel.getvarcol('WEIGHT_SPECTRUM') finally: tsel.close() with sdutil.tbmanager(self.outfile) as tb: nrow = tb.nrows() data_out = tb.getvarcol(datacol_name) flag_out = tb.getvarcol('FLAG') if weight_mode is True: weight_out = tb.getvarcol('WEIGHT_SPECTRUM') # verify nrow self.assertEqual(nrow, expected_nrow, msg='Number of rows mismatch (expected %s actual %s)'%(expected_nrow, nrow)) # verify data eps = 1.0e-6 for key in data_out.keys(): row_in = data_in[key] flg_in = flag_in[key] row_in[numpy.where(flg_in == True)] = 0.0 row_out = data_out[key] self.assertEqual(row_in.shape, row_out.shape, msg='Shape mismatch in row %s'%(key)) npol, nchan, _ = row_out.shape kernel_array = gaussian_kernel(nchan, kwidth) expected = numpy.convolve(row_in[0,:,0], kernel_array, mode='same') output = row_out[0,:,0] zero_index = numpy.where(numpy.abs(expected) <= eps) self.assertTrue(all(numpy.abs(output[zero_index]) < eps), msg='Failed to verify zero values: row %s'%(key)) nonzero_index= numpy.where(numpy.abs(expected) > eps) diff = numpy.abs((output[nonzero_index] - expected[nonzero_index]) / expected[nonzero_index].max()) #print diff #print output[nonzero_index] #print expected[nonzero_index] self.assertTrue(all(diff < eps), msg='Failed to verify nonzero values: row %s'%(key)) #print 'row_in', row_in[0,:,0].tolist() #print 'gaussian', kernel_array.tolist() #print 'expected', expected.tolist() #print 'result', row_out[0,:,0].tolist() # weight check if this is weight test if weight_mode is True: #print 'Weight propagation test' wgt_in = weight_in[key] wgt_out = weight_out[key] wkwidth = int(kwidth + 0.5) wkwidth += (1 if wkwidth % 2 == 0 else 0) half_width = wkwidth / 2 peak_chan = kernel_array.argmax() start_chan = peak_chan - half_width wkernel = kernel_array[start_chan:start_chan+wkwidth].copy() wkernel /= sum(wkernel) weight_expected = wgt_in.copy() for ichan in xrange(half_width, nchan-half_width): s = numpy.zeros(npol, dtype=float) for jchan in xrange(wkwidth): s += wkernel[jchan] * wkernel[jchan] / wgt_in[:,ichan-half_width+jchan,0] weight_expected[:,ichan,0] = 1.0 / s #print weight_expected[:,:10] diff = numpy.abs((wgt_out - weight_expected) / weight_expected) self.assertTrue(all(diff.flatten() < eps), msg='Failed to verify spectral weight: row %s'%(key))
def run_test(self, sel_param, datacolumn): inparams = self._get_selection_string(sel_param) inparams.update(self.common_param) sdsmooth(datacolumn=datacolumn, **inparams) self._test_result(inparams["outfile"], sel_param, datacolumn)
def run_test(self, *args, **kwargs): datacol_name = self.datacolumn.upper() weight_mode = hasattr(self, 'weight_propagation') and getattr( self, 'weight_propagation') is True if kwargs.has_key('kwidth'): kwidth = kwargs['kwidth'] else: kwidth = 5 self.result = sdsmooth(infile=self.infile, outfile=self.outfile, kernel='gaussian', datacolumn=self.datacolumn, **kwargs) # sanity check self.assertIsNone(self.result, msg='The task must complete without error') self.assertTrue(os.path.exists(self.outfile), msg='Output file is not properly created.') if kwargs.has_key('spw'): spw = kwargs['spw'] else: spw = '' dd_selection = None if len(spw) == 0: expected_nrow = 2 with sdutil.tbmanager(self.infile) as tb: data_in = tb.getvarcol(datacol_name) flag_in = tb.getvarcol('FLAG') if weight_mode is True: weight_in = tb.getvarcol('WEIGHT_SPECTRUM') else: myms = gentools(['ms'])[0] a = myms.msseltoindex(self.infile, spw=spw) spw_selection = a['spw'] dd_selection = a['dd'] expected_nrow = len(spw_selection) with sdutil.tbmanager(self.infile) as tb: try: tsel = tb.query('DATA_DESC_ID IN %s' % (dd_selection.tolist())) data_in = tsel.getvarcol(datacol_name) flag_in = tsel.getvarcol('FLAG') if weight_mode is True: weight_in = tsel.getvarcol('WEIGHT_SPECTRUM') finally: tsel.close() with sdutil.tbmanager(self.outfile) as tb: nrow = tb.nrows() data_out = tb.getvarcol(datacol_name) flag_out = tb.getvarcol('FLAG') if weight_mode is True: weight_out = tb.getvarcol('WEIGHT_SPECTRUM') # verify nrow self.assertEqual( nrow, expected_nrow, msg='Number of rows mismatch (expected %s actual %s)' % (expected_nrow, nrow)) # verify data eps = 1.0e-6 for key in data_out.keys(): row_in = data_in[key] flg_in = flag_in[key] row_in[numpy.where(flg_in == True)] = 0.0 row_out = data_out[key] self.assertEqual(row_in.shape, row_out.shape, msg='Shape mismatch in row %s' % (key)) npol, nchan, _ = row_out.shape kernel_array = gaussian_kernel(nchan, kwidth) expected = numpy.convolve(row_in[0, :, 0], kernel_array, mode='same') output = row_out[0, :, 0] zero_index = numpy.where(numpy.abs(expected) <= eps) self.assertTrue(all(numpy.abs(output[zero_index]) < eps), msg='Failed to verify zero values: row %s' % (key)) nonzero_index = numpy.where(numpy.abs(expected) > eps) diff = numpy.abs( (output[nonzero_index] - expected[nonzero_index]) / expected[nonzero_index].max()) #print diff #print output[nonzero_index] #print expected[nonzero_index] self.assertTrue(all(diff < eps), msg='Failed to verify nonzero values: row %s' % (key)) #print 'row_in', row_in[0,:,0].tolist() #print 'gaussian', kernel_array.tolist() #print 'expected', expected.tolist() #print 'result', row_out[0,:,0].tolist() # weight check if this is weight test if weight_mode is True: #print 'Weight propagation test' wgt_in = weight_in[key] wgt_out = weight_out[key] wkwidth = int(kwidth + 0.5) wkwidth += (1 if wkwidth % 2 == 0 else 0) half_width = wkwidth / 2 peak_chan = kernel_array.argmax() start_chan = peak_chan - half_width wkernel = kernel_array[start_chan:start_chan + wkwidth].copy() wkernel /= sum(wkernel) weight_expected = wgt_in.copy() for ichan in xrange(half_width, nchan - half_width): s = numpy.zeros(npol, dtype=float) for jchan in xrange(wkwidth): s += wkernel[jchan] * wkernel[ jchan] / wgt_in[:, ichan - half_width + jchan, 0] weight_expected[:, ichan, 0] = 1.0 / s #print weight_expected[:,:10] diff = numpy.abs((wgt_out - weight_expected) / weight_expected) self.assertTrue( all(diff.flatten() < eps), msg='Failed to verify spectral weight: row %s' % (key))