Skip to content

Commit

Permalink
Merge pull request #533 from mspass-team/bugfix_origintimematcher
Browse files Browse the repository at this point in the history
Fix several bugs in OriginTimeMatcher
  • Loading branch information
wangyinz authored May 7, 2024
2 parents 33e74d8 + 830ea8b commit cdf07be
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 15 deletions.
23 changes: 14 additions & 9 deletions python/mspasspy/db/normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -991,7 +991,7 @@ def find_one(self, mspass_object) -> tuple:
if mdlist is None:
return findreturn
elif len(mdlist) == 1:
return [mdlist, findreturn[1]]
return [mdlist[0], findreturn[1]]
elif len(mdlist) > 1:
if self.require_unique_match:
raise MsPASSError(
Expand Down Expand Up @@ -2357,7 +2357,7 @@ def __init__(
t0offset=0.0,
tolerance=4.0,
query=None,
attributes_to_load=["lat", "lon", "depth", "time"],
attributes_to_load=["_id", "lat", "lon", "depth", "time"],
load_if_defined=["magnitude"],
aliases=None,
require_unique_match=False,
Expand Down Expand Up @@ -2508,7 +2508,12 @@ class OriginTimeMatcher(DataFrameCacheMatcher):
be returned in the output of the find method. The keys listed
must ALL have defined values for all documents in the collection or
some calls to find_one will fail. Default is
["lat","lon","depth","time"]
["_id","lat","lon","depth","time"]. Note if constructing from a
DataFrame created from something like a Datascope origin table
this list will need to be changed to remove _id as it in that context
no ObjectID would normally be defined. Be warned, however, that if
used with a normalize function the _id may be required to match a
"source_id" cross reference in a seismic data object.
:type attributes_to_load: list of string defining keys in collection
documents
Expand Down Expand Up @@ -2548,9 +2553,9 @@ class OriginTimeMatcher(DataFrameCacheMatcher):
:type data_time_key: string
:param source_time_key: dataframe column name to use as source
origin time field. Default is None which is translated to
collection + "_time" (default default is "source_time").
:type source_time_key: string
origin time field. Default is "time"
:type source_time_key: string Can also be a None type which
is causes the internal value to be set to "time"
"""

def __init__(
Expand All @@ -2559,13 +2564,13 @@ def __init__(
collection="source",
t0offset=0.0,
tolerance=4.0,
attributes_to_load=["lat", "lon", "depth", "time"],
attributes_to_load=["_id", "lat", "lon", "depth", "time"],
load_if_defined=["magnitude"],
aliases=None,
require_unique_match=False,
prepend_collection_name=True,
data_time_key=None,
source_time_key=None,
source_time_key="time",
custom_null_values=None,
):
super().__init__(
Expand All @@ -2582,7 +2587,7 @@ def __init__(
self.tolerance = tolerance
self.data_time_key = data_time_key
if source_time_key is None:
self.source_time_key = collection + "_time"
self.source_time_key = "time"
else:
self.source_time_key = source_time_key

Expand Down
23 changes: 17 additions & 6 deletions python/tests/db/test_normalize.py
Original file line number Diff line number Diff line change
Expand Up @@ -146,12 +146,23 @@ def test_custom_null_values(self):
"time": np.nan,
"magnitude": np.nan,
}
originTimeMatcher = OriginTimeMatcher(
self.df, source_time_key="time", custom_null_values=null_value_dict
)
assert Metadata_cmp(
originTimeMatcher.cache.to_dict(orient="records")[-1], matched_dict
)
# Default for constructor demands _id which is not used in this
# context. DataFrame sets undefined to nan and this test will
# fail without turning _id off. _id is needed, however, in any
# id based normalization so it is an appropriate default
otm = OriginTimeMatcher(
self.df,
source_time_key="time",
custom_null_values=null_value_dict,
attributes_to_load=["lat", "lon", "depth", "time", "magnitude"],
)
print("DEBUG")
print("cache value")
val = otm.cache.to_dict(orient="records")[-1]
print(val)
print("match output")
print(matched_dict)
assert Metadata_cmp(otm.cache.to_dict(orient="records")[-1], matched_dict)


class TestObjectIdMatcher(TestNormalize):
Expand Down

0 comments on commit cdf07be

Please sign in to comment.