def command_init(args): ''' Execution of command init ''' def setup(parser): parser.add_option('--force', dest='force', action='store_true', help='overwrite existing project directory') parser, options, args = cl_parse('init', args, setup) if len(args) != 1: help_and_die(parser, 'missing argument') else: fn_config = args[0] if not os.path.isfile(fn_config): die('config file missing: %s' % fn_config) conf = load(filename=fn_config) config.check(conf) if ((not options.force) and (os.path.isdir(conf.project_dir))): die('project dir exists: %s; use force option' % conf.project_dir) else: if os.path.isdir(conf.project_dir): shutil.rmtree(conf.project_dir) os.mkdir(conf.project_dir) conf.dump(filename=os.path.join(conf.project_dir, 'seiscloud.config')) dst = os.path.join(conf.project_dir, 'catalog.pf') if conf.catalog_origin == 'file': src = conf.catalog_fn shutil.copyfile(src, dst) else: if conf.catalog_origin == 'globalcmt': orig_catalog = catalog.GlobalCMT() else: # geofon orig_catalog = catalog.Geofon() events = orig_catalog.get_events(time_range=(util.str_to_time( conf.tmin), util.str_to_time(conf.tmax)), magmin=conf.magmin, latmin=conf.latmin, latmax=conf.latmax, lonmin=conf.lonmin, lonmax=conf.lonmax) selevents = [ev for ev in events if ev.magnitude <= conf.magmax] model.dump_events(selevents, dst) print('Project directory prepared "%s"' % conf.project_dir)
def command_init(args): ''' Execution of command init ''' def setup(parser): parser.add_option('--force', dest='force', action='store_true', help='overwrite existing project directory') parser, options, args = cl_parse('init', args, setup) if len(args) != 1: help_and_die(parser, 'missing argument') else: fn_config = args[0] if not os.path.isfile(fn_config): die('config file missing: %s' % fn_config) conf = load(filename=fn_config) config.check(conf) if ((not options.force) and (os.path.isdir(conf.project_dir))): die('project directory exists: %s; use force option' % conf.project_dir) else: if os.path.isdir(conf.project_dir): shutil.rmtree(conf.project_dir) os.mkdir(conf.project_dir) conf.dump(filename=os.path.join(conf.project_dir, 'seiscloud.config')) src, dst = conf.catalog_fn, os.path.join(conf.project_dir, 'catalog.pf') shutil.copyfile(src, dst) print('Project directory prepared "%s"' % conf.project_dir)
def command_plot(args): ''' Execution of command plot ''' def setup(parser): parser.add_option('--force', dest='force', action='store_true', help='overwrite existing project directory') parser, options, args = cl_parse('plot', args, setup) if len(args) != 1: help_and_die(parser, 'missing argument') else: fn_config = args[0] if not os.path.isfile(fn_config): die('config file missing: %s' % fn_config) conf = load(filename=fn_config) config.check(conf) if not os.path.isdir(conf.project_dir): die('project directory missing: %s' % conf.project_dir) resdir = os.path.join(conf.project_dir, 'clustering_results') if not os.path.isdir(resdir): die('clustering results missing: %s' % resdir) plotdir = os.path.join(conf.project_dir, 'clustering_plots') resdir = os.path.join(conf.project_dir, 'clustering_results') if not (options.force): if (os.path.isdir(plotdir)): die('clustering plot directory exists; use force option') if options.force: if (os.path.isdir(plotdir)): shutil.rmtree(plotdir) os.mkdir(plotdir) simmat_temporal_fn = os.path.join(conf.project_dir, 'simmat_temporal.npy') simmat_clustered_fn = os.path.join(conf.project_dir, 'simmat_clustered.npy') if not os.path.isfile(simmat_temporal_fn): die('similarity matrix does not exists: %s; ' + 'use seiscloud matrix first' % simmat_temporal_fn) if not os.path.isfile(simmat_clustered_fn): die('similarity matrix does not exists: %s; ' + 'use seiscloud matrix first' % simmat_clustered_fn) new_catalog_fn = os.path.join(conf.project_dir, 'events_to_be_clustered.pf') if not os.path.isfile(new_catalog_fn): die('catalog of selected events does not exists: %s; ' + 'use seiscloud matrix and seiscloud cluster first' % new_catalog_fn) events = model.load_events(new_catalog_fn) eventsclusters = sccluster.load_obj( os.path.join(resdir, 'processed.eventsclusters')) clusters = sccluster.load_obj(os.path.join(resdir, 'processed.clusters')) scplot.plot_all(events, eventsclusters, clusters, conf, resdir, plotdir) print('Seiscloud plots prepared in "%s"' % plotdir)
def command_cluster(args): ''' Execution of command cluster ''' def setup(parser): parser.add_option('--force', dest='force', action='store_true', help='overwrite existing project directory') parser.add_option('--view', dest='view', action='store_true', help='view similarity matrix after clustering') parser.add_option( '--savefig', dest='savefig', action='store_true', help='save figure of similarity matrix after clustering') parser, options, args = cl_parse('cluster', args, setup) if len(args) != 1: help_and_die(parser, 'missing argument') else: fn_config = args[0] if not os.path.isfile(fn_config): die('config file missing: %s' % fn_config) conf = load(filename=fn_config) config.check(conf) if not os.path.isdir(conf.project_dir): die('project directory missing: %s' % conf.project_dir) resdir = os.path.join(conf.project_dir, 'clustering_results') if not (options.force): if (os.path.isdir(resdir)): die('clustering result directory exists; use force option') if options.force: if (os.path.isdir(resdir)): shutil.rmtree(resdir) os.mkdir(resdir) simmat_temporal_fn = os.path.join(conf.project_dir, 'simmat_temporal.npy') if not os.path.isfile(simmat_temporal_fn): die('similarity matrix does not exists: %s; ' + 'use seiscloud matrix first' % simmat_temporal_fn) new_catalog_fn = os.path.join(conf.project_dir, 'events_to_be_clustered.pf') if not os.path.isfile(new_catalog_fn): die('catalog of selected events does not exists: %s; ' + 'use seiscloud matrix first' % new_catalog_fn) simmat_temp = sccluster.load_similarity_matrix(simmat_temporal_fn) events = model.load_events(new_catalog_fn) eventsclusters = sccluster.dbscan(simmat_temp, conf.dbscan_nmin, conf.dbscan_eps, conf.sw_force_cluster_all) clusters = sccluster.get_clusters(events, eventsclusters) if min(eventsclusters) == -1: noise_cluster_empty = False else: noise_cluster_empty = True sccluster.save_all(events, eventsclusters, clusters, conf, resdir) simmat_clus = sccluster.get_simmat_clustered(events, eventsclusters, clusters, conf, resdir, simmat_temp) simmat_clustered_fn = os.path.join(conf.project_dir, 'simmat_clustered.npy') sccluster.save_similarity_matrix(simmat_clus, simmat_clustered_fn) print('I run seiscloud for the project in "%s"' % conf.project_dir) n_clusters = len(clusters) if not noise_cluster_empty: n_clusters = n_clusters - 1 print(str(n_clusters) + ' cluster(s) found') simmat_fig_fn = os.path.join(conf.project_dir, 'simmat_clustered.' + conf.figure_format) if options.view and options.savefig: scplot.view_and_savefig_similarity_matrix(simmat_clus, simmat_fig_fn, 'Sorted after clustering') else: if options.view: scplot.view_similarity_matrix(simmat_clus, 'Sorted after clustering') if options.savefig: scplot.savefig_similarity_matrix(simmat_clus, simmat_fig_fn, 'Sorted after clustering') for cluster in clusters: selevents = clusters[cluster] median = sccluster.get_median(selevents, conf) fn_median = os.path.join(resdir, 'median_cluster' + str(cluster) + '.pf') model.dump_events([median], fn_median) # print(cluster,len(selevents)) print('Similarity matrix after clustering computed and stored as "%s"' % simmat_clustered_fn) if options.savefig: print('Similarity matrix figure saved as "%s"' % simmat_fig_fn)
def command_matrix(args): ''' Execution of command matrix ''' def setup(parser): parser.add_option('--force', dest='force', action='store_true', help='overwrite existing project directory') parser.add_option('--view', dest='view', action='store_true', help='view similarity matrix') parser.add_option('--savefig', dest='savefig', action='store_true', help='save figure of similarity matrix') parser, options, args = cl_parse('matrix', args, setup) if len(args) != 1: help_and_die(parser, 'missing argument') else: fn_config = args[0] if not os.path.isfile(fn_config): die('config file missing: %s' % fn_config) conf = load(filename=fn_config) config.check(conf) if not os.path.isdir(conf.project_dir): die('project directory missing: %s' % conf.project_dir) simmat_temporal_fn = os.path.join(conf.project_dir, 'simmat_temporal.npy') if ((not options.force) and (os.path.isfile(simmat_temporal_fn))): die('similarity matrix exists: %s; use force option' % simmat_temporal_fn) catalog_ref_fn = os.path.join(conf.project_dir, 'catalog.pf') if os.path.isfile(catalog_ref_fn): allevents = model.load_events(catalog_ref_fn) else: die('catalog missing: %s' % catalog_ref_fn) if conf.sw_simmat: if not os.path.isfile(conf.sim_mat_fn): die('similarity matrix missing: %s' % conf.sim_mat_fn) if conf.sim_mat_type == 'binary': if os.path.isfile(conf.sim_mat_fn): simmat = sccluster.load_similarity_matrix(conf.sim_mat_fn) else: die('cannot read similarity matrix: %s' % conf.sim_mat_fn) else: die('ascii format for similarity matrix not yet implemented') if len(allevents) != len(simmat): print(len(allevents), len(simmat)) die('clustering stopped, number of events ' + 'differs from matrix size') new_catalog_fn = os.path.join(conf.project_dir, 'events_to_be_clustered.pf') model.dump_events(allevents, new_catalog_fn) else: if conf.metric in config.acceptable_mt_based_metrics: events = [ev for ev in allevents if ev.moment_tensor is not None] else: events = [ev for ev in allevents] new_catalog_fn = os.path.join(conf.project_dir, 'events_to_be_clustered.pf') model.dump_events(events, new_catalog_fn) simmat = sccluster.compute_similarity_matrix(events, conf.metric) sccluster.save_similarity_matrix(simmat, simmat_temporal_fn) simmat_fig_fn = os.path.join(conf.project_dir, 'simmat_temporal.' + conf.figure_format) if options.view and options.savefig: scplot.view_and_savefig_similarity_matrix(simmat, simmat_fig_fn, 'Sorted chronologically') else: if options.view: scplot.view_similarity_matrix(simmat, 'Sorted chronologically') if options.savefig: scplot.savefig_similarity_matrix(simmat, simmat_fig_fn, 'Sorted chronologically') print('Similarity matrix computed and stored as "%s"' % simmat_temporal_fn) if options.savefig: print('Similarity matrix figure saved as "%s"' % simmat_fig_fn)