Ejemplo n.º 1
0
    def test_set_referred_object_warning(self):
        """
        Setting a referred object equal to an object that is equal to the last
        referred object should not emit a warning.
        """
        def assert_differnt_referred_object_warning_issued(w):
            """ assert the different referred object warning is emitted """
            self.assertEqual(len(w), 1)
            self.assertIn('is not equal', str(w[0].message))

        obj1 = UTCDateTime(10)
        obj2 = UTCDateTime(11)
        obj3 = UTCDateTime(10)

        rid1 = ResourceIdentifier('abc123', referred_object=obj1)
        # this should raise a warning as the new object != old object
        with WarningsCapture() as w:
            rid2 = ResourceIdentifier('abc123', referred_object=obj2)
        assert_differnt_referred_object_warning_issued(w)

        with WarningsCapture() as w:
            rid2.set_referred_object(obj1)
        assert_differnt_referred_object_warning_issued(w)

        # set the referred object back to previous state, this should warn
        with WarningsCapture() as w:
            ResourceIdentifier('abc123', referred_object=obj2)
        assert_differnt_referred_object_warning_issued(w)

        # this should not emit a warning since obj1 == obj3
        with WarningsCapture() as w:
            rid1.set_referred_object(obj3)
        self.assertEqual(len(w), 0)
Ejemplo n.º 2
0
 def test_same_resource_id_different_referred_object(self):
     """
     Tests the handling of the case that different ResourceIdentifier
     instances are created that have the same resource id but different
     objects. The referred objects should still return the same objects
     used in the ResourceIdentifier construction or set_referred_object
     call. However, if an object is set to a resource_id that is not
     equal to the last object set it should issue a warning.
     """
     warnings.simplefilter('default')
     object_a = UTCDateTime(1000)
     object_b = UTCDateTime(1000)
     object_c = UTCDateTime(1001)
     self.assertFalse(object_a is object_b)
     id = 'obspy.org/tests/test_resource'
     res_a = ResourceIdentifier(id=id, referred_object=object_a)
     # Now create a new resource with the same id but a different object.
     # This should not raise a warning as the object a and b are equal.
     with WarningsCapture() as w:
         res_b = ResourceIdentifier(id=id, referred_object=object_b)
         self.assertEqual(len(w), 0)
     # if the set object is not equal to the last object set to the same
     # resource_id, however, a warning should be issued.
     with WarningsCapture() as w:
         res_c = ResourceIdentifier(id=id, referred_object=object_c)
         self.assertEqual(len(w), 1)
         expected_text = 'which is not equal to the last object bound'
         self.assertIn(expected_text, str(w[0]))
     # even though the resource_id are the same, the referred objects
     # should point to the original (different) objects
     self.assertIs(object_a, res_a.get_referred_object())
     self.assertIs(object_b, res_b.get_referred_object())
     self.assertIs(object_c, res_c.get_referred_object())
Ejemplo n.º 3
0
    def test_read_file_with_non_valid_blocks_in_between(self):
        """
        Test reading MiniSEED files that have some non-valid blocks in-between.
        """
        # This file has two 4096 bytes records.
        filename = os.path.join(self.path, 'data', 'test.mseed')
        with io.open(filename, "rb") as fh:
            rec1 = fh.read(4096)
            rec2 = fh.read(4096)

        reference = _read_mseed(filename)
        del reference[0].stats.mseed

        # Fill with zero bytes.
        for length in (128, 256, 512, 1024, 2048, 4096, 8192):
            with io.BytesIO() as buf:
                buf.write(rec1)
                buf.write(b'\x00' * length)
                buf.write(rec2)
                buf.seek(0, 0)
                # This will raise 1 warning per 128 bytes.
                with WarningsCapture() as w:
                    st = _read_mseed(buf)
                self.assertEqual(len(w), length // 128)

            # Also explicitly test the first warning message which should be
            # identical for all cases.
            self.assertEqual(
                w[0].message.args[0],
                "readMSEEDBuffer(): Not a SEED record. Will skip bytes "
                "4096 to 4223.")

            # Should always be two records.
            self.assertEqual(st[0].stats.mseed.number_of_records, 2)

            # Remove things like file-size and what not.
            del st[0].stats.mseed
            self.assertEqual(reference, st)

        # Try the same thing but fill with random bytes.
        # The seed is not really needed but hopefully guards against the
        # very very rare case of random bytes making up a valid SEED record.
        np.random.seed(34980)
        for length in (128, 256, 512, 1024, 2048, 4096, 8192):
            with io.BytesIO() as buf:
                buf.write(rec1)
                buf.write(np.random.bytes(length))
                buf.write(rec2)
                buf.seek(0, 0)
                # This will raise 1 warning per 128 bytes.
                with WarningsCapture() as w:
                    st = _read_mseed(buf)
                self.assertEqual(len(w), length // 128)

            # Should always be two records.
            self.assertEqual(st[0].stats.mseed.number_of_records, 2)

            # Remove things like file-size and what not.
            del st[0].stats.mseed
            self.assertEqual(reference, st)
Ejemplo n.º 4
0
    def test_broken_last_record(self):
        """
        Test if Libmseed is able to read files with broken last record. Use
        both methods, readMSTracesViaRecords and readMSTraces
        """
        file = os.path.join(self.path, "data", "brokenlastrecord.mseed")

        # independent reading of the data, 128 Bytes header
        d = np.fromfile(file, dtype=np.uint8)[128:]
        data = util._unpack_steim_2(d, 5980, swapflag=self.swap, verbose=0)

        # test readMSTraces. Will raise an internal warning.
        with WarningsCapture() as w:
            data_record = _read_mseed(file)[0].data

        # This will raise 18 (!) warnings. It will skip 17 * 128 bytes due
        # to it not being a SEED records and then complain that the remaining
        # 30 bytes are not enough to constitute a full SEED record.
        self.assertEqual(len(w), 18)
        self.assertEqual(w[0].category, InternalMSEEDWarning)

        # Test against reference data.
        self.assertEqual(len(data_record), 5980)
        last10samples = [
            2862, 2856, 2844, 2843, 2851, 2853, 2853, 2854, 2857, 2863
        ]
        np.testing.assert_array_equal(data_record[-10:], last10samples)

        # Also test against independently unpacked data.
        np.testing.assert_allclose(data_record, data)
Ejemplo n.º 5
0
 def test_station_response_plot(self, image_path):
     """
     Tests the response plot.
     """
     sta = read_inventory()[0][0]
     with WarningsCapture():
         sta.plot(0.05, channel="*[NE]", outfile=image_path)
Ejemplo n.º 6
0
    def test_casted_stats_nscl_writes_to_mseed(self):
        """
        Ensure a Stream object that has had its nslc types cast to str can
        still be written.
        """
        st = Stream(traces=read()[0])

        # Get a new stats object with just the basic items in it
        stats_items = set(Stats())
        new_stats = Stats()
        new_stats.__dict__.update({x: st[0].stats[x] for x in stats_items})
        with WarningsCapture():
            new_stats.network = 1
            new_stats.station = 1.1
        new_stats.channel = 'Non'
        st[0].stats = new_stats
        # try writing stream to bytes buffer
        bio = io.BytesIO()
        st.write(bio, 'mseed')
        bio.seek(0)
        # read bytes and compare
        stt = read(bio)
        # remove _mseed so streams can compare equal
        stt[0].stats.pop('mseed')
        del stt[0].stats._format  # format gets added upon writing
        assert st == stt
Ejemplo n.º 7
0
    def test_catalog_resource_ids(self):
        """
        Basic tests on the catalog resource ids.
        """
        cat1 = read_events()
        # The resource_id attached to the first event is self-pointing
        self.assertIs(cat1[0], cat1[0].resource_id.get_referred_object())
        # make a copy and re-read catalog
        cat2 = cat1.copy()
        cat3 = read_events()
        # the resource_id on the new catalogs point to attached objects
        self.assertIs(cat1[0], cat1[0].resource_id.get_referred_object())
        self.assertIs(cat2[0], cat2[0].resource_id.get_referred_object())
        self.assertIs(cat3[0], cat3[0].resource_id.get_referred_object())
        # now delete cat1 and make sure cat2 and cat3 still work
        del cat1
        self.assertIs(cat2[0], cat2[0].resource_id.get_referred_object())
        self.assertIs(cat3[0], cat3[0].resource_id.get_referred_object())
        # create a resource_id with the same id as the last defined object
        # with the same resource id (that is still in scope) is returned
        new_id = cat2[0].resource_id.id
        rid = ResourceIdentifier(new_id)

        self.assertIs(rid.get_referred_object(), cat3[0])
        del cat3

        gc.collect()  # Call gc to ensure WeakValueDict works
        # raises UserWarning, suppress to keep std out cleaner
        with WarningsCapture():
            self.assertIs(rid.get_referred_object(), cat2[0])
            del cat2
            self.assertIs(rid.get_referred_object(), None)
Ejemplo n.º 8
0
 def test_writing_using_core(self):
     """
     Tests the writing of SEGY rev1 files using obspy.core. It just compares
     the output of writing using obspy.core with the output of writing the
     files using the internal SEGY object which is thoroughly tested in
     obspy.io.segy.tests.test_segy.
     """
     for file, _ in self.files.items():
         file = os.path.join(self.path, file)
         # Read the file with the internal SEGY representation.
         segy_file = _read_segy_internal(file)
         # Read again using core.
         st = _read_segy(file)
         # Create two temporary files to write to.
         with NamedTemporaryFile() as tf1:
             out_file1 = tf1.name
             with NamedTemporaryFile() as tf2:
                 out_file2 = tf2.name
                 # Write twice and catch header warnings
                 with WarningsCapture():
                     warnings.simplefilter("ignore")
                     segy_file.write(out_file1)
                     _write_segy(st, out_file2)
                 # Read and delete files.
                 with open(out_file1, 'rb') as f1:
                     data1 = f1.read()
                 with open(out_file2, 'rb') as f2:
                     data2 = f2.read()
         # Test if they are equal.
         self.assertEqual(data1[3200:3600], data2[3200:3600])
Ejemplo n.º 9
0
 def test_response_plot(self, image_path):
     """
     Tests the response plot.
     """
     cha = read_inventory()[0][0][0]
     with WarningsCapture():
         rcParams['savefig.dpi'] = 72
         cha.plot(0.005, outfile=image_path)
Ejemplo n.º 10
0
 def test_response_plot(self, image_path):
     """
     Tests the response plot.
     """
     net = read_inventory()[0]
     t = UTCDateTime(2008, 7, 1)
     with WarningsCapture():
         net.plot_response(0.002, output="DISP", channel="B*E",
                           time=t, outfile=image_path)
Ejemplo n.º 11
0
 def test_get_pick_from_arrival_on_copied_catalog_doesnt_warn(self):
     """
     Ensure a copied catalog doesn't raise a warning when
     get_referred_object is called on a resource_id.
     """
     cat = create_diverse_catalog().copy()
     arrival = cat[0].origins[0].arrivals[0]
     with WarningsCapture() as w:
         arrival.pick_id.get_referred_object()
     self.assertEqual(len(w), 0)
Ejemplo n.º 12
0
    def test_mutative_methods_deprecation(self):
        """
        Because Resource ids are hashable they should be immutable. Make
        sure any methods that mutate resource_ids are deprecated. Currently
        there are two:

        1. `convert_id_to_quakeml_uri`
        2. `regnerate_uuid`
        """
        rid = ResourceIdentifier('not_a_valid_quakeml_uri')
        with WarningsCapture() as w:
            rid.convert_id_to_quakeml_uri()
        self.assertGreaterEqual(len(w), 1)
        self.assertTrue([isinstance(x, ObsPyDeprecationWarning) for x in w])

        rid = ResourceIdentifier()
        with WarningsCapture() as w:
            rid.regenerate_uuid()
        self.assertGreaterEqual(len(w), 1)
        self.assertTrue([isinstance(x, ObsPyDeprecationWarning) for x in w])
Ejemplo n.º 13
0
    def test_warning_capture(self):
        """
        Tests for the WarningsCapture class in obspy.core.util.testing
        """
        # ensure a warning issued with warn is captured. Before, this could
        # raise a TypeError.
        with WarningsCapture() as w:
            warnings.warn('something bad is happening in the world')

        assert len(w) == 1
        assert 'something bad' in str(w.captured_warnings[0].message)
Ejemplo n.º 14
0
 def test_response_plot(self, image_path):
     """
     Tests the response plot.
     """
     resp = read_inventory()[0][0][0].response
     with WarningsCapture():
         resp.plot(0.001,
                   output="VEL",
                   start_stage=1,
                   end_stage=3,
                   outfile=image_path)
Ejemplo n.º 15
0
    def test_resetting_id_warns_on_set_id(self):
        """
        Because the ResourceIdentifier class hashes on the id attribute, it
        should warn if it is being changed. This tests the case were an id is
        manually specified.
        """
        rid = ResourceIdentifier('a very unique string indeed')
        with WarningsCapture() as w:
            warnings.simplefilter('default')
            rid.id = 'Another string that will mess up the hash. Bad.'

        self.assertEqual(len(w), 1)
        self.assertIn('overwritting the id attribute', str(w[0].message))
Ejemplo n.º 16
0
 def test_response_plot(self, image_path):
     """
     Tests the response plot.
     """
     inv = read_inventory()
     t = UTCDateTime(2008, 7, 1)
     with WarningsCapture():
         inv.plot_response(0.01,
                           output="ACC",
                           channel="*N",
                           station="[WR]*",
                           time=t,
                           outfile=image_path)
Ejemplo n.º 17
0
    def test_issue_2278(self):
        """
        Tests for issue # 2278 which has to do with resource ids returning
        the wrong objects when the bound object has gone out of scope and
        a new object adopts the old object's python id.
        """

        # Create a simple class for resource_ids to refere to.
        class Simple(object):
            def __init__(self, value):
                self.value = value

        parent = Simple('parent1')

        # keep track of objects, resource_ids, and used python ids
        obj_list1, rid_list1, used_ids1 = [], [], set()
        # create a slew of objects and resource_ids
        for _ in range(100):
            obj_list1.append(Simple(1))
            used_ids1.add(id(obj_list1[-1]))
        for obj in obj_list1:
            kwargs = dict(referred_object=obj, parent=parent)
            rid_list1.append(ResourceIdentifier(**kwargs))
        # delete objects and create second set
        del obj_list1

        # create another slew of objects and resource_ids. Some will reuse
        # deleted object ids
        obj_list2, rid_list2, used_ids2 = [], [], set()
        for _ in range(100):
            obj_list2.append(Simple(2))
            used_ids2.add(id(obj_list2[-1]))
        for obj in obj_list2:
            kwargs = dict(referred_object=obj, parent=parent)
            rid_list2.append(ResourceIdentifier(**kwargs))

        # since we cannot control which IDs python uses, skip the test if
        # no overlapping ids were created.
        if not used_ids1 & used_ids2:
            self.skipTest('setup requires reuse of python ids')

        # Iterate over first list of ids. Referred objects should be None
        for rid in rid_list1:
            # should raise a warning
            with WarningsCapture():
                self.assertIs(rid.get_referred_object(), None)
Ejemplo n.º 18
0
    def test_response_list_stage(self):
        """
        This is quite rare but it happens.
        """
        inv = read_inventory(os.path.join(self.data_dir, "IM_IL31__BHZ.xml"))

        sampling_rate = 40.0
        t_samp = 1.0 / sampling_rate
        nfft = 100

        with WarningsCapture():
            cpx_response, freq = inv[0][0][0].response.get_evalresp_response(
                t_samp=t_samp,
                nfft=nfft,
                output="VEL",
                start_stage=None,
                end_stage=None)

        # Cut of the zero frequency.
        cpx_response = cpx_response[1:]

        amp = np.abs(cpx_response)
        phase = np.angle(cpx_response)
        freq = freq[1:]

        # The expected output goes from 1 to 20 Hz - its somehow really hard
        # to get evalresp to produce results for the desired frequencies so
        # I just gave up on it.
        exp_f, exp_amp, exp_ph = np.loadtxt(
            os.path.join(self.data_dir,
                         "expected_response_IM_IL31__BHZ.txt")).T
        # Interpolate.
        exp_amp = scipy.interpolate.InterpolatedUnivariateSpline(exp_f,
                                                                 exp_amp,
                                                                 k=3)(freq)
        exp_ph = scipy.interpolate.InterpolatedUnivariateSpline(exp_f,
                                                                exp_ph,
                                                                k=3)(freq)
        exp_ph = np.deg2rad(exp_ph)

        # The output is not exactle the same as ObsPy performs a different
        # but visually quite a bit better interpolation.
        np.testing.assert_allclose(amp, exp_amp, rtol=1E-3)
        np.testing.assert_allclose(phase, exp_ph, rtol=1E-3)
Ejemplo n.º 19
0
    def test_mopad_fallback(self, image_path):
        """
        Test the fallback to mopad.
        """
        mt = [0.000, -1.232e25, 1.233e25, 0.141e25, -0.421e25, 2.531e25]

        with WarningsCapture() as w:
            # Always raise warning.
            beachball(mt, outfile=image_path)

        # Make sure the appropriate warnings has been raised.
        assert w
        # Filter
        w = [_i.message.args[0] for _i in w]
        w = [
            _i for _i in w
            if "falling back to the mopad wrapper" in _i.lower()
        ]
        assert w
Ejemplo n.º 20
0
 def test_cartesian_many_phases_one_way_depr(self, model, image_path):
     """
     Same as above but checking deprecation warning.
     """
     arrivals = model.get_ray_paths(500, 140)
     # check warning message on deprecated routine
     with WarningsCapture() as w:
         arrivals.plot(plot_type="cartesian", plot_all=False,
                       show=False, legend=False)
         plt.savefig(image_path)
         assert len(w) >= 1
         for w_ in w:
             try:
                 assert str(w_.message) == 'The plot() function is ' \
                     'deprecated. Please use arrivals.plot_rays()'
                 assert w_.category == ObsPyDeprecationWarning
             except AssertionError:
                 continue
             break
         else:
             raise
Ejemplo n.º 21
0
 def test_getting_gc_with_shared_resource_id(self):
     """
     Test that calling get_referred_object on a resource id whose object
     has been garbage collected, but that has another object that shares
     the same resource_id, returns the other object with the same resource
     id and issues a warning
     """
     uri = 'testuri'
     obj1 = UTCDateTime(1000)
     obj2 = UTCDateTime(1000)
     rid1 = ResourceIdentifier(uri, referred_object=obj1)
     rid2 = ResourceIdentifier(uri, referred_object=obj2)
     assert not (rid1.get_referred_object() is rid2.get_referred_object())
     del obj1
     with WarningsCapture() as w:
         rid1.get_referred_object()
         assert len(w) == 1
         assert 'The object with identity' in str(w[0])
     # now both rids should return the same object
     assert rid1.get_referred_object() is rid2.get_referred_object()
     # the object id should now be bound to obj2
     assert rid1._object_id == rid2._object_id
Ejemplo n.º 22
0
 def test_ray_plot_mismatching_axes_type_warnings(self, model):
     """
     Test warnings when attempting ray path plots in spherical/cartesian
     with bad axes type (polar/not polar).
     """
     arrivals = model.get_ray_paths(500, 20, phase_list=['P'])
     # polar pot attempted in cartesian axes
     fig, ax = plt.subplots()
     expected_message = ("Axes instance provided for plotting with "
                         "`plot_type='spherical'` but it seems the axes is "
                         "not a polar axes.")
     try:
         with warnings.catch_warnings(record=True) as w:
             warnings.simplefilter("always")
             # this raises an exception as well:
             # "AttributeError: 'AxesSubplot' object has no attribute "
             # "'set_theta_zero_location'"
             with pytest.raises(AttributeError):
                 arrivals.plot_rays(plot_type="spherical", ax=ax,
                                    show=False)
         assert len(w) == 1
         assert str(w[0].message) == expected_message
         assert w[0].category == UserWarning
     finally:
         plt.close(fig)
     # cartesian pot attempted in polar axes
     fig, ax = plt.subplots(subplot_kw=dict(projection='polar'))
     expected_message = ("Axes instance provided for plotting with "
                         "`plot_type='cartesian'` but it seems the axes is "
                         "a polar axes.")
     try:
         with WarningsCapture() as w:
             arrivals.plot_rays(plot_type="cartesian", ax=ax, show=False)
         assert len(w) == 1
         assert str(w[0].message) == expected_message
         assert issubclass(w[0].category, Warning)
     finally:
         plt.close(fig)
Ejemplo n.º 23
0
 def test_clear_method_resets_objects(self):
     """
     Tests that the clear() method properly resets all objects. Test for
     #449.
     """
     # Test with basic event object.
     e = Event(force_resource_id=False)
     e.comments.append(Comment(text="test"))
     e.event_type = "explosion"
     assert len(e.comments) == 1
     assert e.event_type == "explosion"
     e.clear()
     assert e == Event(force_resource_id=False)
     assert len(e.comments) == 0
     assert e.event_type is None
     # Test with pick object. Does not really fit in the event test case but
     # it tests the same thing...
     p = Pick()
     p.comments.append(Comment(text="test"))
     p.phase_hint = "p"
     assert len(p.comments) == 1
     assert p.phase_hint == "p"
     # Add some more random attributes. These should disappear upon
     # cleaning.
     with WarningsCapture() as w:
         p.test_1 = "a"
         p.test_2 = "b"
         # two warnings should have been issued by setting non-default keys
         assert len(w) == 2
     assert p.test_1 == "a"
     assert p.test_2 == "b"
     p.clear()
     assert len(p.comments) == 0
     assert p.phase_hint is None
     assert not hasattr(p, "test_1")
     assert not hasattr(p, "test_2")