def make_flat_distribution(self): self.N_CRATERS = 100 cc = cst.Cratercount('') cc.diam = 10**np.linspace(.001, .999, self.N_CRATERS) cc.fraction = [1.] * (self.N_CRATERS - 2) + [.3] * 2 cc.area = 10. return cc
def test_ReadDiamFile2(self): c = textwrap.dedent(""" area = 100. crater = {diameter, fraction, lon, lat 1.1 0.3 127.3 -52.8 } """) with patch('builtins.open', mock_open(read_data=c)): cc = cst.Cratercount('test.diam') # check correctly read (lat/lon not used here) self.assertEqual(cc.diam[0], 1.1) self.assertEqual(cc.fraction[0], .3)
def construct_plot_dicts(args, c): plot = c['plot'] if type(plot) is list: plot = plot[0] #take only first plot entry as template cpl = [] if args.plot is None: return [] for d in args.plot: p = plot.copy() if cpl: # for these items: if not given, carry over from previous for k in [ 'source', 'psym', 'type', 'isochron', 'error_bars', 'colour', 'binning' ]: p[k] = cpl[-1][k] else: if not 'source' in d: sys.exit('Source not specified') for k, v in d.items(): if k in ( 'source', 'name', 'range', 'type', 'error_bars', 'hide', 'binning', 'age_left', 'display_age', 'resurf', 'resurf_showall', 'isochron', 'offset_age', ): p[k] = v elif k == 'colour': names = [n for c1, c2, n in cst.PALETTE] p[k] = decode_abbreviation(names, v, allow_ambiguous=True) elif k == 'psym': j = [i for i, e in enumerate(cst.MARKERS) if e[0] == v] if len(j) == 1: # found standard abbreviation p[k] = j[0] else: # look for arbitrary abbreviation or index names = [e[1] for e in cst.MARKERS] p[k] = decode_abbreviation(names, v, allow_ambiguous=True) p['cratercount'] = cst.Cratercount(p['source']) cpl += [p] return cpl
def test_ReadDiamFile3( self ): # buffered count (indicated by presence of 'reference_area') c = textwrap.dedent(""" crater = {diameter, reference_area 1.1 24.3 } """) with patch('builtins.open', mock_open(read_data=c)): cc = cst.Cratercount('test.diam') # check correctly read self.assertEqual(cc.diam[0], 1.1) # check initialisation self.assertEqual(cc.area, 1.) # crater densities relative to 1 km2 for buffered self.assertTrue(cc.buffered) # check derived values self.assertEqual(cc.fraction[0], 1 / 24.3)
def test_ReadSCCfile(self): c = textwrap.dedent(""" Total_area = 224669.2 <km^2> Perimeter = 1605.7 # crater_diameters: crater = {diam,lon,lat 33.58 -126.0 16.81 40.40 -124.4 16.95 } """) with patch('builtins.open', mock_open(read_data=c)): cc = cst.Cratercount('test.scc') # check correctly read self.assertTrue(np.array_equal(cc.diam, [33.58, 40.40])) self.assertEqual(cc.area, 224669.2) self.assertEqual(cc.perimeter, 1605.7) # check initialisation self.assertEqual(cc.binning, None) self.assertFalse(cc.prebinned) self.assertFalse(cc.buffered) self.assertTrue(np.array_equal(cc.fraction, [1., 1.]))
def test_ReadDiamFile1(self): c = textwrap.dedent(""" # comment area = 100. crater = {diameter 1.1 2.3 3.0 1.4 } """) with patch('builtins.open', mock_open(read_data=c)): cc = cst.Cratercount('test.diam') # check correctly read self.assertTrue(np.array_equal(cc.diam, [3., 2.3, 1.4, 1.1])) self.assertEqual(cc.area, 100.) # check initialisation self.assertEqual(cc.binning, None) self.assertFalse(cc.prebinned) self.assertFalse(cc.buffered) self.assertEqual(cc.perimeter, None) self.assertTrue(np.array_equal(cc.fraction, [1., 1., 1., 1.]))
def UpdateSettings(self, *args, **kwargs): #pass either dictionary or keywords a = {k: v for d in args for k, v in d.items()} a.update(**kwargs) for k, v in a.items(): if k in ('range', 'offset_age'): v = [float(e) for e in v] if k in ( 'error_bars', 'hide', 'age_left', 'display_age', 'resurf', 'resurf_showall', 'isochron', 'colour', 'psym', ): v = int(v) setattr(self, k, v) if not self.cratercount and self.source: self.cratercount = cst.Cratercount(self.source) if not self.source and self.cratercount: self.source = self.cratercount.filename
def test_ReadStatFile(self): f = r'd:\tmp\test.stat' c = textwrap.dedent(""" # D F(D) N_dif Error C(D) N_cum Error .0600 10 2.93E+00 9.28E-01 477 1.40E+00 6.41E-02 .0700 12 3.52E+00 1.02E+00 467 1.37E+00 6.34E-02 """) with patch('builtins.open', mock_open(read_data=c)): cc = cst.Cratercount(f) # check correctly read self.assertTrue(np.array_equal(cc.binned['d_min'], [0.06, 0.07])) self.assertTrue(np.array_equal(cc.binned['ncum'], [477, 467])) # check initialisation self.assertEqual(cc.binning, 'unknown') self.assertTrue(cc.prebinned) # check derived values - tests MakeBinGeometricMean() self.assertAlmostEqual(cc.area, 477 / 1.40e+00) # area found from C(D)/N_cum self.assertTrue( np.allclose(cc.binned['d_max'], [0.07, .07 * (.07 / .06)])) # extrapolated last bin width self.assertAlmostEqual(cc.binned['d_mean'][0], np.sqrt(.06 * .07)) self.assertAlmostEqual(cc.binned['bin_width'][0], .07 - .06)
class TestPlottingClasses(unittest.TestCase): root = cst.gm.filename(cst.__file__, 'p') file_fns = root + 'config/functions.txt' cf = cst.Chronologyfn(file_fns, 'Mars, Hartmann & Neukum (2001)') pf = cst.Productionfn(file_fns, 'Mars, Ivanov (2001)') cc = cst.Cratercount(root + 'sample/Pickering.scc') def test_Craterplotset_CreatePlotSpace(self): cps = cst.Craterplotset() cps.CreatePlotSpace() self.assertTrue(cps.fig) self.assertTrue(cps.ax) def test_Craterplot_get_data_range(self): cp = cst.Craterplot(cratercount=self.cc) cps = cst.Craterplotset(craterplot=[cp]) res = cp.get_data_range(cps) self.assertTrue(np.allclose(res, (.1, 2.7, 6.5e-4, 8.2), rtol=.05)) def test_Craterplotset_autoscale(self): cp = cst.Craterplot(cratercount=self.cc) cps = cst.Craterplotset(craterplot=[cp]) cps.autoscale() self.assertEqual(list(cps.xrange) + list(cps.yrange), [-2, 2, -5, 3]) def test_Craterplotset_summary(self): cp = cst.Craterplot(cratercount=self.cc, range=[.2, .7], type='d-fit') cps = cst.Craterplotset(cf=self.cf, pf=self.pf, craterplot=[cp]) def get_cps_summary(): with patch('sys.stdout', new_callable=io.StringIO) as m: cps.create_summary_table() table = m.getvalue().split('\n') return table[1][:145] # trim off source file # the following results are not fundamental, but verified against CraterstatsII (see demo plots): # name,area,binning,d_min,d_max,method,resurf,n,n_event,age,age-,age+,a0,a0-,a0+,N(1) self.assertEqual( get_cps_summary(), 'Pickering 3036.6 pseudo-log 0.2 0.7 d-fit 0 313.0 313 0.668 0.613 0.722 -3.488 -3.525 -3.453 3.25e-04' ) cp.UpdateSettings(binning='10/decade') self.assertEqual( get_cps_summary(), 'Pickering 3036.6 10/decade 0.2 0.79 d-fit 0 313.0 313 0.645 0.593 0.697 -3.503 -3.539 -3.469 3.14e-04' ) cp.UpdateSettings(type='c-fit', resurf=1, binning='pseudo-log') self.assertEqual( get_cps_summary(), 'Pickering 3036.6 pseudo-log 0.2 0.7 c-fit 1 313.0 313 0.691 0.653 0.729 -3.472 -3.497 -3.449 3.37e-04' ) cp.UpdateSettings(type='poisson', range=[.22, .43], resurf=0) self.assertEqual( get_cps_summary(), 'Pickering 3036.6 pseudo-log 0.22 0.43 poisson 0 223 223 0.684 0.64 0.731 -3.477 -3.506 -3.448 3.34e-04' ) cp.UpdateSettings(type='poisson', range=[.22, .43], binning='none') self.assertEqual( get_cps_summary(), 'Pickering 3036.6 none 0.22 0.43 poisson 0 223 223 0.684 0.64 0.731 -3.477 -3.506 -3.448 3.34e-04' )