def create_catalog(mseed_file): cat = read_events(mseed_file.with_suffix('.xml')) if cat[0].preferred_origin() is not None: evaluation_mode = cat[0].preferred_origin().evaluation_mode else: evaluation_mode = cat[0].origins[-1].evaluation_mode return (str(mseed_file), settings.event_type_lookup[cat[0]['event_type']], evaluation_mode)
def get_catalog(event_file, inventory): cat = read_events(event_file) for i, pick in enumerate(cat[0].picks): for sensor in inventory.sensors: if sensor.alternate_code == pick.waveform_id.station_code: cat[0].picks[i].waveform_id.network_code = inventory[0].code cat[0].picks[i].waveform_id.station_code = sensor.station.code cat[0].picks[i].waveform_id.location_code = \ sensor.location_code cat[0].picks[i].waveform_id.channel_code = \ sensor.channels[0].code break return cat
def create_training_signal(waveform_file): # logger.info('here') waveform_file_path = input_data_dir / waveform_file cat = read_events(waveform_file_path.with_suffix('.xml')) st = read(waveform_file_path) event_time = cat[0].preferred_origin().time.timestamp try: st = st.detrend('demean').detrend('linear').taper( max_percentage=0.1, max_length=0.01).resample(sampling_rate=sampling_rate) except Exception as e: logger.error(e) trs = [] for tr in st: if np.nan in tr.data: continue trs.append(tr.copy()) st.traces = trs trs = [] snrs = [] pick_times = [] logger.info(event_type_lookup[cat[0].event_type]) if event_type_lookup[cat[0].event_type] == 'seismic event': for arrival in cat[0].preferred_origin().arrivals: site = arrival.pick.site for tr in st.select(site=site): snr = calculate_snr(tr, arrival.pick.time, pre_wl=20e-3, post_wl=20e-3) if snr < snr_threshold: continue tr.trim(starttime=tr.stats.starttime, endtime=tr.stats.starttime + sequence_length_second, pad=True, fill_value=0) trs.append( tr.copy().resample(sampling_rate=int(sampling_rate))) snrs.append(snr) pick_times.append(arrival.pick.time) else: return ct = 0 for tr, pick_time in zip(trs, pick_times): for i in range(0, 5): try: start = np.random.randint( 0, number_sample - int(0.25 * number_sample)) pick_sample = int( (pick_time - tr.stats.starttime) * tr.stats.sampling_rate) start_sample = int(pick_sample - start) data = tr.data[start_sample:start_sample + number_sample] data = data - np.mean(data) data = np.abs(data) / np.abs(data).max() pick = start out_dict = {'data': data, 'pick': pick} out_dir = output_data_dir out_dir.mkdir(parents=True, exist_ok=True) filename = f'{event_time:0.0f}_{tr.stats.site}_' \ f'{tr.stats.channel}_{i}.pickle' with open(out_dir / filename, 'wb') as file_out: pickle.dump(out_dict, file_out) except Exception as e: logger.error(e)
def get_event_id(filename): cat = read_events(filename) return str(filename.stem), cat[0].resource_id.id
# # plt.clf() # # plt.imshow(spec) # # plt.pause(0.1) # # # prediction = ec.predict_spectrogram(spec) # # print(prediction) # # # input() # # specs.append(spec) # # return specs for fle in tqdm(glob('/data_1/ot-reprocessed-data/*.xml')): try: cat, st = read_events(fle), read(fle.replace('.xml', '.mseed')) except Exception as e: logger.error(e) continue predictions = ec2.predict(st) n_seismic_events = ( predictions[0] == event_dict['seismic event']).sum().item() n_blasts = (predictions[0] == event_dict['blast']).sum().item() n_e_b = n_seismic_events + n_blasts if n_e_b >= threshold: if n_seismic_events / n_e_b >= 0.7: predicted_event_class = 'seismic event' elif n_blasts / n_e_b >= 0.7:
def create_training_image(waveform_file): # logger.info('here') waveform_file_path = input_data_dir / waveform_file cat = read_events(waveform_file_path.with_suffix('.xml')) st = read(waveform_file_path) if cat[0].preferred_origin() is not None: origin = cat[0].preferred_origin() else: origin = cat[0].origins[-1] if origin.evaluation_mode == 'automatic': return event_time = origin.time.timestamp try: st = st.detrend('demean').detrend( 'linear').taper(max_percentage=0.1, max_length=0.01).resample( sampling_rate=sampling_rate) except Exception as e: logger.error(e) trs = [] for tr in st: if np.nan in tr.data: continue trs.append(tr.copy()) st.traces = trs trs = [] logger.info(event_type_lookup[cat[0].event_type]) if event_type_lookup[cat[0].event_type] == 'seismic event': for arrival in cat[0].preferred_origin().arrivals: site = arrival.pick.site for tr in st.select(site=site).copy(): # filtering out low frequency before calculating the SNR tr = tr.filter('highpass', freq=100) snr = calculate_snr(tr, arrival.pick.time, pre_wl=20e-3, post_wl=20e-3) if arrival.pick.evaluation_mode == 'automatic': if snr < snr_threshold: continue tr.trim(starttime=tr.stats.starttime, endtime=tr.stats.starttime + sequence_length_second, pad=True, fill_value=0) trs.append(tr.copy().resample(sampling_rate= int(sampling_rate))) elif event_type_lookup[cat[0].event_type] == 'blast': for tr in st: if (np.max(np.abs(tr.data)) / np.std(tr.data)) < 6: continue trs.append(tr.trim(endtime=tr.stats.starttime + sequence_length_second)) else: for tr in st: trs.append(tr.trim(endtime=tr.stats.starttime + sequence_length_second)) ct = 0 for tr in trs: try: spec = spectrogram(tr) except Exception as e: logger.error(e) ct += 1 logger.info(f'{ct} exceptions') return spec = (spec / np.max(spec)) * 255 img = Image.fromarray(np.array(spec.tolist()).astype(np.uint8)) filename = f'{event_time:0.0f}_{tr.stats.site}_' \ f'{tr.stats.channel}.jpg' out_dir = (output_data_dir / event_type_lookup[cat[0].event_type]) out_dir.mkdir(parents=True, exist_ok=True) out_file_name = out_dir / filename ImageOps.grayscale(img).save(out_file_name, format='JPEG')
def create_training_dataset(waveform_file): # logger.info('here') waveform_file_path = input_data_dir / waveform_file cat = read_events(waveform_file_path.with_suffix('.xml')) st = read(waveform_file_path) event_time = cat[0].preferred_origin().time.timestamp trs = [] for tr in st.copy(): if np.any(np.isnan(tr.data)): continue trs.append(tr.copy()) st2 = Stream(traces=trs) # try: st2 = st2.detrend('demean').detrend('linear') st2 = st2.taper(max_percentage=0.1, max_length=0.01) st2 = st2.resample(sampling_rate=sampling_rate) # except Exception as e: # logger.error(e) # trs = [] # for tr in st.copy(): # try: # tr = tr.detrend('demean').detrend('linear') # tr = tr.taper(max_percentage=0.1, # max_length=0.01) # tr = tr.resample(sampling_rate=sampling_rate) # trs.append(tr.copy()) # except Exception as e: # logger.error(e) # continue # # st2 = Stream(traces=trs) trs = [] # logger.info(event_type_lookup[cat[0].event_type]) if event_type_lookup[cat[0].event_type] == 'seismic event': for arrival in cat[0].preferred_origin().arrivals: site = arrival.pick.site for tr in st2.select(site=site).copy(): # filtering out low frequency before calculating the SNR tr = tr.filter('highpass', freq=100) snr = calculate_snr(tr, arrival.pick.time, pre_wl=20e-3, post_wl=20e-3) if arrival.pick.evaluation_mode == 'automatic': if snr < snr_threshold: continue tr.trim(starttime=tr.stats.starttime, endtime=tr.stats.starttime + sequence_length_second, pad=True, fill_value=0) trs.append( tr.copy().resample(sampling_rate=int(sampling_rate))) elif event_type_lookup[cat[0].event_type] == 'blast': for tr in st2: if (np.max(np.abs(tr.data)) / np.std(tr.data)) < 6: continue trs.append( tr.trim(endtime=tr.stats.starttime + sequence_length_second)) else: for tr in st2: trs.append( tr.trim(endtime=tr.stats.starttime + sequence_length_second)) ct = 0 for tr in trs: event_type = event_type_lookup[cat[0].event_type] filename = f'{event_time:0.0f}_{tr.stats.site}_' \ f'{tr.stats.channel}.pickle' output_dir = output_data_dir / event_type output_file = output_dir / filename output_dir.mkdir(parents=True, exist_ok=True) out_dict = { 'data': tr.data, 'seismogram file': waveform_file_path, 'event type': event_type_lookup[cat[0].event_type] } with open(output_file, 'wb') as f_out: pickle.dump(out_dict, f_out)