diff --git a/astrotools/auger.py b/astrotools/auger.py index 80996ad8768b1b7a39e9b0e1305a4ade7ed94722..405bff8457c3cc5d99c9ec5a2299df4d333fed91 100644 --- a/astrotools/auger.py +++ b/astrotools/auger.py @@ -589,10 +589,10 @@ def plot_mean_xmax(ax=None, with_legend=True, models=None): # pragma: no cov models = ['EPOS-LHC', 'Sibyll2.1', 'QGSJetII-04'] if models is None else models d = DXMAX['moments'] log10e = d['meanLgEnergy'] - m_x = d['mean_xmax'] - e_stat = d['mean_xmaxSigmaStat'] - e_syslo = d['mean_xmaxSigmaSysLow'] - e_syshi = d['mean_xmaxSigmaSysUp'] + m_x = d['meanXmax'] + e_stat = d['meanXmaxSigmaStat'] + e_syslo = d['meanXmaxSigmaSysLow'] + e_syshi = d['meanXmaxSigmaSysUp'] l1 = ax.errorbar(log10e, m_x, yerr=e_stat, fmt='ko', lw=1, ms=8, capsize=0) l2 = ax.errorbar(log10e, m_x, yerr=[-e_syslo, e_syshi], diff --git a/astrotools/cosmic_rays.py b/astrotools/cosmic_rays.py index 4f0e87a1e1083f2580f86ac33c14d772eb193479..9325aab5729c8ec7b12db66137cd5dc5678b502d 100644 --- a/astrotools/cosmic_rays.py +++ b/astrotools/cosmic_rays.py @@ -146,15 +146,16 @@ def plot_energy_spectrum(crs, xlabel='log$_{10}$(Energy / eV)', ylabel='entries' class CosmicRaysBase: """ Cosmic rays base class meant for inheritance """ - def __init__(self, cosmic_rays, **kwargs): + def __init__(self, cosmic_rays): self.type = "CosmicRays" # needed for the iteration self._current_idx = 0 # type: int self.general_object_store = {} - # noinspection PyUnresolvedReferences - if isinstance(cosmic_rays, str): - self.load(cosmic_rays, **kwargs) + if cosmic_rays is None: + self.cosmic_rays = np.empty(0, dtype=DTYPE_TEMPLATE) + elif isinstance(cosmic_rays, str): + self.load(cosmic_rays) elif isinstance(cosmic_rays, np.ndarray): self.cosmic_rays = cosmic_rays elif isinstance(cosmic_rays, (int, float, np.integer, np.dtype)): @@ -408,15 +409,14 @@ class CosmicRaysBase: class CosmicRaysSets(CosmicRaysBase): """Set of cosmic rays """ - def __init__(self, nsets, ncrs=None, **kwargs): + def __init__(self, nsets, ncrs=None): self.type = "CosmicRaysSet" if nsets is None: - raise NotImplementedError( - "Either the number of cosmic rays has to be set or the numpy array with cosmic rays has to be given" - "or a filename to load cosmic rays from has to be given") + CosmicRaysBase.__init__(self, cosmic_rays=None) + self.type = "CosmicRaysSet" # noinspection PyUnresolvedReferences - if isinstance(nsets, str): - self.load(nsets, **kwargs) + elif isinstance(nsets, str): + self.load(nsets) elif isinstance(nsets, (tuple, float, int, np.integer)): self.nsets = nsets[0] if isinstance(nsets, tuple) else nsets ncrs = nsets[1] if isinstance(nsets, tuple) else ncrs diff --git a/astrotools/gamale.py b/astrotools/gamale.py index 099174b7fa5951b5d1e64ce589a5de7d602b707d..3b9845840ec7c43220d50af6a60913b3d09cbe02 100644 --- a/astrotools/gamale.py +++ b/astrotools/gamale.py @@ -219,6 +219,7 @@ class Lens: return self.neutral_lens_part if not self.lens_parts: raise Exception("Lens empty") + assert isinstance(log10e, (float, int)), "Type of log10e not understood" log10r = log10e - np.log10(z) log10r_bins = np.append(self.log10r_mins, np.max(self.log10r_max)) i = np.digitize(log10r, log10r_bins) -1 diff --git a/astrotools/healpytools.py b/astrotools/healpytools.py index a0854df517f8862a85aa9ba477171c02afa0e095..173a16026884fc653a930886cacf02a5313540fc 100644 --- a/astrotools/healpytools.py +++ b/astrotools/healpytools.py @@ -294,7 +294,7 @@ def fisher_pdf(nside, v, y=None, z=None, k=None, threshold=4, sparse=False): :param k: kappa for the fisher distribution, 1 / sigma**2 :param threshold: Threshold in sigma up to where the distribution should be calculated :param sparse: returns the map in the form (pixels, weights); this may be meaningfull for small distributions - :return: pixels, weights at pixels + :return: pixels, weights at pixels if sparse else a full map with length nside2npix(nside) """ assert k is not None, "Concentration parameter 'k' for fisher_pdf() must be set!" sigma = 1. / np.sqrt(k) # in radians diff --git a/astrotools/skymap.py b/astrotools/skymap.py index 350c0e6a97512d3c00ac4a0285040108b1822c52..52173c2e4952db2f580022c3821c3ef72793ee15 100644 --- a/astrotools/skymap.py +++ b/astrotools/skymap.py @@ -27,21 +27,27 @@ def scatter(v, log10e, cblabel='log$_{10}$(Energy / eV)', fontsize=26, opath=Non kwargs.setdefault('s', 8) kwargs.setdefault('lw', 0) kwargs.setdefault('c', log10e) - events = ax.scatter(lons, lats, **kwargs) + finite = np.isfinite(kwargs.get('c')) + vmin = kwargs.pop('vmin', smart_round(np.min(kwargs.get('c')[finite]))) + vmax = kwargs.pop('vmax', smart_round(np.max(kwargs.get('c')[finite]))) + + step = smart_round((vmax - vmin) / 5., order=1) + cticks = kwargs.pop('cticks', np.arange(vmin, vmax, step)) - cbar = plt.colorbar(events, orientation='horizontal', shrink=0.85, pad=0.05, aspect=30, cmap=kwargs.get('cmap')) + events = ax.scatter(lons, lats, **kwargs) + cbar = plt.colorbar(events, orientation='horizontal', shrink=0.85, pad=0.05, aspect=30, + cmap=kwargs.get('cmap'), ticks=cticks) cbar.set_label(cblabel, fontsize=fontsize) - step = 0.2 if np.max(log10e) - np.min(log10e) > 1. else 0.1 - cbar.set_ticks(np.arange(round(np.min(log10e), 1), round(np.max(log10e), 1), step)) + cbar.set_clim(vmin, vmax) cbar.ax.tick_params(labelsize=fontsize - 4) + cbar.draw_all() - plt.xticks(np.arange(-5. / 6. * np.pi, np.pi, np.pi / 6.), + plt.xticks(np.pi/6. * np.arange(-5, 6, 1), ['', '', r'90$^{\circ}$', '', '', r'0$^{\circ}$', '', '', r'-90$^{\circ}$', '', ''], fontsize=fontsize) # noinspection PyTypeChecker plt.yticks([-np.radians(60), -np.radians(30), 0, np.radians(30), np.radians(60)], [r'-60$^{\circ}$', r'-30$^{\circ}$', r'0$^{\circ}$', r'30$^{\circ}$', r'60$^{\circ}$'], fontsize=fontsize) - ax.grid(True) if opath is not None: diff --git a/test/test_cosmic_rays.py b/test/test_cosmic_rays.py index 2720b13af0a75a4484dafd93ac9017d405ee2b46..aa82a94c246cbb46214417ab5dd31a34628c4989 100644 --- a/test/test_cosmic_rays.py +++ b/test/test_cosmic_rays.py @@ -310,8 +310,19 @@ class TestCosmicRaysSets(unittest.TestCase): self.assertEqual(crsset.nsets, nsets) def test_01a_create_with_None(self): - with self.assertRaises(NotImplementedError): - CosmicRaysSets(None) + ncrs = 10 + nsets = 15 + crsset = CosmicRaysSets((nsets, ncrs)) + log10e = np.random.random((15, 10)) + crsset['log10e'] = log10e + outpath = "/tmp/cosmicraysset.npz" + crsset.save(outpath) + + crsset2 = CosmicRaysSets(None) + crsset2.load(outpath) + self.assertTrue('log10e' in crsset2.keys()) + self.assertTrue((crsset2['log10e'] == log10e).all()) + os.remove(outpath) def test_01b_create_with_fake_object(self): class test: