def test_wcs(): rss = MaNGARSS.from_plateifu(7815, 3702, directory_path=remote_data_file()) # Unrestricted x, y = rss.mean_sky_coordinates() methods = available_reduction_assessments() i = numpy.where([m['key'] == 'SNRG' for m in methods])[0] assert len(i) == 1, 'Could not find correct reduction assessment definition.' # Weighted by the g-band _x, _y = rss.mean_sky_coordinates(response_func=methods[i[0]]['response_func']) assert numpy.ma.amax(x-_x) - numpy.ma.amin(x-_x) > 0, 'Should be different' assert numpy.ma.amax(y-_y) - numpy.ma.amin(y-_y) > 0, 'Should be different' # Find a cluster of dithers d = numpy.ma.sqrt(numpy.square(_x) + numpy.square(_y)) srt = numpy.argsort(d) theta = numpy.arctan2(-_y, _x) indx = theta[srt[:10]] < -2 assert numpy.sum(indx) == 5, 'Should find close fiber positions' indx = srt[:10][indx] bin_indx = numpy.full(rss.nspec, -1, dtype=int) bin_indx[indx] = 0 bins, area = rss.binned_on_sky_area(bin_indx, response_func=methods[i[0]]['response_func']) assert numpy.array_equal(bins, [0]), 'Should only be one bin' try: import shapely except: # Could not import shapely for proper calcultion, so test the stupid calculation assert area[0] == numpy.sum(rss.area[bin_indx > -1]), \ 'Stupid calculation is just the sum of the fiber area' else: # Check the proper calculation assert area[0] < numpy.sum(rss.area[bin_indx > -1]), \ 'Area should be substantially smaller than the stupid calculation yields.'
def test_stats(): rss = MaNGARSS.from_plateifu(7815, 3702, directory_path=remote_data_file()) methods = available_reduction_assessments() i = numpy.where([m['key'] == 'SNRG' for m in methods])[0] assert len(i) == 1, 'Could not find correct reduction assessment definition.' cenwave = rss.central_wavelength(response_func=methods[i[0]]['response_func']) assert numpy.isclose(cenwave, 4686.2), 'Central wavelength calculation changed' sig, var, snr = rss.flux_stats(response_func=methods[i[0]]['response_func']) assert sig.shape == (rss.nspec,), 'Should be one measurement per spectrum.' assert isinstance(sig, numpy.ma.MaskedArray), 'Expected masked arrays' assert numpy.ma.amax(snr) > 15, 'S/N changed' assert numpy.ma.median(snr) < 3, 'S/N changed' # Try it with the linear rss rss = MaNGARSS.from_plateifu(7815, 3702, directory_path=remote_data_file(), log=False) _sig, _var, _snr = rss.flux_stats(response_func=methods[i[0]]['response_func']) # TODO: Not sure why these are not closer. assert numpy.absolute(numpy.ma.median((sig-_sig)/_sig)) < 0.01, \ 'Signal should be the same to better than 1%.' assert numpy.absolute(numpy.ma.median((var-_var)/_var)) < 0.03, \ 'Variance should be the same to better than 3%.' assert numpy.absolute(numpy.ma.median((snr-_snr)/_snr)) < 0.02, \ 'S/N should be the same to better than 2%.'
def test_stats(): cube = MaNGADataCube.from_plateifu(7815, 3702, directory_path=remote_data_file()) # Create a fake bin map bin_indx = numpy.arange(cube.nspec / 4, dtype=int).reshape(cube.spatial_shape[0] // 2, cube.spatial_shape[0] // 2) bin_indx = numpy.repeat(bin_indx, 2, axis=0) bin_indx = numpy.repeat(bin_indx, 2, axis=1) # Get the bin area bins, area = cube.binned_on_sky_area(bin_indx) assert numpy.array_equal(bins, numpy.arange(cube.nspec / 4)), 'Bad bin list' assert numpy.allclose(area, 1.), 'Bad area calculation' methods = available_reduction_assessments() i = numpy.where([m['key'] == 'SNRG' for m in methods])[0] assert len( i) == 1, 'Could not find correct reduction assessment definition.' cen_wave = cube.central_wavelength( response_func=methods[i[0]]['response_func'], flag=cube.do_not_use_flags()) assert numpy.isclose(cen_wave, 4638.0), 'Central wavelength changed.' cen_wave = cube.central_wavelength(waverange=[4000, 8000], flag=cube.do_not_use_flags(), fluxwgt=True) assert numpy.isclose(cen_wave, 5895.7), 'Central wavelength changed.' cen_wave = cube.central_wavelength(waverange=[4000, 8000], flag=cube.do_not_use_flags(), per_pixel=False) assert numpy.isclose(cen_wave, 6044.9), 'Central wavelength changed.' sig, var, snr = cube.flux_stats( response_func=methods[i[0]]['response_func']) assert sig.shape == cube.spatial_shape, 'Should be shaped as a map.' assert isinstance(sig, numpy.ma.MaskedArray), 'Expected masked arrays' assert numpy.ma.amax(snr) > 60, 'S/N changed' # Try it with the linear cube cube = MaNGADataCube.from_plateifu(7815, 3702, directory_path=remote_data_file(), log=False) _sig, _var, _snr = cube.flux_stats( response_func=methods[i[0]]['response_func']) # TODO: Not sure why these are not closer. assert numpy.absolute(numpy.ma.median((sig-_sig)/_sig)) < 0.01, \ 'Signal should be the same to better than 1%.' assert numpy.absolute(numpy.ma.median((var-_var)/_var)) < 0.03, \ 'Variance should be the same to better than 3%.' assert numpy.absolute(numpy.ma.median((snr-_snr)/_snr)) < 0.02, \ 'S/N should be the same to better than 2%.'
def test_copyto(): cube = MaNGADataCube.from_plateifu(7815, 3702, directory_path=remote_data_file()) flux = cube.copy_to_array() assert not isinstance(flux, numpy.ma.MaskedArray), 'Should output normal array' assert flux.shape[0] == cube.nspec, 'Should be flattened into a 2D array.' assert flux.shape[1] == cube.nwave, 'Should be flattened into a 2D array.' # Apply a wavelength mask waverange = [5000, 7000] flux = cube.copy_to_array(waverange=waverange) indx = (cube.wave > waverange[0]) & (cube.wave < waverange[1]) assert flux.shape[1] == numpy.sum(indx), 'Wavelength range masking failed' # Find the spaxels with non-zero signal methods = available_reduction_assessments() i = numpy.where([m['key'] == 'SNRG' for m in methods])[0] assert len( i) == 1, 'Could not find correct reduction assessment definition.' sig, var, snr = cube.flux_stats( response_func=methods[i[0]]['response_func']) indx = ((sig > 0) & numpy.invert(numpy.ma.getmaskarray(sig))).data.ravel() ngood = numpy.sum(indx) # Select the spaxels with non-zero signal flux = cube.copy_to_array(waverange=waverange, select_bins=indx) assert flux.shape[0] == ngood, 'Bin selection failed' # Get the masked array flux = cube.copy_to_masked_array() assert isinstance(flux, numpy.ma.MaskedArray), 'Should output a masked array' assert flux.shape[0] == cube.nspec, 'Should be flattened into a 2D array.' assert flux.shape[1] == cube.nwave, 'Should be flattened into a 2D array.' # Select the spaxels with non-zero signal flux = cube.copy_to_masked_array(select_bins=indx) assert flux.shape[0] == ngood, 'Bin selection failed' # Try to get the inverse variance i = cube.nspec // 2 + cube.spatial_shape[1] // 2 ivar = cube.copy_to_masked_array(attr='ivar') assert ivar.shape == (cube.nspec, cube.nwave), 'Bad ivar shape' assert numpy.array_equal( cube.ivar[numpy.unravel_index(i, cube.spatial_shape)], ivar[i].data), 'Did not pull ivar data.' # Try to get the spectral resolution sres = cube.copy_to_masked_array(attr='sres') assert sres.shape == (cube.nspec, cube.nwave), 'Bad sres shape' assert numpy.array_equal( cube.sres[numpy.unravel_index(i, cube.spatial_shape)], sres[i].data), 'Did not pull sres data.'
def test_avail(): method_list = available_reduction_assessments() assert len(method_list) > 0, 'No reduction assessment methods available'