def test_ppsd_restricted_stacks(self, state, image_path): """ Test PPSD.calculate_histogram() with restrictions to what data should be stacked. Also includes image tests. """ # set up a bogus PPSD, with fixed random psds but with real start times # of psd pieces, to facilitate testing the stack selection. ppsd = PPSD(stats=Stats(dict(sampling_rate=150)), metadata=None, db_bins=(-200, -50, 20.), period_step_octaves=1.4) # change data to nowadays used nanoseconds POSIX timestamp ppsd._times_processed = [ UTCDateTime(t)._ns for t in np.load( os.path.join(state.path, "ppsd_times_processed.npy")).tolist() ] np.random.seed(1234) ppsd._binned_psds = [ arr for arr in np.random.uniform(-200, -50, ( len(ppsd._times_processed), len(ppsd.period_bin_centers))) ] # Test callback function that selects a fixed random set of the # timestamps. Also checks that we get passed the type we expect, # which is 1D numpy ndarray of int type. def callback(t_array): assert isinstance(t_array, np.ndarray) assert t_array.shape == (len(ppsd._times_processed), ) assert np.issubdtype(t_array.dtype, np.integer) np.random.seed(1234) res = np.random.randint(0, 2, len(t_array)).astype(bool) return res # test several different sets of stack criteria, should cover # everything, even with lots of combined criteria stack_criteria_list = [ dict(starttime=UTCDateTime(2015, 3, 8), month=[2, 3, 5, 7, 8]), dict(endtime=UTCDateTime(2015, 6, 7), year=[2015], time_of_weekday=[(1, 0, 24), (2, 0, 24), (-1, 0, 11)]), dict(year=[2013, 2014, 2016, 2017], month=[2, 3, 4]), dict(month=[1, 2, 5, 6, 8], year=2015), dict(isoweek=[4, 5, 6, 13, 22, 23, 24, 44, 45]), dict(time_of_weekday=[(5, 22, 24), (6, 0, 2), (6, 22, 24)]), dict(callback=callback, month=[1, 3, 5, 7]), dict(callback=callback) ] expected_selections = np.load( os.path.join(state.path, "ppsd_stack_selections.npy")) # test every set of criteria for stack_criteria, expected_selection in zip(stack_criteria_list, expected_selections): selection_got = ppsd._stack_selection(**stack_criteria) np.testing.assert_array_equal(selection_got, expected_selection) plot_kwargs = dict(max_percentage=15, xaxis_frequency=True, period_lim=(0.01, 50)) ppsd.calculate_histogram(**stack_criteria_list[1]) fig = ppsd.plot(show=False, **plot_kwargs) fig.axes[1].set_xlim(left=fig.axes[1].get_xlim()[0] - 2) image_path_1 = image_path.parent / 'test_ppsd_restricted_stacks_1.png' with np.errstate(under='ignore'): fig.savefig(image_path_1) # test it again, checking that updating an existing plot with different # stack selection works.. # a) we start with the stack for the expected image and test that it # matches (like above): ppsd.calculate_histogram(**stack_criteria_list[1]) image_path_2 = image_path.parent / 'test_ppsd_restricted_stacks_2.png' with np.errstate(under='ignore'): fig.savefig(image_path_2) ppsd.calculate_histogram(**stack_criteria_list[1]) image_path_3 = image_path.parent / 'test_ppsd_restricted_stacks_3.png' ppsd._plot_histogram(fig=fig, draw=True) with np.errstate(under='ignore'): fig.savefig(image_path_3)
def test_ppsd_restricted_stacks(self): """ Test PPSD.calculate_histogram() with restrictions to what data should be stacked. Also includes image tests. """ # set up a bogus PPSD, with fixed random psds but with real start times # of psd pieces, to facilitate testing the stack selection. ppsd = PPSD(stats=Stats(dict(sampling_rate=150)), metadata=None, db_bins=(-200, -50, 20.), period_step_octaves=1.4) # change data to nowadays used nanoseconds POSIX timestamp ppsd._times_processed = [ UTCDateTime(t)._ns for t in np.load( os.path.join(self.path, "ppsd_times_processed.npy")).tolist() ] np.random.seed(1234) ppsd._binned_psds = [ arr for arr in np.random.uniform(-200, -50, ( len(ppsd._times_processed), len(ppsd.period_bin_centers))) ] # Test callback function that selects a fixed random set of the # timestamps. Also checks that we get passed the type we expect, # which is 1D numpy ndarray of int type. def callback(t_array): self.assertIsInstance(t_array, np.ndarray) self.assertEqual(t_array.shape, (len(ppsd._times_processed), )) self.assertTrue(np.issubdtype(t_array.dtype, np.integer)) np.random.seed(1234) res = np.random.randint(0, 2, len(t_array)).astype(np.bool) return res # test several different sets of stack criteria, should cover # everything, even with lots of combined criteria stack_criteria_list = [ dict(starttime=UTCDateTime(2015, 3, 8), month=[2, 3, 5, 7, 8]), dict(endtime=UTCDateTime(2015, 6, 7), year=[2015], time_of_weekday=[(1, 0, 24), (2, 0, 24), (-1, 0, 11)]), dict(year=[2013, 2014, 2016, 2017], month=[2, 3, 4]), dict(month=[1, 2, 5, 6, 8], year=2015), dict(isoweek=[4, 5, 6, 13, 22, 23, 24, 44, 45]), dict(time_of_weekday=[(5, 22, 24), (6, 0, 2), (6, 22, 24)]), dict(callback=callback, month=[1, 3, 5, 7]), dict(callback=callback) ] expected_selections = np.load( os.path.join(self.path, "ppsd_stack_selections.npy")) # test every set of criteria for stack_criteria, expected_selection in zip(stack_criteria_list, expected_selections): selection_got = ppsd._stack_selection(**stack_criteria) np.testing.assert_array_equal(selection_got, expected_selection) # test one particular selection as an image test plot_kwargs = dict(max_percentage=15, xaxis_frequency=True, period_lim=(0.01, 50)) ppsd.calculate_histogram(**stack_criteria_list[1]) with ImageComparison(self.path_images, 'ppsd_restricted_stack.png', reltol=1.5) as ic: fig = ppsd.plot(show=False, **plot_kwargs) # some matplotlib/Python version combinations lack the left-most # tick/label "Jan 2015". Try to circumvent and get the (otherwise # OK) test by changing the left x limit a bit further out (by two # days, axis is in mpl days). See e.g. # https://tests.obspy.org/30657/#1 fig.axes[1].set_xlim(left=fig.axes[1].get_xlim()[0] - 2) with np.errstate(under='ignore'): fig.savefig(ic.name) # test it again, checking that updating an existing plot with different # stack selection works.. # a) we start with the stack for the expected image and test that it # matches (like above): ppsd.calculate_histogram(**stack_criteria_list[1]) with ImageComparison(self.path_images, 'ppsd_restricted_stack.png', reltol=1.5, plt_close_all_exit=False) as ic: fig = ppsd.plot(show=False, **plot_kwargs) # some matplotlib/Python version combinations lack the left-most # tick/label "Jan 2015". Try to circumvent and get the (otherwise # OK) test by changing the left x limit a bit further out (by two # days, axis is in mpl days). See e.g. # https://tests.obspy.org/30657/#1 fig.axes[1].set_xlim(left=fig.axes[1].get_xlim()[0] - 2) with np.errstate(under='ignore'): fig.savefig(ic.name) # b) now reuse figure and set the histogram with a different stack, # image test should fail: ppsd.calculate_histogram(**stack_criteria_list[3]) try: with ImageComparison(self.path_images, 'ppsd_restricted_stack.png', adjust_tolerance=False, plt_close_all_enter=False, plt_close_all_exit=False) as ic: # rms of the valid comparison above is ~31, # rms of the invalid comparison we test here is ~36 if MATPLOTLIB_VERSION == [1, 1, 1]: ic.tol = 33 ppsd._plot_histogram(fig=fig, draw=True) with np.errstate(under='ignore'): fig.savefig(ic.name) except ImageComparisonException: pass else: msg = "Expected ImageComparisonException was not raised." self.fail(msg) # c) now reuse figure and set the original histogram stack again, # image test should pass agin: ppsd.calculate_histogram(**stack_criteria_list[1]) with ImageComparison(self.path_images, 'ppsd_restricted_stack.png', reltol=1.5, plt_close_all_enter=False) as ic: ppsd._plot_histogram(fig=fig, draw=True) with np.errstate(under='ignore'): fig.savefig(ic.name)
def test_ppsd_restricted_stacks(self): """ Test PPSD.calculate_histogram() with restrictions to what data should be stacked. Also includes image tests. """ # set up a bogus PPSD, with fixed random psds but with real start times # of psd pieces, to facilitate testing the stack selection. ppsd = PPSD(stats=Stats(dict(sampling_rate=150)), metadata=None, db_bins=(-200, -50, 20.), period_step_octaves=1.4) ppsd._times_processed = np.load( os.path.join(self.path, "ppsd_times_processed.npy")).tolist() np.random.seed(1234) ppsd._binned_psds = [ arr for arr in np.random.uniform( -200, -50, (len(ppsd._times_processed), len(ppsd.period_bin_centers)))] # Test callback function that selects a fixed random set of the # timestamps. Also checks that we get passed the type we expect, # which is 1D numpy ndarray of float type. def callback(t_array): self.assertIsInstance(t_array, np.ndarray) self.assertEqual(t_array.shape, (len(ppsd._times_processed),)) self.assertEqual(t_array.dtype, np.float64) np.random.seed(1234) res = np.random.randint(0, 2, len(t_array)).astype(np.bool) return res # test several different sets of stack criteria, should cover # everything, even with lots of combined criteria stack_criteria_list = [ dict(starttime=UTCDateTime(2015, 3, 8), month=[2, 3, 5, 7, 8]), dict(endtime=UTCDateTime(2015, 6, 7), year=[2015], time_of_weekday=[(1, 0, 24), (2, 0, 24), (-1, 0, 11)]), dict(year=[2013, 2014, 2016, 2017], month=[2, 3, 4]), dict(month=[1, 2, 5, 6, 8], year=2015), dict(isoweek=[4, 5, 6, 13, 22, 23, 24, 44, 45]), dict(time_of_weekday=[(5, 22, 24), (6, 0, 2), (6, 22, 24)]), dict(callback=callback, month=[1, 3, 5, 7]), dict(callback=callback)] expected_selections = np.load( os.path.join(self.path, "ppsd_stack_selections.npy")) # test every set of criteria for stack_criteria, expected_selection in zip( stack_criteria_list, expected_selections): selection_got = ppsd._stack_selection(**stack_criteria) np.testing.assert_array_equal(selection_got, expected_selection) # test one particular selection as an image test plot_kwargs = dict(max_percentage=15, xaxis_frequency=True, period_lim=(0.01, 50)) ppsd.calculate_histogram(**stack_criteria_list[1]) with ImageComparison(self.path_images, 'ppsd_restricted_stack.png', reltol=1.5) as ic: fig = ppsd.plot(show=False, **plot_kwargs) # some matplotlib/Python version combinations lack the left-most # tick/label "Jan 2015". Try to circumvent and get the (otherwise # OK) test by changing the left x limit a bit further out (by two # days, axis is in mpl days). See e.g. # https://tests.obspy.org/30657/#1 fig.axes[1].set_xlim(left=fig.axes[1].get_xlim()[0] - 2) with np.errstate(under='ignore'): fig.savefig(ic.name) # test it again, checking that updating an existing plot with different # stack selection works.. # a) we start with the stack for the expected image and test that it # matches (like above): ppsd.calculate_histogram(**stack_criteria_list[1]) with ImageComparison(self.path_images, 'ppsd_restricted_stack.png', reltol=1.5, plt_close_all_exit=False) as ic: fig = ppsd.plot(show=False, **plot_kwargs) # some matplotlib/Python version combinations lack the left-most # tick/label "Jan 2015". Try to circumvent and get the (otherwise # OK) test by changing the left x limit a bit further out (by two # days, axis is in mpl days). See e.g. # https://tests.obspy.org/30657/#1 fig.axes[1].set_xlim(left=fig.axes[1].get_xlim()[0] - 2) with np.errstate(under='ignore'): fig.savefig(ic.name) # b) now reuse figure and set the histogram with a different stack, # image test should fail: ppsd.calculate_histogram(**stack_criteria_list[3]) try: with ImageComparison(self.path_images, 'ppsd_restricted_stack.png', adjust_tolerance=False, plt_close_all_enter=False, plt_close_all_exit=False) as ic: # rms of the valid comparison above is ~31, # rms of the invalid comparison we test here is ~36 if MATPLOTLIB_VERSION == [1, 1, 1]: ic.tol = 33 ppsd._plot_histogram(fig=fig, draw=True) with np.errstate(under='ignore'): fig.savefig(ic.name) except ImageComparisonException: pass else: msg = "Expected ImageComparisonException was not raised." self.fail(msg) # c) now reuse figure and set the original histogram stack again, # image test should pass agin: ppsd.calculate_histogram(**stack_criteria_list[1]) with ImageComparison(self.path_images, 'ppsd_restricted_stack.png', reltol=1.5, plt_close_all_enter=False) as ic: ppsd._plot_histogram(fig=fig, draw=True) with np.errstate(under='ignore'): fig.savefig(ic.name)