def test_SaveFails(self): print(SEGY) data = NoInitRadarData() with self.assertRaises(ImportError): data.save_as_segy( os.path.join(THIS_DIR, 'input_data', 'shots0001_0200_resave.segy'))
def test_plot_power(self, mock_show): # Only checking that these do not throw errors dat = NoInitRadarData(big=True) with self.assertRaises(TypeError): plot.plot_power(dat, [12, 14]) with self.assertRaises(ValueError): plot.plot_power(dat, 0) dat.picks = Picks(dat) dat.picks.add_pick(10) dat.picks.power[:] = 10.5 # works with constant power fig, ax = plot.plot_power(dat, 10) # works with various inputs fig, ax = plt.subplots() plot.plot_power(dat, 10, fig=fig) plot.plot_power(dat, 10, fig=fig, ax=ax) plot.plot_power(dat, 10, clims=(-100, 100), fig=fig, ax=ax) # works with multiple inputs plot.plot_power([dat, dat], 10, fig=fig, ax=ax) # works with projected coordinates dat.x_coord = np.arange(dat.data.shape[1]) dat.y_coord = np.arange(dat.data.shape[1]) plot.plot_power(dat, 10, fig=fig, ax=ax) plot.plot_power([dat, dat], 10, fig=fig, ax=ax) with self.assertRaises(ValueError): plot.plot_power(dat, 0, fig=fig, ax=ax) # gets ok lims with variable power? dat.picks.power[:, 0] = 1 plot.plot_power(dat, 10, fig=fig, ax=ax)
def test_plot_radargram_flattenlayer(self, mock_show): dat = NoInitRadarData(big=True) dat.picks = Picks(dat) dat.picks.add_pick(10) dat.picks.power[:] = 10 dat.picks.samp1[:] = 0 dat.picks.samp2[:] = 1 # make sure no bugs if this is actually constant dat.picks.samp3[:] = 3 # works with constant power fig, ax = plot.plot_radargram(dat, flatten_layer=10) # make sure we can actually follow a variable layer dat.picks.samp2[:, 1:] = 2 dat.picks.samp2[:, -1] = 4 # works with constant power fig, ax = plot.plot_radargram(dat, flatten_layer=10) dat.picks.samp2[:] = 0 # make sure no bugs if this is at the top fig, ax = plot.plot_radargram(dat, flatten_layer=10) dat.picks.samp2[:] = dat.data.shape[ 0] - 1 # make sure no bugs if this is at the bottom fig, ax = plot.plot_radargram(dat, flatten_layer=10) dat.picks.samp2[:, 1] = np.NaN # make sure no bugs if this is at the bottom fig, ax = plot.plot_radargram(dat, flatten_layer=10) with self.assertRaises(ValueError): fig, ax = plot.plot_radargram(dat, flatten_layer=1)
def test_plot_traces(self, mock_show): # Only checking that these do not throw errors dat = NoInitRadarData(big=True) fig, ax = plot.plot_traces(dat, 0) fig, ax = plt.subplots() plot.plot_traces(dat, 0, fig=fig) plot.plot_traces(dat, 0, fig=fig, ax=ax) plot.plot_traces(dat, [1, 1], fig=fig, ax=ax) plot.plot_traces(dat, [1, 18], fig=fig, ax=ax) with self.assertRaises(ValueError): plot.plot_traces(dat, np.arange(10), fig=fig, ax=ax) with self.assertRaises(IndexError): plot.plot_traces(dat, 999, fig=fig, ax=ax) # no nmo plot.plot_traces(dat, 0, ydat='depth', fig=fig, ax=ax) # with nmo dat.nmo_depth = np.arange(10) plot.plot_traces(dat, 0, ydat='depth', fig=fig, ax=ax) with self.assertRaises(ValueError): plot.plot_traces(dat, 0, ydat='dum', fig=fig, ax=ax) # Make sure we handle axes rescaling ok dat.data[:, 0] = 10 dat.data[:, 1] = -10 plot.plot_traces(dat, (0, 2), fig=fig, ax=ax)
def test_sustolt_seisunix(self): data = NoInitRadarData(big=True) data.dt = 1.0e-9 data.travel_time = data.travel_time * 1.0e-9 data.fn = os.path.join(THIS_DIR, 'input_data', 'rectangle_sustolt.mat') with self.assertRaises(FileNotFoundError): migrationlib.migrationSeisUnix(data)
def test_plot_spectrogram(self, mock_show): # Only checking that these do not throw errors dat = NoInitRadarData(big=True) dat.picks = Picks(dat) dat.picks.samp1 = np.ones((2, len(dat.lat))) dat.picks.samp2 = np.ones((2, len(dat.lat))) dat.picks.samp3 = np.ones((2, len(dat.lat))) fig, ax = plot.plot_spectrogram(dat, (0., 5.0)) plot.plot_spectrogram(dat, (0., 5.0), fig=fig) plot.plot_spectrogram(dat, (0., 5.0), fig=fig, ax=ax) plot.plot_spectrogram(dat, (0., 5.0), window='hamming') plot.plot_spectrogram(dat, (0., 5.0), scaling='density') # no error if freq high plot.plot_spectrogram(dat, 100) # freq too low with self.assertRaises(ValueError): plot.plot_spectrogram(dat, (0., -100)) with self.assertRaises(ValueError): plot.plot_spectrogram(dat, (0., 5), scaling='dummy') with self.assertRaises(ValueError): plot.plot_spectrogram(dat, (0., 5), window='dummy')
def test_failure_3(self): dat = NoInitRadarData(big=True) dat.picks = Picks(dat) dat.picks.samp1 = np.ones((2, len(dat.lat))) dat.picks.samp2 = np.ones((2, len(dat.lat))) dat.picks.samp3 = np.ones((2, len(dat.lat))) with self.assertRaises(AttributeError): plot.plot_specdense(dat, 'bad')
def test_compKirchoff_cython(self): data = NoInitRadarData(big=True) pdata = NoInitRadarData(big=True) data = mig_cython.migrationKirchhoff(data) pdata = mig_python.migrationKirchhoff(pdata) data.data[np.isnan(data.data)] = 0. self.assertTrue(np.allclose(data.data, pdata.data))
def test_plot_picks_via_radargram(self, mock_show): """We want to be able to call this via plot_radargram""" dat = NoInitRadarData(big=True) dat.picks = Picks(dat) dat.picks.samp1 = np.ones((2, len(dat.lat))) dat.picks.samp2 = np.ones((2, len(dat.lat))) dat.picks.samp3 = np.ones((2, len(dat.lat))) dat.picks.picknums = [0, 9] plot.plot_radargram(dat, pick_colors='mgm')
def test_check_data_shape(self): data = NoInitRadarData(big=True) # should pass, i.e. nothing happens migrationlib._check_data_shape(data) # make it fail data.data = np.ones((1, 1)) with self.assertRaises(ValueError): migrationlib._check_data_shape(data)
def test_output_csv_nolayers(self): rd = NoInitRadarData() rd.output_csv(os.path.join(THIS_DIR, 'input_data', 'test.csv')) with open(os.path.join(THIS_DIR, 'input_data', 'test.csv')) as fin: lines = fin.readlines() # we should only have three entries: lat, lon, trace in header and data self.assertEqual(len(lines[0].split(',')), 3) self.assertEqual(len(lines[1].split(',')), 3) # we should have a row per trace, plus a header self.assertEqual(len(lines), rd.tnum + 1)
def testWriteWithPicksFull(self): rd = NoInitRadarData() rd.picks = Picks(rd) rd.picks.add_pick() rd.save(os.path.join(THIS_DIR, 'input_data', 'test_out.mat')) data = RadarData(os.path.join(THIS_DIR, 'input_data', 'test_out.mat')) self.assertTrue(data.picks is not None) self.assertTrue(data.picks.lasttrace is not None) self.assertTrue(data.picks.samp1 is not None) self.assertTrue(data.picks.samp2 is not None) self.assertTrue(data.picks.samp3 is not None)
def test_getVelocityProfile(self): data = NoInitRadarData(big=True) self.assertEqual(1.68e8, migrationlib.getVelocityProfile(data, 1.68e8)) # need reasonable input here for 2d. Needs a different travel time. data.travel_time = data.travel_time / 10. migrationlib.getVelocityProfile( data, np.genfromtxt( os.path.join(THIS_DIR, 'input_data', 'velocity_layers.txt'))) # this should still work since we are close data.travel_time = data.travel_time / 10. twod = np.genfromtxt( os.path.join(THIS_DIR, 'input_data', 'velocity_layers.txt')) twod = twod * 0.0045 + 1.0e-7 * twod[1] migrationlib.getVelocityProfile(data, twod) # need reasonable input here for 3d data = NoInitRadarData(big=True) migrationlib.getVelocityProfile( data, np.genfromtxt( os.path.join(THIS_DIR, 'input_data', 'velocity_lateral.txt'))) # Bad distance with good 3d grid data.dist = None with self.assertRaises(ValueError): migrationlib.getVelocityProfile( data, np.genfromtxt( os.path.join(THIS_DIR, 'input_data', 'velocity_lateral.txt'))) data = NoInitRadarData(big=True) # this should fail on bad z twod_vel = 1.68e8 * np.ones((10, 2)) twod_vel[:, 1] = 0. with self.assertRaises(ValueError): migrationlib.getVelocityProfile(data, twod_vel) # Use some bad x values with self.assertRaises(ValueError): migrationlib.getVelocityProfile(data, 1.68e8 * np.ones((10, 3))) # bad z values threed_vel = 1.68e8 * np.ones((10, 3)) threed_vel[:, -1] = np.arange(10) * 1000. with self.assertRaises(ValueError): migrationlib.getVelocityProfile(data, threed_vel) # Make sure we reject bad input shapes with self.assertRaises(ValueError): migrationlib.getVelocityProfile(data, 1.68e8 * np.ones((8, ))) with self.assertRaises(ValueError): migrationlib.getVelocityProfile(data, 1.68e8 * np.ones((8, 1))) with self.assertRaises(ValueError): migrationlib.getVelocityProfile(data, 1.68e8 * np.ones((1, 2))) with self.assertRaises(ValueError): migrationlib.getVelocityProfile(data, 1.68e8 * np.ones((8, 4)))
def test_concat(self): dats = process.concat([NoInitRadarData(), NoInitRadarData()]) self.assertTrue(dats[0].data.shape == (2, 4)) with self.assertRaises(ValueError): d2 = NoInitRadarData() d2.snum = 3 dats = process.concat([NoInitRadarData(), d2]) with self.assertRaises(ValueError): d2 = NoInitRadarData() d2.travel_time = np.array((2, 3)) dats = process.concat([NoInitRadarData(), d2])
def test_PhaseShiftVariable(self): data = NoInitRadarData(big=True) data.travel_time = data.travel_time / 10. data = migrationlib.migrationPhaseShift(data, vel_fn=os.path.join( THIS_DIR, 'input_data', 'velocity_layers.txt')) data = NoInitRadarData(big=True) with self.assertRaises(TypeError): data = migrationlib.migrationPhaseShift(data, vel_fn=os.path.join( THIS_DIR, 'input_data', 'notafile.txt'))
def test_plot_hft(self, mock_show): # Only checking that these do not throw errors dat = NoInitRadarData(big=True) plot.plot_hft(dat) fig, ax = plt.subplots() plot.plot_hft(dat, fig=fig, ax=ax) plot.plot_hft(dat, fig=fig)
def test_interp(self, mock_kgc, mock_kgm): dats = [NoInitRadarData(big=True)] dats[0].constant_space = MagicMock() gpslib.interp(dats, 10., fn='dum.csv') self.assertTrue(len(mock_kgc.mock_calls) > 0) self.assertTrue(len(dats[0].constant_space.mock_calls) > 0) dats[0].constant_space = MagicMock() gpslib.interp(dats, 10., fn='dum.mat') self.assertTrue(len(mock_kgm.mock_calls) > 0) self.assertTrue(len(dats[0].constant_space.mock_calls) > 0) with self.assertRaises(Exception): gpslib.interp(dats, 10., fn='dum.badext') dats = [NoInitRadarData(big=True)] dats[0].constant_space = MagicMock() gpslib.interp(dats, 10.) self.assertTrue(len(dats[0].constant_space.mock_calls) > 0)
def test_HighPassBadcutoff(self): radardata = NoInitRadarData() # fails without constant-spaced data radardata.flags.interp = np.ones((2, )) with self.assertRaises(ValueError): # We have a screwed up filter here because of sampling vs. frequency used radardata.highpass(1.0e-4)
def test_vbp_fir(self): radardata = NoInitRadarData() radardata.vertical_band_pass(1., 10., filttype='fir', order=100) radardata.vertical_band_pass(1., 10., filttype='fir', order=2, fir_window='hanning')
def test_plot_radargram_figaxin(self): # Only checking that these do not throw errors dat = NoInitRadarData(big=True) fig, ax = plot.plot_radargram(dat) fig, ax = plt.subplots() fig, ax = plot.plot_radargram(dat, fig=fig, ax=ax) fig, ax = plt.subplots() fig, ax = plot.plot_radargram(dat, fig=fig)
def test_plot_power(self): # Only checking that these do not throw errors dat = NoInitRadarData(big=True) with self.assertRaises(TypeError): fig, ax = plot.plot_power(dat, [12, 14]) with self.assertRaises(ValueError): fig, ax = plot.plot_power(dat, 0) dat.picks = Picks(dat) dat.picks.add_pick(10) dat.picks.power[:] = 10 # works with constant power fig, ax = plot.plot_power(dat, 10) with self.assertRaises(ValueError): fig, ax = plot.plot_power(dat, 0) # gets ok lims with variable power? dat.picks.power[:, 0] = 1 fig, ax = plot.plot_power(dat, 10)
def test_plot_picks(self): # Only checking that these do not throw errors dat = NoInitRadarData(big=True) fig, ax = plot.plot_picks(dat, np.arange(int(dat.tnum)), dat.travel_time) dat.picks = Picks(dat) dat.picks.samp1 = np.ones((2, len(dat.lat))) dat.picks.samp2 = np.ones((2, len(dat.lat))) dat.picks.samp3 = np.ones((2, len(dat.lat))) fig, ax = plot.plot_picks(dat, np.arange(int(dat.tnum)), dat.travel_time) fig, ax = plot.plot_picks(dat, np.arange(int(dat.tnum)), dat.travel_time, colors='g') fig, ax = plot.plot_picks(dat, np.arange(int(dat.tnum)), dat.travel_time, colors='gmm') fig, ax = plot.plot_picks(dat, np.arange(int(dat.tnum)), dat.travel_time, colors=['c', 'g']) fig, ax = plot.plot_picks(dat, np.arange(int(dat.tnum)), dat.travel_time, colors=['cmy', 'brb']) fig, ax = plot.plot_picks(dat, np.arange(int(dat.tnum)), dat.travel_time, colors=['cm', 'br']) with self.assertRaises(ValueError): fig, ax = plot.plot_picks(dat, np.arange(int(dat.tnum)), dat.travel_time, colors=['c', 'm', 'b'])
def test_HighPass(self): radardata = NoInitRadarData() # fails without constant-spaced data radardata.flags.interp = np.ones((2, )) radardata.highpass(1000.0) # There is no high-frequency variability, so this result should be small # We only have residual variability from the quality of the filter print( np.abs((radardata.data - radardata.data[0, 0]) / radardata.data[0, 0])) self.assertTrue( np.all( np.abs((radardata.data - radardata.data[0, 0]) / radardata.data[0, 0]) < 1.0e-3))
def test_plot_traces(self): # Only checking that these do not throw errors dat = NoInitRadarData(big=True) fig, ax = plot.plot_traces(dat, 0) fig, ax = plot.plot_traces(dat, [1, 1]) fig, ax = plot.plot_traces(dat, [1, 18]) with self.assertRaises(ValueError): fig, ax = plot.plot_traces(dat, np.arange(10)) with self.assertRaises(IndexError): fig, ax = plot.plot_traces(dat, 999) # no nmo fig, ax = plot.plot_traces(dat, 0, ydat='depth') # with nmo dat.nmo_depth = np.arange(10) fig, ax = plot.plot_traces(dat, 0, ydat='depth') with self.assertRaises(ValueError): fig, ax = plot.plot_traces(dat, 0, ydat='dum') # Make sure we handle axes rescaling ok dat.data[:, 0] = 10 dat.data[:, 1] = -10 fig, ax = plot.plot_traces(dat, (0, 2))
def test_plot_specdense(self): # Only checking that these do not throw errors dat = NoInitRadarData(big=True) dat.picks = Picks(dat) dat.picks.samp1 = np.ones((2, len(dat.lat))) dat.picks.samp2 = np.ones((2, len(dat.lat))) dat.picks.samp3 = np.ones((2, len(dat.lat))) fig, ax = plot.plot_specdense(dat, 3.0e-7) plot.plot_specdense(dat, 3.0e-7, fig=fig) plot.plot_specdense(dat, 3.0e-7, fig=fig, ax=ax) plot.plot_specdense(dat, 3.0e-7, window='hamming') plot.plot_specdense(dat, 3.0e-7, scaling='density') # freq too low with self.assertRaises(ValueError): plot.plot_specdense(dat, -100) with self.assertRaises(ValueError): plot.plot_specdense(dat, 3.0e-7, scaling='dummy') with self.assertRaises(ValueError): plot.plot_specdense(dat, 3.0e-7, window='dummy')
def test__get_pick_targ_infoBadSelections(self): # Make sure that we are selecting the proper output format rd = NoInitRadarData() # Try depth when there is no depth rd.nmo_depth = None with self.assertRaises(AttributeError): out_name, tout = rd._get_pick_targ_info('depth') # Elevation with no depth or elevation with self.assertRaises(AttributeError): out_name, tout = rd._get_pick_targ_info('elev') # Elevation with depth but not elevation rd.nmo_depth = np.arange(len(rd.travel_time)) * 1.1 with self.assertRaises(AttributeError): out_name, tout = rd._get_pick_targ_info('elev') # Now try to pass a bad value for the selection with self.assertRaises(ValueError): out_name, tout = rd._get_pick_targ_info('dummy') with self.assertRaises(ValueError): out_name, tout = rd._get_pick_targ_info(['dummy', 'snum'])
def test_kinematic_gps_mat(self, mock_kgc): dats = [NoInitRadarData(big=True)] gpslib.kinematic_gps_mat( dats, os.path.join(THIS_DIR, 'input_data', 'gps_control.mat')) self.assertTrue( np.allclose(np.arange(0, 2.0, 0.1), mock_kgc.call_args[0][1])) self.assertTrue( np.allclose(np.arange(0, 200, 10), mock_kgc.call_args[0][2])) self.assertTrue( np.allclose(np.arange(0, 2000, 100), mock_kgc.call_args[0][3])) self.assertTrue( np.allclose(np.arange(0, 20, 1), mock_kgc.call_args[0][4])) with self.assertRaises(ValueError): gpslib.kinematic_gps_mat(dats, os.path.join(THIS_DIR, 'input_data', 'gps_control_badfields.mat'), extrapolate=False)
def test__get_pick_targ_infoGoodSelections(self): # Make sure that we are selecting the proper output format rd = NoInitRadarData() rd.nmo_depth = np.arange(len(rd.travel_time)) * 1.1 rd.elev = np.arange(rd.tnum) * 1001 out_name, tout = rd._get_pick_targ_info('twtt') self.assertEqual(out_name, 'twtt') self.assertTrue(np.all(tout == rd.travel_time)) out_name, tout = rd._get_pick_targ_info('depth') self.assertEqual(out_name, 'depth') self.assertTrue(np.all(tout == rd.nmo_depth)) out_name, tout = rd._get_pick_targ_info('snum') self.assertEqual(out_name, 'snum') self.assertTrue(np.all(tout == np.arange(rd.snum))) out_name, tout = rd._get_pick_targ_info('elev') self.assertEqual(out_name, 'elev')
def test__get_pick_targ_infoAutoselect(self): # Make sure that we are selecting the proper output format rd = NoInitRadarData() # With no depth, we should output travel time rd.nmo_depth = None out_name, tout = rd._get_pick_targ_info(None) self.assertEqual(out_name, 'twtt') self.assertTrue(np.all(tout == rd.travel_time)) # With depth, return depth rd.nmo_depth = np.arange(len(rd.travel_time)) * 1.1 out_name, tout = rd._get_pick_targ_info(None) self.assertEqual(out_name, 'depth') self.assertTrue(np.all(tout == rd.nmo_depth))
def test_kinematic_gps_csv(self, mock_kgc): dats = [NoInitRadarData(big=True)] gpslib.kinematic_gps_csv( dats, os.path.join(THIS_DIR, 'input_data', 'gps_control.csv')) self.assertTrue( np.allclose(np.arange(0, 2.0, 0.1), mock_kgc.call_args[0][1])) self.assertTrue( np.allclose(np.arange(0, 200, 10), mock_kgc.call_args[0][2])) self.assertTrue( np.allclose(np.arange(0, 2000, 100), mock_kgc.call_args[0][3])) self.assertTrue( np.allclose(np.arange(0, 20, 1), mock_kgc.call_args[0][4])) with self.assertRaises(ValueError): gpslib.kinematic_gps_csv(dats, os.path.join(THIS_DIR, 'input_data', 'gps_control.csv'), names='dumbanddummer') with self.assertRaises(ValueError): gpslib.kinematic_gps_csv(dats, os.path.join(THIS_DIR, 'input_data', 'gps_control.csv'), names='dumb,dumb,and,dummer')