Ejemplo n.º 1
0
Archivo: frb.py Proyecto: FRBs/FRB
    def write_to_json(self, outfile=None, path='./', overwrite=True):
        """
        Write key aspects of the class to disk in a JSON file

        Args:
            outfile (str, optional): Output filename
              If not provided, one will be generated with make_outfile()
            path (str, optional): Path for the output file
            overwrite (bool, optional): Overwrite?

        Returns:

        """
        if outfile is None:
            outfile = self.make_outfile()
        # Build the dict
        frb_dict = {}

        # Basics
        if self.coord is not None:
            frb_dict['ra'] = self.coord.ra.value
            frb_dict['dec'] = self.coord.dec.value
        if self.frb_name is not None:
            frb_dict['FRB'] = self.frb_name
        frb_dict['cosmo'] = self.cosmo.name
        frb_dict['refs'] = self.refs

        if self.repeater is not None:
            frb_dict['repeater'] = self.repeater

        # Measured properties
        for attr in ['S', 'nu_c', 'DM', 'z', 'RM', 'DMISM', 'fluence', 'lpol']:
            # Value
            if getattr(self, attr) is not None:
                frb_dict[attr] = getattr(self, attr)
            # Error
            if hasattr(self, attr + '_err'):
                if getattr(self, attr + '_err') is not None:
                    frb_dict[attr + '_err'] = getattr(self, attr + '_err')

        # Main dicts
        for idict in self.main_dict:
            if getattr(self,
                       idict) is not None and len(getattr(self, idict)) > 0:
                frb_dict[idict] = getattr(self, idict)

        # JSONify
        jdict = utils.jsonify(copy.deepcopy(frb_dict))

        # Write
        utils.savejson(os.path.join(path, outfile),
                       jdict,
                       easy_to_read=True,
                       overwrite=overwrite)
        print("Wrote data to {}".format(os.path.join(path, outfile)))
Ejemplo n.º 2
0
    def write_to_json(self, outfile=None, path='./', overwrite=True):
        """
        Write key aspects of the class to disk in a JSON file

        Args:
            outfile (str, optional): Output filename
              If not provided, one will be generated with make_outfile()
            path (str, optional): Path for the output file
            overwrite (bool, optional): Overwrite?

        Returns:

        """
        # Generate path as needed
        if not os.path.isdir(path):
            os.mkdir(path)
        if outfile is None:
            outfile = self.make_outfile()
        # Build the dict
        frbgal_dict = {}

        # Basics
        frbgal_dict['ra'] = self.coord.ra.value
        frbgal_dict['dec'] = self.coord.dec.value
        frbgal_dict['FRB'] = self.frb.frb_name
        if self.frb.coord is not None:
            frbgal_dict['ra_FRB'] = self.frb.coord.ra.value
            frbgal_dict['dec_FRB'] = self.frb.coord.dec.value
        frbgal_dict['cosmo'] = self.cosmo.name

        # Main attributes
        for attr in self.main_attr:
            if len(getattr(self, attr)) > 0:
                frbgal_dict[attr] = getattr(self, attr)

        # JSONify
        jdict = utils.jsonify(frbgal_dict)

        # Write
        utils.savejson(os.path.join(path, outfile),
                       jdict,
                       easy_to_read=True,
                       overwrite=overwrite)
        print("Wrote data to {}".format(os.path.join(path, outfile)))
Ejemplo n.º 3
0
def test_pm():
    # This takes 5min to run
    # Hiding this import in here
    import pymc3 as pm

    parm_dict = mcmc.grab_parmdict()
    outroot = os.path.join(resource_filename('frb', 'tests'), 
                           'files', 'mcmc')

    with mcmc.pm_four_parameter_model(parm_dict, beta=3.):
        # Sample
        #trace = pm.sample(40000, tune=2000) # This defaults to 4 chains
        trace = pm.sample(1000, tune=500) # This defaults to 4 chains
        # Save the traces -- Needs to be done before the plot
        pm.save_trace(trace, directory=outroot, overwrite=True)
        print("All done with the 4 parameter, beta=3 run ")
        # Save a plot
        plt.clf()
        _ = pm.plot_trace(trace)
        #plt.savefig(os.path.join(outroot, 'traceplot.png'))
        # Parameters
        jdict = utils.jsonify(parm_dict)
        utils.savejson(os.path.join(outroot, 'parms.json'), jdict, easy_to_read=True)