Conversation
There was a problem hiding this comment.
Pull request overview
This PR optimizes SrcRec.write() in pytomoatt by avoiding per-source DataFrame filtering/itertuples() over receiver data, aiming to significantly reduce runtime when writing large src_rec files.
Changes:
- Precomputes receiver/common-source/common-receiver output lines grouped by
src_indexto avoid repeated DataFrame filtering inside the source loop. - Switches source iteration to
itertuples(name=None)and writes prebuilt receiver blocks via dictionary lookups.
Suppressed comments (2)
pytomoatt/src_rec.py:532
- Common-source receiver record formatting reduces
weightto 3 decimals (%7.3f), which changes persisted values compared to the previous 4-decimal formatting. If weights are meant to be preserved across write→read, keep the previous precision.
"%7d %7d %6s %9.4f %9.4f %9.4f %7d %6s %9.4f %9.4f %9.4f %s %8.4f %7.3f\n" % row
pytomoatt/src_rec.py:559
- Common-receiver receiver record formatting reduces
weightto 3 decimals (%7.3f), which changes persisted values compared to the previous 4-decimal formatting. If weights are meant to be preserved across write→read, keep the previous precision.
"%7d %7d %6s %9.4f %9.4f %9.4f %7d %6s %9.4f %9.4f %9.4f %s %8.4f %7.3f\n" % row
💡 Add Copilot custom instructions for smarter, more guided reviews. Learn how to get started.
| + origin_time.nanosecond * 1e-9 | ||
| ) | ||
| output.write("{:d} {} {} {} {} {} {} {:.4f} {:.4f} {:.4f} {:.4f} {} {} {:.4f}\n".format( | ||
| output.write("%7d %6d %2d %2d %2d %2d %5.2f %9.4f %9.4f %9.4f %5.2f %7d %s %7.3f\n" % ( |
| for src in tqdm.tqdm( | ||
| src_points.itertuples(), | ||
| src_points.itertuples(name=None), | ||
| total=src_points.shape[0], | ||
| desc="Writing src_rec file", | ||
| ): |
| ): | ||
| src_index, rec_index, staname, stla, stlo, stel, phase, tt, weight = row | ||
| rec_lines_by_src.setdefault(src_index, []).append( | ||
| "%7d %7d %6s %9.4f %9.4f %9.4f %s %8.4f %7.3f\n" % ( |
| + origin_time.nanosecond * 1e-9 | ||
| ) | ||
| output.write("{:d} {} {} {} {} {} {} {:.4f} {:.4f} {:.4f} {:.4f} {} {} {:.4f}\n".format( | ||
| output.write("%7d %6d %2d %2d %2d %2d %5.2f %9.4f %9.4f %9.4f %5.2f %7d %s %7.3f\n" % ( |
Codecov Report✅ All modified and coverable lines are covered by tests. 📢 Thoughts on this report? Let us know! |
|
@copilot Fix the code for all comments in this review thread. When a review comment includes a suggested change, apply the suggestion exactly. Do not make changes beyond what is described in the linked review thread. |
…oundtrip regression test
All issues from the review have been addressed in commit
|
…se f-strings for formatting
Accelerate sr.write:
1m20s -> 1.6 s
by preparing text info before loop.
Frequently using 'itertuples' is computationally expensive.