def test_compatible_metadata_merge(self): a = metaarray.Spectrum(np.arange(1000), Spectrum_metadata) b = metaarray.Spectrum(np.arange(1000), Spectrum_metadata) c = a * b self.assertTrue(c.metadata.df == Spectrum_metadata["df"]) self.assertTrue(c.metadata.f_low == Spectrum_metadata["f_low"]) self.assertTrue(c.metadata.comments == Spectrum_metadata["comments"]) self.assertTrue(c.metadata.segments == Spectrum_metadata["segments"])
def test_incompatible_metadata_merge(self): a = metaarray.Spectrum(np.arange(1000), Spectrum_metadata) b = metaarray.Spectrum(np.arange(1000), Spectrum_metadata) b.metadata.df = 2 * a.metadata.df self.assertRaises(AssertionError, operator.mul, a, b) b.metadata.df = a.metadata.df b.metadata.f_low = a.metadata.f_low + 10 self.assertRaises(AssertionError, operator.mul, a, b) b.metadata.f_low = a.metadata.f_low
def test_multiply_correctness(self): a = np.random.random(1000) b = np.random.random(1000) a_Spectrum = metaarray.Spectrum(a, Spectrum_metadata) b_Spectrum = metaarray.Spectrum(b, Spectrum_metadata) # multiply self.assertTrue((a * b == (a_Spectrum * b_Spectrum).A).all()) self.assertTrue((a * b == (a * b_Spectrum).A).all()) self.assertTrue((a * b == (a_Spectrum * b).A).all()) # in-place multiply c = np.array(a, copy=True) c *= b_Spectrum self.assertTrue((a * b == c).all()) d = metaarray.Spectrum(a_Spectrum, copy=True) d *= b self.assertTrue((a * b == d.A).all())
def test_multiply_type(self): """ See that Spectrum metadata are retained by a multiplication. """ a = np.random.random(1000) b = np.random.random(1000) a_Spectrum = metaarray.Spectrum(a, Spectrum_metadata) b_Spectrum = metaarray.Spectrum(b, Spectrum_metadata) # multiply self.assertTrue(isinstance(a_Spectrum * b_Spectrum, metaarray.Spectrum)) self.assertTrue(isinstance(a * b_Spectrum, metaarray.Spectrum)) self.assertTrue(isinstance(a_Spectrum * b, metaarray.Spectrum)) # in-place multiply c = np.array(a, copy=True) c *= b_Spectrum self.assertTrue(isinstance(c, np.ndarray)) d = metaarray.Spectrum(a_Spectrum, copy=True) d *= b self.assertTrue(isinstance(d, metaarray.Spectrum))
def test_identity(self): """ See that the Spectrum wrapping doesn't touch array data """ arr = np.arange(100, dtype=np.float32) spec = metaarray.Spectrum(arr, Spectrum_metadata) self.assertTrue((arr == spec.A).all()) arr = np.arange(100, dtype=np.float64) spec = metaarray.Spectrum(arr, Spectrum_metadata) self.assertTrue((arr == spec.A).all()) arr = np.ones(100, dtype=np.bool8) spec = metaarray.Spectrum(arr, Spectrum_metadata) self.assertTrue((arr == spec.A).all()) arr = np.arange(100, dtype=np.int32) spec = metaarray.Spectrum(arr, Spectrum_metadata) self.assertTrue((arr == spec.A).all()) arr = np.arange(100, dtype=np.int64) spec = metaarray.Spectrum(arr, Spectrum_metadata) self.assertTrue((arr == spec.A).all()) arr = np.arange(100, dtype=np.int64) spec = metaarray.Spectrum(arr, Spectrum_metadata) self.assertTrue((arr == spec.A).all())