-
Notifications
You must be signed in to change notification settings - Fork 19
feat: implement interactive shell and major documentation update for v2 #226
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
Merged
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,21 @@ | ||
| from sqlmodel import Session | ||
| from aimbat.db import engine | ||
| from aimbat.core import ( | ||
| create_iccs_instance, | ||
| create_snapshot, | ||
| get_default_event, | ||
| run_iccs, | ||
| run_mccc, | ||
| ) | ||
|
|
||
| with Session(engine) as session: | ||
| event = get_default_event(session) | ||
| assert event is not None | ||
|
|
||
| bound = create_iccs_instance(session, event) | ||
|
|
||
| run_iccs(session, bound.iccs, autoflip=True, autoselect=True) | ||
| create_snapshot(session, event, comment="after ICCS") | ||
|
|
||
| run_mccc(session, event, bound.iccs, all_seismograms=False) | ||
| create_snapshot(session, event, comment="after MCCC") |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,96 @@ | ||
| """ | ||
| Deduplicate events that were imported from sources reporting slightly different | ||
| origin times for the same earthquake. | ||
|
|
||
| Background | ||
| ---------- | ||
| ``add_data_to_project`` deduplicates stations by SEED code | ||
| ``(network, name, location, channel)`` — so importing the same station twice, | ||
| even with different coordinates, always reuses the existing record. Station | ||
| duplicates therefore cannot arise through the normal import path. | ||
|
|
||
| Events are deduplicated by exact origin time. When two data sources report | ||
| the same earthquake with origin times that differ by a second or two, they are | ||
| stored as *separate* ``AimbatEvent`` records. This script finds such | ||
| near-duplicate events, merges their seismograms into the canonical record | ||
| (the one with the most seismograms), averages the location and depth, then | ||
| removes the duplicates. | ||
|
|
||
| Run this script *before* starting any processing, and take a snapshot | ||
| afterwards so the clean state is recoverable. | ||
| """ | ||
|
|
||
| from pandas import Timedelta | ||
| from sqlmodel import Session, select | ||
|
|
||
| from aimbat.db import engine | ||
| from aimbat.models import AimbatEvent | ||
|
|
||
| # Merge events whose origin times differ by less than this value. | ||
| TIME_TOLERANCE = Timedelta(seconds=10) | ||
|
|
||
|
|
||
| def _mean(values: list[float]) -> float: | ||
| return sum(values) / len(values) | ||
|
|
||
|
|
||
| def _mean_opt(values: list[float | None]) -> float | None: | ||
| clean = [v for v in values if v is not None] | ||
| return sum(clean) / len(clean) if clean else None | ||
|
|
||
|
|
||
| def deduplicate_events(session: Session, tolerance: Timedelta = TIME_TOLERANCE) -> int: | ||
| """Merge event records whose origin times are within *tolerance*. | ||
|
|
||
| Events are sorted by time and clustered greedily: a new cluster begins | ||
| whenever the gap to the previous event exceeds *tolerance*. | ||
|
|
||
| For each cluster the record with the most seismograms is kept as the | ||
| canonical entry; its location and depth are updated to the group mean. | ||
|
|
||
| Returns the number of duplicate records removed. | ||
| """ | ||
| events = sorted( | ||
| session.exec(select(AimbatEvent)).all(), | ||
| key=lambda e: e.time, | ||
| ) | ||
|
|
||
| # Build clusters of near-simultaneous events. | ||
| clusters: list[list[AimbatEvent]] = [] | ||
| for event in events: | ||
| if clusters and event.time - clusters[-1][-1].time <= tolerance: | ||
| clusters[-1].append(event) | ||
| else: | ||
| clusters.append([event]) | ||
|
|
||
| removed = 0 | ||
| for cluster in clusters: | ||
| if len(cluster) < 2: | ||
| continue | ||
|
|
||
| canonical = max(cluster, key=lambda e: len(e.seismograms)) | ||
| duplicates = [e for e in cluster if e.id != canonical.id] | ||
|
|
||
| # Set location / depth to the group mean. | ||
| canonical.latitude = _mean([e.latitude for e in cluster]) | ||
| canonical.longitude = _mean([e.longitude for e in cluster]) | ||
| canonical.depth = _mean_opt([e.depth for e in cluster]) | ||
|
|
||
| for dup in duplicates: | ||
| for seis in list(dup.seismograms): | ||
| seis.event_id = canonical.id | ||
| session.add(seis) | ||
| session.flush() # apply FK changes before deleting the row | ||
| session.delete(dup) | ||
| removed += 1 | ||
|
|
||
| session.add(canonical) | ||
|
|
||
| session.commit() | ||
| return removed | ||
|
|
||
|
|
||
| with Session(engine) as session: | ||
| n = deduplicate_events(session) | ||
|
|
||
| print(f"Removed {n} duplicate event(s).") |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The changelog entry on line 22 contains a spelling error: "didn't" should read "didn't" (curly apostrophe vs straight apostrophe is acceptable, but "did't" is not present — the actual text "didn't do anything" is fine). However, line 88 contains "Re-arange" which should be "Re-arrange".