def test_load_full(self): """ Check the whole sequence: saving calibration data to file, loading it back from file, finding it. """ # Background data dbckg = numpy.array([1, 2, 2, 3, 4, 5, 4, 6, 9], dtype=numpy.uint16) dbckg.shape += (1, 1, 1, 1) wl_calib = 400e-9 + numpy.array(range(dbckg.shape[0])) * 10e-9 bckg = model.DataArray(dbckg, metadata={model.MD_WL_LIST: wl_calib}) # Give one DA, the correct one, so expect to get it back # Compensation data dcalib = numpy.array([1, 1.3, 2, 3.5, 4, 5, 0.1, 6, 9.1], dtype=numpy.float) dcalib.shape = (dcalib.shape[0], 1, 1, 1, 1) wl_calib = 400e-9 + numpy.array(range(dcalib.shape[0])) * 10e-9 calib = model.DataArray(dcalib, metadata={model.MD_WL_LIST: wl_calib}) # More DataArrays, just to make it slightly harder to find the data data1 = model.DataArray(numpy.ones((1, 1, 1, 520, 230), dtype=numpy.uint16)) data2 = model.DataArray(numpy.zeros((3, 1, 1, 520, 230), dtype=numpy.uint16)) # RGB image thumb = model.DataArray(numpy.ones((520, 230, 3), dtype=numpy.uint8)) full_coef = [data1, calib, data2] full_bckg = [data1, bckg, data2] for fmt in dataio.get_available_formats(os.O_WRONLY): exporter = dataio.get_converter(fmt) logging.info("Trying to export/import with %s", fmt) fn_coef = u"test_spec" + exporter.EXTENSIONS[0] exporter.export(fn_coef, full_coef, thumb) fn_bckg = u"test_bckg" + exporter.EXTENSIONS[0] exporter.export(fn_bckg, full_bckg, thumb) if fmt in dataio.get_available_formats(os.O_RDONLY): data_bckg = exporter.read_data(fn_bckg) ibckg = calibration.get_spectrum_data(data_bckg) data_coef = exporter.read_data(fn_coef) icoef = calibration.get_spectrum_efficiency(data_coef) numpy.testing.assert_equal(icoef, calib) numpy.testing.assert_almost_equal(icoef.metadata[model.MD_WL_LIST], calib.metadata[model.MD_WL_LIST]) numpy.testing.assert_equal(ibckg, bckg) numpy.testing.assert_almost_equal(ibckg.metadata[model.MD_WL_LIST], bckg.metadata[model.MD_WL_LIST]) try: os.remove(fn_coef) except OSError: logging.exception("Failed to delete the file %s", fn_coef) try: os.remove(fn_bckg) except OSError: logging.exception("Failed to delete the file %s", fn_bckg)
def test_load_full(self): """ Check the whole sequence: saving calibration data to file, loading it back from file, finding it. """ # Background data dbckg = numpy.array([1, 2, 2, 3, 4, 5, 4, 6, 9], dtype=numpy.uint16) dbckg.shape += (1, 1, 1, 1) wl_calib = 400e-9 + numpy.array(range(dbckg.shape[0])) * 10e-9 bckg = model.DataArray(dbckg, metadata={model.MD_WL_LIST: wl_calib}) # Give one DA, the correct one, so expect to get it back # Compensation data dcalib = numpy.array([1, 1.3, 2, 3.5, 4, 5, 0.1, 6, 9.1], dtype=numpy.float) dcalib.shape = (dcalib.shape[0], 1, 1, 1, 1) wl_calib = 400e-9 + numpy.array(range(dcalib.shape[0])) * 10e-9 calib = model.DataArray(dcalib, metadata={model.MD_WL_LIST: wl_calib}) # More DataArrays, just to make it slightly harder to find the data data1 = model.DataArray(numpy.ones((1, 1, 1, 520, 230), dtype=numpy.uint16)) data2 = model.DataArray(numpy.zeros((3, 1, 1, 520, 230), dtype=numpy.uint16)) # RGB image thumb = model.DataArray(numpy.ones((520, 230, 3), dtype=numpy.uint8)) full_coef = [data1, calib, data2] full_bckg = [data1, bckg, data2] for fmt in dataio.get_available_formats(os.O_WRONLY): exporter = dataio.get_converter(fmt) logging.info("Trying to export/import with %s", fmt) fn_coef = u"test_spec" + exporter.EXTENSIONS[0] exporter.export(fn_coef, full_coef, thumb) fn_bckg = u"test_bckg" + exporter.EXTENSIONS[0] exporter.export(fn_bckg, full_bckg, thumb) if fmt in dataio.get_available_formats(os.O_RDONLY): data_bckg = exporter.read_data(fn_bckg) ibckg = calibration.get_spectrum_data(data_bckg) data_coef = exporter.read_data(fn_coef) icoef = calibration.get_spectrum_efficiency(data_coef) numpy.testing.assert_equal(icoef, calib) numpy.testing.assert_almost_equal(icoef.metadata[model.MD_WL_LIST], calib.metadata[model.MD_WL_LIST]) numpy.testing.assert_equal(ibckg, bckg) numpy.testing.assert_almost_equal(ibckg.metadata[model.MD_WL_LIST], bckg.metadata[model.MD_WL_LIST]) try: os.remove(fn_coef) except OSError: logging.exception("Failed to delete the file %s", fn_coef) try: os.remove(fn_bckg) except OSError: logging.exception("Failed to delete the file %s", fn_bckg)
def test_load_compensation(self): # Compensation data dcalib = numpy.array([1, 1.3, 2, 3.5, 4, 5, 0.1, 6, 9.1], dtype=numpy.float) dcalib.shape = (dcalib.shape[0], 1, 1, 1, 1) wl_calib = 400e-9 + numpy.array(range(dcalib.shape[0])) * 10e-9 calib = model.DataArray(dcalib, metadata={model.MD_WL_LIST: wl_calib}) # Give one DA, the correct one, so expect to get it back out = calibration.get_spectrum_efficiency([calib]) numpy.testing.assert_equal(out, calib) numpy.testing.assert_almost_equal(out.metadata[model.MD_WL_LIST], calib.metadata[model.MD_WL_LIST]) # More DataArrays, just to make it slightly harder to find the data data1 = model.DataArray(numpy.ones((1, 1, 1, 520, 230), dtype=numpy.uint16)) out = calibration.get_spectrum_efficiency([data1, calib]) numpy.testing.assert_equal(out, calib) numpy.testing.assert_almost_equal(out.metadata[model.MD_WL_LIST], calib.metadata[model.MD_WL_LIST])
def test_load_full(self): """ Check the whole sequence: saving calibration data to file, loading it back from file, finding it. """ # Compensation data dcalib = numpy.array([1, 1.3, 2, 3.5, 4, 5, 0.1, 6, 9.1], dtype=numpy.float) dcalib.shape = (dcalib.shape[0], 1, 1, 1, 1) wl_calib = 400e-9 + numpy.array(range(dcalib.shape[0])) * 10e-9 calib = model.DataArray(dcalib, metadata={model.MD_WL_LIST: wl_calib}) # More DataArrays, just to make it slightly harder to find the data data1 = model.DataArray( numpy.ones((1, 1, 1, 520, 230), dtype=numpy.uint16)) data2 = model.DataArray( numpy.zeros((3, 1, 1, 520, 230), dtype=numpy.uint16)) # RGB image thumb = model.DataArray(numpy.ones((520, 230, 3), dtype=numpy.uint8)) full_data = [data1, calib, data2] for fmt in dataio.get_available_formats(): exporter = dataio.get_exporter(fmt) logging.info("Trying to export/import with %s", fmt) fn = u"test_spec" + exporter.EXTENSIONS[0] exporter.export(fn, full_data, thumb) idata = exporter.read_data(fn) icalib = calibration.get_spectrum_efficiency(idata) numpy.testing.assert_equal(icalib, calib) numpy.testing.assert_almost_equal( icalib.metadata[model.MD_WL_LIST], calib.metadata[model.MD_WL_LIST]) os.remove(fn)