Skip to content
Snippets Groups Projects

Cli improvements

Merged Jameson Rollins requested to merge jameson.rollins/pygwinc:cli-improvements into master
Files
2
+ 51
38
@@ -3,7 +3,6 @@ import os
@@ -3,7 +3,6 @@ import os
import signal
import signal
import argparse
import argparse
import numpy as np
import numpy as np
from IPython.terminal.embed import InteractiveShellEmbed
import logging
import logging
logging.basicConfig(
logging.basicConfig(
@@ -51,6 +50,7 @@ See documentation for inspiral_range package for details.
@@ -51,6 +50,7 @@ See documentation for inspiral_range package for details.
IFO = 'aLIGO'
IFO = 'aLIGO'
FREQ = '5:3000:6000'
FREQ = '5:3000:6000'
 
DATA_SAVE_FORMATS = ['.hdf5', '.h5']
parser = argparse.ArgumentParser(
parser = argparse.ArgumentParser(
prog='gwinc',
prog='gwinc',
@@ -75,23 +75,23 @@ group.add_argument(
@@ -75,23 +75,23 @@ group.add_argument(
'--interactive', '-i', action='store_true',
'--interactive', '-i', action='store_true',
help="interactive plot with interactive shell")
help="interactive plot with interactive shell")
group.add_argument(
group.add_argument(
'--save', '-s',
'--save', '-s', action='append',
help="save budget traces (.hdf5/.h5) or plot (.pdf/.png/.svg) to file")
help="save plot (.png/.pdf/.svg) or budget traces (.hdf5/.h5) to file (may be specified multiple times)")
group.add_argument(
group.add_argument(
'--yaml', '-y', action='store_true',
'--yaml', '-y', action='store_true',
help="print IFO as yaml to stdout and exit")
help="print IFO as yaml to stdout and exit (budget not calculated)")
group.add_argument(
group.add_argument(
'--text', '-x', action='store_true',
'--text', '-x', action='store_true',
help="print IFO as text table to stdout and exit")
help="print IFO as text table to stdout and exit (budget not calculated)")
group.add_argument(
group.add_argument(
'--diff', '-d', metavar='IFO',
'--diff', '-d', metavar='IFO',
help="show differences table between another IFO description")
help="show differences table between another IFO description and exit (budget not calculated)")
group.add_argument(
group.add_argument(
'--no-plot', '-np', action='store_false', dest='plot',
'--no-plot', '-np', action='store_false', dest='plot',
help="supress plotting")
help="supress plotting")
parser.add_argument(
parser.add_argument(
'IFO',
'IFO',
help="IFO name, description file path (.yaml, .mat, .m), budget module (.py), or HDF5 data file (.hdf5, .h5)")
help="IFO name, or path to budget module (.py), description file (.yaml/.mat/.m), or HDF5 data file (.hdf5/.h5)")
def freq_from_spec(spec):
def freq_from_spec(spec):
@@ -113,7 +113,7 @@ def main():
@@ -113,7 +113,7 @@ def main():
##########
##########
# initial arg processing
# initial arg processing
if os.path.splitext(os.path.basename(args.IFO))[1] in ['.hdf5', '.h5']:
if os.path.splitext(os.path.basename(args.IFO))[1] in DATA_SAVE_FORMATS:
from .io import load_hdf5
from .io import load_hdf5
Budget = None
Budget = None
freq, traces, attrs = load_hdf5(args.IFO)
freq, traces, attrs = load_hdf5(args.IFO)
@@ -135,6 +135,14 @@ def main():
@@ -135,6 +135,14 @@ def main():
plot_style = getattr(Budget, 'plot_style', {})
plot_style = getattr(Budget, 'plot_style', {})
traces = None
traces = None
 
out_data_files = set()
 
out_plot_files = set()
 
if args.save:
 
for path in args.save:
 
if os.path.splitext(path)[1] in DATA_SAVE_FORMATS:
 
out_data_files.add(path)
 
out_plot_files = set(args.save) - out_data_files
 
if args.ifo:
if args.ifo:
for paramval in args.ifo:
for paramval in args.ifo:
param, val = paramval.split('=', 1)
param, val = paramval.split('=', 1)
@@ -234,31 +242,12 @@ def main():
@@ -234,31 +242,12 @@ def main():
plot_style['title'] += '\n{}'.format(fom_title)
plot_style['title'] += '\n{}'.format(fom_title)
##########
##########
# output
# interactive
# save noise traces to HDF5 file
if args.save and os.path.splitext(args.save)[1] in ['.hdf5', '.h5']:
from .io import save_hdf5
logging.info("saving budget traces {}...".format(args.save))
if ifo:
plot_style['IFO'] = ifo.to_yaml()
save_hdf5(
path=args.save,
freq=freq,
traces=traces,
**plot_style
)
# interactive shell plotting
# interactive shell plotting
elif args.interactive:
if args.interactive:
 
from IPython.terminal.embed import InteractiveShellEmbed
ipshell = InteractiveShellEmbed(
ipshell = InteractiveShellEmbed(
user_ns={
'freq': freq,
'traces': traces,
'ifo': ifo,
'plot_style': plot_style,
'plot_noise': plot_noise,
},
banner1='''
banner1='''
GWINC interactive plotter
GWINC interactive plotter
@@ -266,14 +255,39 @@ You may interact with plot using "plt." methods, e.g.:
@@ -266,14 +255,39 @@ You may interact with plot using "plt." methods, e.g.:
>>> plt.title("foo")
>>> plt.title("foo")
>>> plt.savefig("foo.pdf")
>>> plt.savefig("foo.pdf")
''')
''',
 
user_ns={
 
'freq': freq,
 
'traces': traces,
 
'ifo': ifo,
 
'plot_style': plot_style,
 
'plot_noise': plot_noise,
 
},
 
)
ipshell.enable_pylab()
ipshell.enable_pylab()
ipshell.run_code("plot_noise(freq, traces, **plot_style)")
ipshell.ex("fig = plot_noise(freq, traces, **plot_style)")
ipshell.run_code("plt.title('{}')".format(plot_style['title']))
ipshell.ex("plt.title('{}')".format(plot_style['title']))
ipshell()
ipshell()
 
##########
 
# output
 
 
# save noise traces to HDF5 file
 
if out_data_files:
 
from .io import save_hdf5
 
attrs = dict(plot_style)
 
attrs['IFO'] = ifo.to_yaml()
 
for path in out_data_files:
 
logging.info("saving budget traces {}...".format(path))
 
save_hdf5(
 
path=path,
 
freq=freq,
 
traces=traces,
 
**attrs,
 
)
 
# standard plotting
# standard plotting
elif args.plot:
if args.plot or out_plot_files:
logging.info("plotting noises...")
logging.info("plotting noises...")
fig = plt.figure()
fig = plt.figure()
ax = fig.add_subplot(1, 1, 1)
ax = fig.add_subplot(1, 1, 1)
@@ -284,10 +298,9 @@ You may interact with plot using "plt." methods, e.g.:
@@ -284,10 +298,9 @@ You may interact with plot using "plt." methods, e.g.:
**plot_style
**plot_style
)
)
fig.tight_layout()
fig.tight_layout()
if args.save:
if out_plot_files:
fig.savefig(
for path in out_plot_files:
args.save,
fig.savefig(path)
)
else:
else:
plt.show()
plt.show()
Loading