Exemplo n.º 1
0
    def _plot(self, run_id='current'):

        saved = self.load('source_receivers', run_id=run_id)
        conf = self.in_config.get_config()
        
        #
        # Station map
        #
        receivers = saved['receivers']
        source_location = saved['source_location']
               
        lats = num.array( [ r.lat for r in receivers ], dtype='float' )
        lons = num.array( [ r.lon for r in receivers ], dtype='float' )
        dists = num.array( [ r.distance_deg for r in receivers ], dtype='float' )
        rnames = [ ' '.join(r.name.split('.')) for r in receivers ]
        slat, slon = source_location[:2]
        
        station_size = [ 0.05 ] * len(receivers)
        station_color = [ (0.,1.)[r.enabled] for r in receivers ]
        
        plotdir = self.make_plotdir_path(run_id)
        source = None
        
        fn = pjoin(plotdir, 'stations.pdf')
        plotting.station_plot( slat, slon, lats, lons, rnames, station_color, station_size, 
                               source, num.amax(dists)*1.05, fn, {}, zexpand=1.02, nsets=conf['nsets'], symbols=('t','t'))
        
        #
        # Region map
        #
        fn = pjoin(plotdir, 'region.pdf')
        plotting.location_map( fn, slat, slon, 5., {}, receivers=(lats, lons, rnames))
        
        
        return [ 'stations.pdf', 'region.pdf' ]
Exemplo n.º 2
0
    def plot(self, dirname, nsets=1, source_model_infos=None, conf_overrides=None):
        
        best_source = self.best_source
        bootstrap_sources = self.bootstrap_sources
        
        
        plot_files = []
        
        param_min_misfits = {} 
        
        for iparam, param in enumerate(self.sourceparams):
            #
            # 1D misfit cross section
            #
            xy = []
            for s,m in zip(self.sources, self.misfits_by_s):
                if not num.isnan(m):
                    xy.append((s[param], m))
            
            xdata, ydata = num.array(xy,dtype=num.float).transpose()
            
            conf = dict( xlabel = param.title(),
                         xunit = self.base_source.sourceinfo(param).unit )
            if conf_overrides:
                conf.update(conf_overrides)
            
            plotting.km_hack(conf)
            fn = 'misfit-%s.pdf' % param
            plotting.misfit_plot_1d( [(xdata,ydata)],
                                     pjoin(dirname, fn),
                                     conf, apply_moment_to_magnitude_hack=True )
            plot_files.append(fn)
            
            
            
            #
            # 1D histogram
            #
            gvalues = self.param_values[iparam][1]
            gedges = values_to_bin_edges(gvalues)
            
            kwargs = {}
            if util.cmp_version(numpy_version(), '1.3.0') < 0:
                kwargs = {'new': True}
                
            hist, edges = num.histogram(self.stats[param].distribution,
                                        bins=gedges, **kwargs)
            
            hist = hist/float(len(bootstrap_sources))
            
            conf = dict( xlabel = param.title(),
                         xunit = self.base_source.sourceinfo(param).unit)
            if conf_overrides:
                conf.update(conf_overrides)
            
            plotting.km_hack(conf)
            fn = 'histogram-%s.pdf' % param
            plotting.histogram_plot_1d( edges, hist, 
                                        pjoin(dirname, fn),
                                        conf, apply_moment_to_magnitude_hack=True)
            
            plot_files.append( fn )
            
            #
            #
            # gather data for 1D projected misfit cross sections
            indi = num.digitize(xdata, gedges) - 1
            for_minmisfits = []
            for ival in range(gvalues.size):
                vydata = num.extract(indi == ival, ydata)
                if vydata.size != 0:
                    for_minmisfits.append((gvalues[ival], vydata.min()))
                        
            param_min_misfits[param] = num.array(for_minmisfits,dtype=num.float).transpose()
        
        # data range for projected misfit cross section
        mi, ma = num.inf, -num.inf
        for xdata,ydata in param_min_misfits.values():
            mi = min(mi, ydata.min())
            ma = max(ma, ydata.max())
            
        param_min_misfits_range = (mi,ma)
        
        # 1D projected misfit cross sections
        for iparam, param in enumerate(self.sourceparams):
            mini, maxi = param_min_misfits_range
            
            if mini < 0.95:
                maxi = 1.
                       
            else:
                if mini < 1.95:
                    maxi = 2.
                       
            conf = dict( xlabel = param.title(),
                         xunit = self.base_source.sourceinfo(param).unit,
                         ylimits = (mini, maxi),
                         ymasking=False)
            
            if conf_overrides:
                conf.update(conf_overrides)

            plotting.km_hack(conf)
            fn = 'min-misfit-%s.pdf' % param
                        
            plotting.misfit_plot_1d( [param_min_misfits[param]],
                                     pjoin(dirname, fn),
                                     conf,apply_moment_to_magnitude_hack=True )
            plot_files.append(fn)
            
            fn_table = 'min-misfit-%s.table' % param
            num.savetxt(pjoin(dirname, fn_table),  param_min_misfits[param].transpose())
                        
        for ixparam, xparam in enumerate(self.sourceparams):
            for iyparam, yparam in enumerate(self.sourceparams):
                if ixparam == iyparam: continue
                
                #
                # 2D misfogram plot
                #
                vmisfits = {}
                maxmisfit = num.nanmax(self.misfits_by_s)
                for (source, misfit) in zip(self.sources, self.misfits_by_s):
                    if num.isnan(misfit): continue
                    vx = source[xparam]
                    vy = source[yparam]
                    vmisfits[(vx,vy)] = min(vmisfits.get((vx,vy), maxmisfit), misfit)
                    
                vcounts = {}
                for source in bootstrap_sources:
                    vx = source[xparam]
                    vy = source[yparam]
                    vcounts[(vx,vy)] = vcounts.get((vx,vy), 0) + 1
                
                lx, ly, lz = [], [], []
                for ((vx,vy),vmisfit) in vmisfits.iteritems():
                    lx.append(vx)
                    ly.append(vy)
                    lz.append(vmisfit)
                    
                lxc, lyc, lc =  [], [], []
                for ((vx,vy),vcount) in vcounts.iteritems():
                    lxc.append(vx)
                    lyc.append(vy)
                    lc.append(vcount)
                
                ax = num.array(lx,dtype=num.float)
                ay = num.array(ly,dtype=num.float)
                az = num.array(lz,dtype=num.float)
                
                
                axc = num.array(lxc,dtype=num.float)
                ayc = num.array(lyc,dtype=num.float)
                ac = num.array(lc,dtype=num.float)
                ac = ac/len(bootstrap_sources)
                if len(bootstrap_sources) > 1:
                    bootstrap_data = (axc, ayc, ac)
                else:
                    bootstrap_data = ([],[],[])
                
                if az.min() < 0.95:
                    zlimits = (az.min(),min(1.,az.max()))
                else:
                    zlimits = (az.min(),az.max())
                
                if 'misfit_limits' in dir(config):
                    zlimits = config.misfit_limits
                
                conf = dict( xlabel = xparam.title(),
                             xunit = self.base_source.sourceinfo(xparam).unit,
                             ylabel = yparam.title(),
                             yunit = self.base_source.sourceinfo(yparam).unit,
                             zlimits = zlimits,
                             zmasking=False,
                             zsnap = True
                         )
                
                if conf_overrides:
                    for k in conf_overrides:
                        if not k.endswith('snap'):
                            conf[k] = conf_overrides[k]
                    
                
                best_loc = (num.array([ best_source[xparam]]), num.array([best_source[yparam]]))
                plotting.km_hack(conf)
                plotting.nukl_hack(conf)
                
                fn = 'misfogram-%s-%s.pdf' % (xparam, yparam)
                plotting.misfogram_plot_2d_gmtpy( [(ax, ay, az), best_loc, bootstrap_data],
                                        pjoin(dirname, fn),
                                        conf, apply_moment_to_magnitude_hack=True )
                plot_files.append(fn)
        
        #
        # Station plot
        #
        lats = num.array( [ r.lat for r in self.receivers ], dtype='float' )
        lons = num.array( [ r.lon for r in self.receivers ], dtype='float' )
        dists = num.array( [ r.distance_deg for r in self.receivers ], dtype='float' )
        rnames = [ ' '.join(r.name.split('.')) for r in self.receivers ]
        slat, slon = self.source_location[:2]
        if len(self.misfits_by_r) == 0:
            station_misfits = self.ref_misfits_by_r
        else:
            station_misfits = self.misfits_by_r-self.ref_misfits_by_r
        #station_varia = self.variability_by_r / num.sum(self.variability_by_r) * len(self.receivers)
        station_varia = self.misfits_by_r / num.sum(self.misfits_by_r) * len(self.receivers)
        plotting.station_plot( slat, slon, lats, lons, rnames, station_misfits, station_varia, 
                              best_source, num.amax(dists)*1.05, pjoin(dirname, 'stations.pdf'), {}, nsets=nsets)
        plot_files.append('stations.pdf')
        
        indicate_plane = 0
        if source_model_infos:
            source_size = 0.
            if 'bord-radius' in best_source.keys():
                source_size = best_source['bord-radius']
                
            delta_lat = 1.5*max(source_size*2.,50000)/(20000.*1000.)*180
            
            si = None
            if 'bord-radius' in best_source.keys() and best_source['bord-radius'] > 0.0:
                si = source_model_infos
                indicate_plane = 1
            
            plotting.location_map(pjoin(dirname, 'location.pdf'), slat, slon, delta_lat, {}, source=best_source,
                                  source_model_infos=si, receivers=(lats,lons,rnames))
                                  
            plot_files.append('location.pdf')
        
        fns = plotting.beachball(best_source, pjoin(dirname, 'beachball.pdf'), indicate_plane=indicate_plane)
        if fns:
            plot_files.append('beachball.pdf')
        
        
        
        return plot_files
Exemplo n.º 3
0
    def plot(self,
             dirname,
             nsets=1,
             source_model_infos=None,
             conf_overrides=None):

        best_source = self.best_source
        bootstrap_sources = self.bootstrap_sources

        plot_files = []

        param_min_misfits = {}

        for iparam, param in enumerate(self.sourceparams):
            #
            # 1D misfit cross section
            #
            xy = []
            for s, m in zip(self.sources, self.misfits_by_s):
                if not num.isnan(m):
                    xy.append((s[param], m))

            xdata, ydata = num.array(xy, dtype=num.float).transpose()

            conf = dict(xlabel=param.title(),
                        xunit=self.base_source.sourceinfo(param).unit)
            if conf_overrides:
                conf.update(conf_overrides)

            plotting.km_hack(conf)
            fn = 'misfit-%s.pdf' % param
            plotting.misfit_plot_1d([(xdata, ydata)],
                                    pjoin(dirname, fn),
                                    conf,
                                    apply_moment_to_magnitude_hack=True)
            plot_files.append(fn)

            #
            # 1D histogram
            #
            gvalues = self.param_values[iparam][1]
            gedges = values_to_bin_edges(gvalues)

            kwargs = {}
            if util.cmp_version(numpy_version(), '1.3.0') < 0:
                kwargs = {'new': True}

            hist, edges = num.histogram(self.stats[param].distribution,
                                        bins=gedges,
                                        **kwargs)

            hist = hist / float(len(bootstrap_sources))

            conf = dict(xlabel=param.title(),
                        xunit=self.base_source.sourceinfo(param).unit)
            if conf_overrides:
                conf.update(conf_overrides)

            plotting.km_hack(conf)
            fn = 'histogram-%s.pdf' % param
            plotting.histogram_plot_1d(edges,
                                       hist,
                                       pjoin(dirname, fn),
                                       conf,
                                       apply_moment_to_magnitude_hack=True)

            plot_files.append(fn)

            #
            #
            # gather data for 1D projected misfit cross sections
            indi = num.digitize(xdata, gedges) - 1
            for_minmisfits = []
            for ival in range(gvalues.size):
                vydata = num.extract(indi == ival, ydata)
                if vydata.size != 0:
                    for_minmisfits.append((gvalues[ival], vydata.min()))

            param_min_misfits[param] = num.array(for_minmisfits,
                                                 dtype=num.float).transpose()

        # data range for projected misfit cross section
        mi, ma = num.inf, -num.inf
        for xdata, ydata in param_min_misfits.values():
            mi = min(mi, ydata.min())
            ma = max(ma, ydata.max())

        param_min_misfits_range = (mi, ma)

        # 1D projected misfit cross sections
        for iparam, param in enumerate(self.sourceparams):
            mini, maxi = param_min_misfits_range

            if mini < 0.95:
                maxi = 1.

            else:
                if mini < 1.95:
                    maxi = 2.

            conf = dict(xlabel=param.title(),
                        xunit=self.base_source.sourceinfo(param).unit,
                        ylimits=(mini, maxi),
                        ymasking=False)

            if conf_overrides:
                conf.update(conf_overrides)

            plotting.km_hack(conf)
            fn = 'min-misfit-%s.pdf' % param

            plotting.misfit_plot_1d([param_min_misfits[param]],
                                    pjoin(dirname, fn),
                                    conf,
                                    apply_moment_to_magnitude_hack=True)
            plot_files.append(fn)

            fn_table = 'min-misfit-%s.table' % param
            num.savetxt(pjoin(dirname, fn_table),
                        param_min_misfits[param].transpose())

        for ixparam, xparam in enumerate(self.sourceparams):
            for iyparam, yparam in enumerate(self.sourceparams):
                if ixparam == iyparam: continue

                #
                # 2D misfogram plot
                #
                vmisfits = {}
                maxmisfit = num.nanmax(self.misfits_by_s)
                for (source, misfit) in zip(self.sources, self.misfits_by_s):
                    if num.isnan(misfit): continue
                    vx = source[xparam]
                    vy = source[yparam]
                    vmisfits[(vx, vy)] = min(vmisfits.get((vx, vy), maxmisfit),
                                             misfit)

                vcounts = {}
                for source in bootstrap_sources:
                    vx = source[xparam]
                    vy = source[yparam]
                    vcounts[(vx, vy)] = vcounts.get((vx, vy), 0) + 1

                lx, ly, lz = [], [], []
                for ((vx, vy), vmisfit) in vmisfits.iteritems():
                    lx.append(vx)
                    ly.append(vy)
                    lz.append(vmisfit)

                lxc, lyc, lc = [], [], []
                for ((vx, vy), vcount) in vcounts.iteritems():
                    lxc.append(vx)
                    lyc.append(vy)
                    lc.append(vcount)

                ax = num.array(lx, dtype=num.float)
                ay = num.array(ly, dtype=num.float)
                az = num.array(lz, dtype=num.float)

                axc = num.array(lxc, dtype=num.float)
                ayc = num.array(lyc, dtype=num.float)
                ac = num.array(lc, dtype=num.float)
                ac = ac / len(bootstrap_sources)
                if len(bootstrap_sources) > 1:
                    bootstrap_data = (axc, ayc, ac)
                else:
                    bootstrap_data = ([], [], [])

                if az.min() < 0.95:
                    zlimits = (az.min(), min(1., az.max()))
                else:
                    zlimits = (az.min(), az.max())

                if 'misfit_limits' in dir(config):
                    zlimits = config.misfit_limits

                conf = dict(xlabel=xparam.title(),
                            xunit=self.base_source.sourceinfo(xparam).unit,
                            ylabel=yparam.title(),
                            yunit=self.base_source.sourceinfo(yparam).unit,
                            zlimits=zlimits,
                            zmasking=False,
                            zsnap=True)

                if conf_overrides:
                    for k in conf_overrides:
                        if not k.endswith('snap'):
                            conf[k] = conf_overrides[k]

                best_loc = (num.array([best_source[xparam]]),
                            num.array([best_source[yparam]]))
                plotting.km_hack(conf)
                plotting.nukl_hack(conf)

                fn = 'misfogram-%s-%s.pdf' % (xparam, yparam)
                plotting.misfogram_plot_2d_gmtpy(
                    [(ax, ay, az), best_loc, bootstrap_data],
                    pjoin(dirname, fn),
                    conf,
                    apply_moment_to_magnitude_hack=True)
                plot_files.append(fn)

        #
        # Station plot
        #
        lats = num.array([r.lat for r in self.receivers], dtype='float')
        lons = num.array([r.lon for r in self.receivers], dtype='float')
        dists = num.array([r.distance_deg for r in self.receivers],
                          dtype='float')
        rnames = [' '.join(r.name.split('.')) for r in self.receivers]
        slat, slon = self.source_location[:2]
        if len(self.misfits_by_r) == 0:
            station_misfits = self.ref_misfits_by_r
        else:
            station_misfits = self.misfits_by_r - self.ref_misfits_by_r
        #station_varia = self.variability_by_r / num.sum(self.variability_by_r) * len(self.receivers)
        station_varia = self.misfits_by_r / num.sum(self.misfits_by_r) * len(
            self.receivers)
        plotting.station_plot(slat,
                              slon,
                              lats,
                              lons,
                              rnames,
                              station_misfits,
                              station_varia,
                              best_source,
                              num.amax(dists) * 1.05,
                              pjoin(dirname, 'stations.pdf'), {},
                              nsets=nsets)
        plot_files.append('stations.pdf')

        indicate_plane = 0
        if source_model_infos:
            source_size = 0.
            if 'bord-radius' in best_source.keys():
                source_size = best_source['bord-radius']

            delta_lat = 1.5 * max(source_size * 2.,
                                  50000) / (20000. * 1000.) * 180

            si = None
            if 'bord-radius' in best_source.keys(
            ) and best_source['bord-radius'] > 0.0:
                si = source_model_infos
                indicate_plane = 1

            plotting.location_map(pjoin(dirname, 'location.pdf'),
                                  slat,
                                  slon,
                                  delta_lat, {},
                                  source=best_source,
                                  source_model_infos=si,
                                  receivers=(lats, lons, rnames))

            plot_files.append('location.pdf')

        fns = plotting.beachball(best_source,
                                 pjoin(dirname, 'beachball.pdf'),
                                 indicate_plane=indicate_plane)
        if fns:
            plot_files.append('beachball.pdf')

        return plot_files