@@ -291,166 +291,166 @@ def savefig(self, ax, name):
291291# gc.collect()
292292#
293293#
294- # class TestPlotDistributionTests(TestPlots):
295- #
296- # def setUp(self):
297- # self.result_obs_scalar = MagicMock()
298- # self.result_obs_scalar.test_distribution = numpy.random.normal(0, 1, 1000)
299- # self.result_obs_scalar.observed_statistic = numpy.random.rand(1)[0]
300- #
301- # self.result_obs_array = MagicMock()
302- # self.result_obs_array.test_distribution = numpy.random.normal(0, 1, 1000)
303- # self.result_obs_array.observed_statistic = numpy.random.normal(0, 1, 100)
304- #
305- # self.result_nan = MagicMock()
306- # self.result_nan.test_distribution = numpy.random.normal(0, 1, 1000)
307- # self.result_nan.observed_statistic = -numpy.inf
308- #
309- # # Example data for testing
310- # n_test = os.path.join(
311- # self.artifacts, "example_csep2_forecasts", "Results", "catalog_n_test.json"
312- # )
313- # s_test = os.path.join(
314- # self.artifacts, "example_csep2_forecasts", "Results", "catalog_s_test.json"
315- # )
316- # m_test = os.path.join(
317- # self.artifacts, "example_csep2_forecasts", "Results", "catalog_m_test.json"
318- # )
319- # l_test = os.path.join(
320- # self.artifacts, "example_csep2_forecasts", "Results", "catalog_l_test.json"
321- # )
322- #
323- # with open(n_test, "r") as fp:
324- # self.n_test = CatalogNumberTestResult.from_dict(json.load(fp))
325- # with open(s_test, "r") as fp:
326- # self.s_test = CatalogSpatialTestResult.from_dict(json.load(fp))
327- # with open(m_test, "r") as fp:
328- # self.m_test = CatalogMagnitudeTestResult.from_dict(json.load(fp))
329- # with open(l_test, "r") as fp:
330- # self.l_test = CatalogPseudolikelihoodTestResult.from_dict(json.load(fp))
331- #
332- # def test_plot_dist_test_with_scalar_observation_default(self):
333- # ax = plot_distribution_test(
334- # evaluation_result=self.result_obs_scalar,
335- # show=show_plots,
336- # )
337- #
338- # # Check if a vertical line was drawn for the scalar observation
339- # lines = [line for line in ax.get_lines() if line.get_linestyle() == "--"]
340- # self.assertEqual(len(lines), 1) # Expect one vertical line
341- # self.assertEqual(lines[0].get_xdata()[0], self.result_obs_scalar.observed_statistic)
342- #
343- # def test_plot_dist_test_with_scalar_observation_w_labels(self):
344- # ax = plot_distribution_test(
345- # evaluation_result=self.result_obs_scalar,
346- # xlabel="Test X Label",
347- # ylabel="Test Y Label",
348- # title="Test Title",
349- # show=show_plots,
350- # )
351- #
352- # # Check if a vertical line was drawn for the scalar observation
353- # lines = [line for line in ax.get_lines() if line.get_linestyle() == "--"]
354- # self.assertEqual(len(lines), 1) # Expect one vertical line
355- # self.assertEqual(lines[0].get_xdata()[0], self.result_obs_scalar.observed_statistic)
356- #
357- # def test_plot_dist_test_with_array_observation(self):
358- # ax = plot_distribution_test(
359- # evaluation_result=self.result_obs_array,
360- # alpha=0.5,
361- # show=show_plots,
362- # )
363- # bars = ax.patches
364- # self.assertTrue(
365- # all(bar.get_alpha() == 0.5 for bar in bars),
366- # "Alpha transparency not set correctly for bars",
367- # )
368- #
369- # def test_plot_dist_test_with_percentile_shading(self):
370- # ax = plot_distribution_test(
371- # evaluation_result=self.result_obs_scalar,
372- # percentile=60,
373- # show=show_plots,
374- # )
375- # expected_red = (1.0, 0.0, 0.0)
376- # red_patches = []
377- # for patch_ in ax.patches:
378- # facecolor = patch_.get_facecolor()[:3] # Get RGB, ignore alpha
379- # if all(abs(facecolor[i] - expected_red[i]) < 0.01 for i in range(3)):
380- # red_patches.append(patch_)
381- # self.assertGreater(
382- # len(red_patches),
383- # 0,
384- # "Expected some patches to be colored red for percentile shading",
385- # )
386- #
387- # def test_plot_dist_test_with_annotation(self):
388- # annotation_text = "Test Annotation"
389- # ax = plot_distribution_test(
390- # evaluation_result=self.result_obs_scalar,
391- # xlabel="Test X Label",
392- # ylabel="Test Y Label",
393- # title="Test Title",
394- # annotation_text=annotation_text,
395- # annotation_xy=(0.5, 0.5),
396- # annotation_fontsize=12,
397- # show=show_plots,
398- # )
399- # annotations = ax.texts
400- # self.assertEqual(len(annotations), 1)
401- # self.assertEqual(annotations[0].get_text(), annotation_text)
402- #
403- # def test_plot_dist_test_xlim(self):
404- # xlim = (-5, 5)
405- # ax = plot_distribution_test(
406- # evaluation_result=self.result_obs_scalar,
407- # percentile=95,
408- # xlim=xlim,
409- # show=show_plots,
410- # )
411- # self.savefig(ax, "plot_dist_test_xlims.png")
412- # self.assertEqual(ax.get_xlim(), xlim)
413- #
414- # def test_plot_dist_test_autoxlim_nan(self):
415- #
416- # ax = plot_distribution_test(
417- # evaluation_result=self.result_nan,
418- # percentile=95,
419- # show=show_plots,
420- # )
421- # self.savefig(ax, "plot_dist_test_xlims_inf.png")
422- #
423- # def test_plot_n_test(self):
424- # ax = plot_distribution_test(
425- # self.n_test,
426- # show=show_plots,
427- # )
428- # self.savefig(ax, "plot_n_test.png")
429- #
430- # def test_plot_m_test(self):
431- # ax = plot_distribution_test(
432- # self.m_test,
433- # show=show_plots,
434- # )
435- # self.savefig(ax, "plot_m_test.png")
436- #
437- # def test_plot_s_test(self):
438- # ax = plot_distribution_test(
439- # self.s_test,
440- # show=show_plots,
441- # )
442- # self.savefig(ax, "plot_s_test.png")
443- #
444- # def test_plot_l_test(self):
445- # ax = plot_distribution_test(
446- # self.l_test,
447- # show=show_plots,
448- # )
449- # self.savefig(ax, "plot_l_test.png")
450- #
451- # def tearDown(self):
452- # plt.close("all")
453- # gc.collect()
294+ class TestPlotDistributionTests (TestPlots ):
295+
296+ def setUp (self ):
297+ self .result_obs_scalar = MagicMock ()
298+ self .result_obs_scalar .test_distribution = numpy .random .normal (0 , 1 , 1000 )
299+ self .result_obs_scalar .observed_statistic = numpy .random .rand (1 )[0 ]
300+
301+ self .result_obs_array = MagicMock ()
302+ self .result_obs_array .test_distribution = numpy .random .normal (0 , 1 , 1000 )
303+ self .result_obs_array .observed_statistic = numpy .random .normal (0 , 1 , 100 )
304+
305+ self .result_nan = MagicMock ()
306+ self .result_nan .test_distribution = numpy .random .normal (0 , 1 , 1000 )
307+ self .result_nan .observed_statistic = - numpy .inf
308+
309+ # Example data for testing
310+ n_test = os .path .join (
311+ self .artifacts , "example_csep2_forecasts" , "Results" , "catalog_n_test.json"
312+ )
313+ s_test = os .path .join (
314+ self .artifacts , "example_csep2_forecasts" , "Results" , "catalog_s_test.json"
315+ )
316+ m_test = os .path .join (
317+ self .artifacts , "example_csep2_forecasts" , "Results" , "catalog_m_test.json"
318+ )
319+ l_test = os .path .join (
320+ self .artifacts , "example_csep2_forecasts" , "Results" , "catalog_l_test.json"
321+ )
322+
323+ with open (n_test , "r" ) as fp :
324+ self .n_test = CatalogNumberTestResult .from_dict (json .load (fp ))
325+ with open (s_test , "r" ) as fp :
326+ self .s_test = CatalogSpatialTestResult .from_dict (json .load (fp ))
327+ with open (m_test , "r" ) as fp :
328+ self .m_test = CatalogMagnitudeTestResult .from_dict (json .load (fp ))
329+ with open (l_test , "r" ) as fp :
330+ self .l_test = CatalogPseudolikelihoodTestResult .from_dict (json .load (fp ))
331+
332+ def test_plot_dist_test_with_scalar_observation_default (self ):
333+ ax = plot_distribution_test (
334+ evaluation_result = self .result_obs_scalar ,
335+ show = show_plots ,
336+ )
337+
338+ # Check if a vertical line was drawn for the scalar observation
339+ lines = [line for line in ax .get_lines () if line .get_linestyle () == "--" ]
340+ self .assertEqual (len (lines ), 1 ) # Expect one vertical line
341+ self .assertEqual (lines [0 ].get_xdata ()[0 ], self .result_obs_scalar .observed_statistic )
342+
343+ def test_plot_dist_test_with_scalar_observation_w_labels (self ):
344+ ax = plot_distribution_test (
345+ evaluation_result = self .result_obs_scalar ,
346+ xlabel = "Test X Label" ,
347+ ylabel = "Test Y Label" ,
348+ title = "Test Title" ,
349+ show = show_plots ,
350+ )
351+
352+ # Check if a vertical line was drawn for the scalar observation
353+ lines = [line for line in ax .get_lines () if line .get_linestyle () == "--" ]
354+ self .assertEqual (len (lines ), 1 ) # Expect one vertical line
355+ self .assertEqual (lines [0 ].get_xdata ()[0 ], self .result_obs_scalar .observed_statistic )
356+
357+ def test_plot_dist_test_with_array_observation (self ):
358+ ax = plot_distribution_test (
359+ evaluation_result = self .result_obs_array ,
360+ alpha = 0.5 ,
361+ show = show_plots ,
362+ )
363+ bars = ax .patches
364+ self .assertTrue (
365+ all (bar .get_alpha () == 0.5 for bar in bars ),
366+ "Alpha transparency not set correctly for bars" ,
367+ )
368+
369+ def test_plot_dist_test_with_percentile_shading (self ):
370+ ax = plot_distribution_test (
371+ evaluation_result = self .result_obs_scalar ,
372+ percentile = 60 ,
373+ show = show_plots ,
374+ )
375+ expected_red = (1.0 , 0.0 , 0.0 )
376+ red_patches = []
377+ for patch_ in ax .patches :
378+ facecolor = patch_ .get_facecolor ()[:3 ] # Get RGB, ignore alpha
379+ if all (abs (facecolor [i ] - expected_red [i ]) < 0.01 for i in range (3 )):
380+ red_patches .append (patch_ )
381+ self .assertGreater (
382+ len (red_patches ),
383+ 0 ,
384+ "Expected some patches to be colored red for percentile shading" ,
385+ )
386+
387+ def test_plot_dist_test_with_annotation (self ):
388+ annotation_text = "Test Annotation"
389+ ax = plot_distribution_test (
390+ evaluation_result = self .result_obs_scalar ,
391+ xlabel = "Test X Label" ,
392+ ylabel = "Test Y Label" ,
393+ title = "Test Title" ,
394+ annotation_text = annotation_text ,
395+ annotation_xy = (0.5 , 0.5 ),
396+ annotation_fontsize = 12 ,
397+ show = show_plots ,
398+ )
399+ annotations = ax .texts
400+ self .assertEqual (len (annotations ), 1 )
401+ self .assertEqual (annotations [0 ].get_text (), annotation_text )
402+
403+ def test_plot_dist_test_xlim (self ):
404+ xlim = (- 5 , 5 )
405+ ax = plot_distribution_test (
406+ evaluation_result = self .result_obs_scalar ,
407+ percentile = 95 ,
408+ xlim = xlim ,
409+ show = show_plots ,
410+ )
411+ self .savefig (ax , "plot_dist_test_xlims.png" )
412+ self .assertEqual (ax .get_xlim (), xlim )
413+
414+ def test_plot_dist_test_autoxlim_nan (self ):
415+
416+ ax = plot_distribution_test (
417+ evaluation_result = self .result_nan ,
418+ percentile = 95 ,
419+ show = show_plots ,
420+ )
421+ self .savefig (ax , "plot_dist_test_xlims_inf.png" )
422+
423+ def test_plot_n_test (self ):
424+ ax = plot_distribution_test (
425+ self .n_test ,
426+ show = show_plots ,
427+ )
428+ self .savefig (ax , "plot_n_test.png" )
429+
430+ def test_plot_m_test (self ):
431+ ax = plot_distribution_test (
432+ self .m_test ,
433+ show = show_plots ,
434+ )
435+ self .savefig (ax , "plot_m_test.png" )
436+
437+ def test_plot_s_test (self ):
438+ ax = plot_distribution_test (
439+ self .s_test ,
440+ show = show_plots ,
441+ )
442+ self .savefig (ax , "plot_s_test.png" )
443+
444+ def test_plot_l_test (self ):
445+ ax = plot_distribution_test (
446+ self .l_test ,
447+ show = show_plots ,
448+ )
449+ self .savefig (ax , "plot_l_test.png" )
450+
451+ def tearDown (self ):
452+ plt .close ("all" )
453+ gc .collect ()
454454
455455
456456class TestPlotCalibrationTest (TestPlots ):
0 commit comments