Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Speedup 01 Quicker grouping of templates #524

Merged
Merged
2 changes: 2 additions & 0 deletions CHANGES.md
Original file line number Diff line number Diff line change
@@ -1,4 +1,6 @@
## Current
* core.match_filter.template
- new quick_group_templates function for 50x quicker template grouping.

## 0.4.4
* core.match_filter
Expand Down
33 changes: 33 additions & 0 deletions eqcorrscan/core/match_filter/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -707,6 +707,39 @@ def group_templates(templates):
return template_groups


def quick_group_templates(templates):
"""
Group templates into sets of similarly processed templates.

:type templates: List of Tribe of Templates
:return: List of Lists of Templates.
"""
# Get the template's processing parameters
processing_tuples = [
tuple(
[value for key, value in template.__dict__.items()
if key not in ['name', 'st', 'prepick', 'event', 'template_info']]
)
for template in templates]
# Get list of unique parameter-tuples. Sort it so that the order in which
# the groups are processed is consistent across different runs.
uniq_processing_parameters = sorted(list(set(processing_tuples)))
# sort templates into groups
template_groups = []
for parameter_combination in uniq_processing_parameters:
# find indices of tuples in list with same parameters
template_indices_for_group = [
j for j, param_tuple in enumerate(processing_tuples)
if param_tuple == parameter_combination]

new_group = list()
for template_index in template_indices_for_group: #[0]:
calum-chamberlain marked this conversation as resolved.
Show resolved Hide resolved
# use indices to sort templates into groups
new_group.append(templates[int(template_index)])
template_groups.append(new_group)
return template_groups


if __name__ == "__main__":
import doctest

Expand Down
6 changes: 4 additions & 2 deletions eqcorrscan/core/match_filter/tribe.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@
from obspy import Catalog, Stream, read, read_events
from obspy.core.event import Comment, CreationInfo

from eqcorrscan.core.match_filter.template import Template, group_templates
from eqcorrscan.core.match_filter.template import (
Template, group_templates, quick_group_templates)
calum-chamberlain marked this conversation as resolved.
Show resolved Hide resolved
from eqcorrscan.core.match_filter.party import Party
from eqcorrscan.core.match_filter.helpers import (
_safemembers, _par_read, get_waveform_client)
Expand Down Expand Up @@ -584,7 +585,8 @@ def detect(self, stream, threshold, threshold_type, trig_int, plot=False,
length is the number of channels within this template.
"""
party = Party()
template_groups = group_templates(self.templates)
# template_groups = group_templates(self.templates)
template_groups = quick_group_templates(self.templates)
if len(template_groups) > 1 and pre_processed:
raise NotImplementedError(
"Inconsistent template processing and pre-processed data - "
Expand Down
13 changes: 13 additions & 0 deletions eqcorrscan/tests/match_filter_test.py
Original file line number Diff line number Diff line change
Expand Up @@ -586,6 +586,19 @@ def test_tribe_copy(self):
self.assertEqual(t, copy_t)
self.assertEqual(tribe, copied)

def test_tribe_copy_quick(self):
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This looks like the wrong test for this PR.

Copy link
Collaborator Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Ups.. thanks for removing!

from robustraqn.templates_creation import _quick_tribe_copy
calum-chamberlain marked this conversation as resolved.
Show resolved Hide resolved
party = Party().read(
filename=os.path.join(
os.path.abspath(os.path.dirname(__file__)),
'test_data', 'test_party.tgz'))
tribe = Tribe(f.template for f in party.families)
copied = _quick_tribe_copy(tribe)
self.assertEqual(len(tribe), len(copied))
for t, copy_t in zip(tribe.templates, copied.templates):
self.assertEqual(t, copy_t)
self.assertEqual(tribe, copied)


@pytest.mark.network
class TestTribeConstruction(unittest.TestCase):
Expand Down