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

Add new script to visualize the energy output(s) #980

Open
wants to merge 3 commits into
base: master
Choose a base branch
from

Conversation

fabian-kutschera
Copy link
Collaborator

Allows to plot the energy output(s), also of multiple input files, as part of the SIO group meeting

Allows to plot the energy output(s), also of multiple input files, as part of the SIO group meeting
@fabian-kutschera
Copy link
Collaborator Author

@jwjeremy

@codecov-commenter
Copy link

codecov-commenter commented Oct 20, 2023

Codecov Report

All modified and coverable lines are covered by tests ✅

Comparison is base (f0d0fd3) 14.35% compared to head (7babdff) 13.99%.
Report is 70 commits behind head on master.

❗ Your organization needs to install the Codecov GitHub app to enable full functionality.

Additional details and impacted files
@@            Coverage Diff             @@
##           master     #980      +/-   ##
==========================================
- Coverage   14.35%   13.99%   -0.37%     
==========================================
  Files         253      254       +1     
  Lines       14253    14864     +611     
==========================================
+ Hits         2046     2080      +34     
- Misses      12207    12784     +577     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

parser.add_argument("--dpi", default=300, type=int, help="Dots per Inch")
parser.add_argument("-o", "--Output", help='Name of the output file', default='energy_output.png')
args = parser.parse_args()

Copy link
Contributor

@jwjeremy jwjeremy Oct 20, 2023

Choose a reason for hiding this comment

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

Allow wildcards file path

Suggested change
### search through wildcards
files = []
for _f in args.Input:
files.extend(glob.glob(_f))

import argparse
import sys
import os

Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
import glob

parser.add_argument("-o", "--Output", help='Name of the output file', default='energy_output.png')
args = parser.parse_args()

for i in range(0,len(args.Input)):
Copy link
Contributor

Choose a reason for hiding this comment

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

Suggested change
for i in range(0,len(args.Input)):
for i in range(0,len(files)):

for j in range(0,len(args.Data)):
if (args.Data[j].upper() == "MR"):
energy_output = "moment_rate"
df[energy_output] = np.gradient(df["seismic_moment"], df["time"].iloc[1])
Copy link
Contributor

Choose a reason for hiding this comment

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

Add default units

Suggested change
df[energy_output] = np.gradient(df["seismic_moment"], df["time"].iloc[1])
df[energy_output] = np.gradient(df["seismic_moment"], df["time"].iloc[1])
args.ylabel = 'Seismic moment [Nm]' if args.ylabel==""

Copy link
Contributor

@jwjeremy jwjeremy left a comment

Choose a reason for hiding this comment

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

Nice useful tool @fabian-kutschera. I added a few changes to allow wildcards and default units if ylabel option is empty

@Thomas-Ulrich
Copy link
Contributor

Well this does not work with the latest format of energy output:
https://seissol.readthedocs.io/en/latest/energy-output.html#postprocessing-and-plotting

(base) ulrich@ulrich-ThinkPad-T490s:~/SeisSol/SeisSol$ head /home/ulrich/trash/./output_model_2603_s205_second_event_noDCFS/Turkey_2events_31mio_o5_2603_s205-energy.csv -n 100
time,variable,measurement
0,gravitational_energy,0
0,acoustic_energy,0
0,acoustic_kinetic_energy,0
0,elastic_energy,0
0,elastic_kinetic_energy,0
0,plastic_moment,0
0,total_frictional_work,0
0,static_frictional_work,0
0,seismic_moment,0
0,plastic_moment,0
0.1,total_frictional_work,0
0.1,static_frictional_work,0
0.1,seismic_moment,0
0.1,plastic_moment,0
0.2,total_frictional_work,5.425e+07
0.2,static_frictional_work,1.60529e+07
0.2,seismic_moment,2.52645e+12
0.2,plastic_moment,0
0.3,total_frictional_work,1.72581e+10
0.3,static_frictional_work,9.15028e+09
0.3,seismic_moment,2.7248e+14
0.3,plastic_moment,0

@Thomas-Ulrich
Copy link
Contributor

Hi, I get the following error:

(base) ulrich@ulrich-ThinkPad-T490s:~/SeisSol/SeisSol$ python3 postprocessing/visualization/tools/plot_EnergyOutput.py /home/ulrich/trash/output_tpv1213/tpv13-energy.csv
File selected: /home/ulrich/trash/output_tpv1213/tpv13-energy.csv
Traceback (most recent call last):
  File "/home/ulrich/SeisSol/SeisSol/postprocessing/visualization/tools/plot_EnergyOutput.py", line 55, in <module>
    df = df.pivot_table(index="time", columns="variable", values="measurement")
  File "/home/ulrich/mambaforge3/lib/python3.10/site-packages/pandas/core/frame.py", line 9190, in pivot_table
    return pivot_table(
  File "/home/ulrich/mambaforge3/lib/python3.10/site-packages/pandas/core/reshape/pivot.py", line 99, in pivot_table
    table = __internal_pivot_table(
  File "/home/ulrich/mambaforge3/lib/python3.10/site-packages/pandas/core/reshape/pivot.py", line 145, in __internal_pivot_table
    raise KeyError(i)
KeyError: 'measurement'

Appart from that:
I would format it with black, e.g.
black -l 79 --preview postprocessing/visualization/tools/plot_EnergyOutput.py
I would try to fix flake8 errors
flake8 postprocessing/visualization/tools/plot_EnergyOutput.py
and see what are the suggestions.
consider using f strings for formating strings
print(f"M0 = {M0:.2e} and Mw = {Mw:.2f}")

if args.ylabel: is better than if args.ylabel != "":

using a dictionary would be more pythonic:

        elif args.Data[j].upper() == "GE":
            energy_output = "gravitational_energy"
        elif args.Data[j].upper() == "AE":
            energy_output = "acoustic_energy"
        elif args.Data[j].upper() == "AKE":
            energy_output = "acoustic_kinetic_energy"
        elif args.Data[j].upper() == "EE":
            energy_output = "elastic_energy"
        elif args.Data[j].upper() == "EKE":
            energy_output = "elastic_kinetic_energy"
        elif args.Data[j].upper() == "TFW":
            energy_output = "total_frictional_work"
        elif args.Data[j].upper() == "SFW":
            energy_output = "static_frictional_work"
        elif args.Data[j].upper() == "SM":
            energy_output = "seismic_moment"
        elif args.Data[j].upper() == "PM":
            energy_output = "plastic_moment"

this #!/usr/bin/env python3 allows running the script without writing python yourscript.py, but your should add executable permission to the script chmod +x yourscript.py

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

4 participants