Files
pipeline/pipeline_disk.py
T
2020-12-14 16:28:58 +01:00

398 lines
15 KiB
Python

# coding: utf-8
import os
import glob
from shutil import copy
import argparse
import time
import numpy as np
from functools import reduce
from pp_params import *
from plotter import *
from postprocessor import *
storage_in = '/home/nbrucy/simus/'
storage_out = '/home/nbrucy/visus/'
parser = argparse.ArgumentParser()
parser.add_argument("runs", help='name of runs', nargs='*',
default=[])
parser.add_argument("-wo", "--which_outputs",
choices=['all', 'id', 'time'],
help="Select outputs by time range, id range or all of them",
default='all')
parser.add_argument("-b", "--begin",
help="id of first output",
type=int,
default=1)
parser.add_argument("-e", "--end",
help="id of last output",
type=int,
default=100)
parser.add_argument("-s", "--step",
help="step between two output",
type=int,
default=1)
parser.add_argument("-tb", "--time_begin",
help="time of first output",
type=float,
default=0.)
parser.add_argument("-te", "--time_end",
help="time of last output",
type=float,
default=6.)
parser.add_argument("-p", "--project",
help="specify project name",
default="disk")
parser.add_argument("-fr", "--force_redo",
help="redo plots even if the files already exist",
action='store_true')
parser.add_argument("-w", "--watch",
help="wait and watch for missing outputs",
action='store_true')
parser.add_argument("--skip",
help="skip failed loadings",
action='store_true')
parser.add_argument("-wt", "--waiting_time",
help="time between to successive try when watching new outputs (in second)",
type=int,
default=120)
parser.add_argument("-af", "--allowed_failures",
help="number of allowed failures when waiting",
default=30)
parser.add_argument("-i", "--interactive",
help="Interactive mode",
action='store_true')
parser.add_argument("--tag",
help="Add a special tag on output filemanes",
default='')
parser.add_argument("-d", "--disk",
help="compute specific disk radial analysis",
action='store_true')
parser.add_argument("-pd", "--plot_disk",
help="plot specific disk radial analysis",
action='store_true')
parser.add_argument("-m", "--maps",
help="compute generic maps",
action='store_true')
parser.add_argument("-pm", "--plot_maps",
help="plot generic maps",
action='store_true')
parser.add_argument("-c", "--compare",
help="compare different runs",
action='store_true')
parser.add_argument("-ev", "--evolution",
help="plot evolution of quantities over time",
action='store_true')
parser.add_argument("--pdf",
help="plot pdf of fluctuations of column density",
action='store_true')
parser.add_argument("--pdf2",
help="plot patrick's PDF",
action='store_true')
parser.add_argument("--cpdf",
help="compare pdf",
action='store_true')
parser.add_argument("--clump",
help="Do clump study",
action='store_true')
parser.add_argument("--print_outputs",
help="print names of outputs",
action='store_true')
parser.add_argument("--fft",
help="use quick and dirty fft rendering",
action='store_true')
parser.add_argument("--images", nargs='*',
default=['coldens', 'rho', 'speed', 'Q', 'T'],
choices=['coldens', 'rho', 'speed', 'Q', 'T', 'levels', 'cpu', 'jeans', 'jeans_ratio'])
parser.add_argument("--axes", nargs='*',
default=['x', 'y', 'z'],
choices=['x', 'y', 'z'])
parser.add_argument("--zoom",
help="zoom",
type=float,
default=2.)
parser.add_argument("-ms", "--mapsize",
help="size of the maps created in he map mode (in pixel)",
type=int,
default=1024)
parser.add_argument("--nb_bin",
help="Number of bins for azimuthal averages",
type=int,
default=50)
parser.add_argument("--pdf_nb_bin",
help="Number of bins for PDF",
type=int,
default=50)
parser.add_argument("--binning",
help="Kind of binning (logarithmic or linear)",
choices=['log', 'lin'],
default='log')
parser.add_argument("--rad_ext",
help="Value of the highest bin",
type=float,
default=1.)
parser.add_argument("-x",
help="x position of the central point",
type=float,
default=1.)
parser.add_argument("-y",
help="y position of the central point",
type=float,
default=1.)
parser.add_argument("-z",
help="z position of the central point",
type=float,
default=1.)
parser.add_argument(
"-ta", "--time_avg", help="Plot time averaged comparaison", action="store_true"
)
parser.add_argument("--colormap",
help="Colormap used",
choices=dp.P.colormaps(),
default='plasma')
parser.add_argument("--format",
help="Format of the output",
choices=['png', 'jpeg', 'pdf', 'svg', 'ps'],
default='jpeg')
parser.add_argument("--dpi",
help="Resolution of the output",
type=int,
default=400)
parser.add_argument("--beamer",
help="Beamer mode",
action='store_true')
parser.add_argument("--Q_in_name",
help="Whether to use the name to have Q",
action='store_true')
args = parser.parse_args()
project = args.project
runs = args.runs
first = args.begin
last = args.end
step = args.step
rad = 0.5 / args.zoom
# extension for out files
dp.out_ext = '.' + args.format
dp.P.style.use("seaborn-deep")
if args.format == 'pdf':
dp.P.style.use("~/.config/matplotlib/pdf.mplstyle")
if args.beamer:
dp.P.rcParams['font.family'] = 'sans-serif'
dp.P.rcParams['figure.figsize'] = (7, 4.5)
# Plot properties
dp.P.rcParams['image.cmap'] = args.colormap
dp.P.rcParams['savefig.dpi'] = args.dpi
dp.P.rcParams['lines.linewidth'] = 2
dp.P.rcParams['lines.markersize'] = 10
dp.P.rcParams["errorbar.capsize"] = 4
me.P = dp.P
# List of id that were successfully computed
run_succeded = {}
# Care for dependencies
images = args.images
if 'jeans_ratio' in images and not 'jeans' in images :
images = ['jeans'] + images
if 'jeans_ratio' in images and not 'levels' in images :
images.append('levels')
if 'jeans' in images and not 'T' in images :
images.append('T')
if ('T' in images or 'jeans' in images) and not 'rho' in images :
images.append('rho')
if 'Q' in images and not 'coldens' in images :
images.append('coldens')
# Go through all runs
for run in runs:
path_suffix = project + '/' + run
path_in = storage_in + path_suffix
path_out = storage_out + path_suffix
if args.tag == '':
tag_run = run
else:
tag_run = run + '_' + args.tag
if not os.path.exists(path_out):
os.makedirs(path_out)
try:
copy(path_in + '/disk.nml', path_out)
copy(path_in + '/output_00001/compilation.txt', path_out)
except:
pass
run_succeded[run] = []
if args.which_outputs in ['all', 'time'] :
names = glob.glob(path_in + "/output_[0-9][0-9][0-9][0-9][0-9]")
nums_all = [int(n.split('/')[-1].split('_')[1]) for n in names]
nums_all = np.sort(nums_all)
if args.which_outputs == 'all':
nums = nums_all
else:
time = [dp.get_time(path_in, n) for n in nums_all]
nums = [n for i,n in enumerate(nums_all) if time[i] >= args.time_begin and time[i] < args.time_end]
else:
nums = range(first, last + 1, step)
for num in nums:
failures = 0
success = False
while not success:
try:
maps_disk = None
if args.print_outputs:
print("[{}, {}]".format(run, num))
if args.maps:
print("[{}, {}] computing maps".format(run, num))
maps_disk = dp.compute_image_data(path_in, num,
radius=rad,
path_out=path_out,
tag=tag_run,
map_size=args.mapsize,
force=args.force_redo,
axes_los=args.axes,
images=images,
pos_star=np.array([args.x, args.y, args.z]),
fft=args.fft)
print("[{}, {}] maps computed".format(run, num))
if args.plot_maps:
print("[{}, {}] plotting maps".format(run, num))
maps_disk = dp.plot_maps(path_out, num,
maps_disk=maps_disk,
axes_los=args.axes,
images=images,
tag=tag_run,
force=args.force_redo,
interactive=args.interactive,
put_title=(not args.beamer))
print("[{}, {}] maps plotted".format(run, num))
if args.disk:
print("[{}, {}] computing disk props".format(run, num))
dp.disk_prop(path_in, num, path_out=path_out,
nb_bin=args.nb_bin,
binning=args.binning,
rad_ext=args.rad_ext,
force=args.force_redo,
pos_star=np.array([args.x, args.y, args.z]))
print("[{}, {}] disk_props computed".format(run, num))
if args.plot_disk:
print("[{}, {}] plotting disk props".format(run, num))
dp.plot_disk_prop(path_out, num, tag=tag_run,
force=args.force_redo,
interactive=args.interactive,
put_title=(not args.beamer))
print("[{}, {}] disk props plotted".format(run, num))
if args.pdf:
print("[{}, {}] computing pdf #1".format(run, num))
dp.disk_pdf(path_out, num, maps_disk,
pos_star=np.array([args.x, args.y, args.z]),
force=args.force_redo,
tag=tag_run,
nb_bin_hist=args.pdf_nb_bin,
interactive=args.interactive,
put_title=(not args.beamer))
print("[{}, {}] pdf #1 computed".format(run, num))
if args.pdf2:
print("[{}, {}] computing pdf #2".format(run, num))
me.get_pdf(path_in, num, path_out=path_out,
force=args.force_redo,
tag=tag_run)
print("[{}, {}] pdf #2 computed".format(run, num))
if args.clump:
print("[{}, {}] computing clumps".format(run, num))
dp.clump_study(path_in, num, path_out, tag_run)
print("[{}, {}] clumps computed".format(run, num))
if args.interactive:
print("[{}, {}] Interactive session".format(run, num))
maps_disk = dp.interactive_plots(args, path_out, num, tag=tag_run)
# If we are here, success !
success = True
run_succeded[run].append(num)
except (ValueError, IOError, KeyError) as e:
print(e)
if(args.watch and failures < args.allowed_failures):
failures = failures + 1
print("Unable to proceed for run {} output {}. Trying again in {} s ({} tries remaining)".format(run, num, args.waiting_time, args.allowed_failures - failures))
time.sleep(args.waiting_time)
elif args.skip:
break
else:
raise
if args.evolution:
print("[{}] plotting evolution".format(run))
dp.evolution(path_out, run_succeded[run],
force=args.force_redo,
tag=args.tag,
interactive=args.interactive,
pdf=args.pdf or args.cpdf)
print("[{}] evolution plotted".format(run))
if args.compare:
path_suffix = project
path = storage_out + path_suffix
if (args.time_avg):
dp.compare(path, runs, run_succeded,
path_out=path + '/comp',
force=args.force_redo,
interactive=args.interactive,
Q_in_name=args.Q_in_name,
tag=args.tag,
pdf=args.pdf or args.cpdf)
else:
for i in range(first, last + 1, step):
# Select usable runs
runs_ok = [run for run in runs if i in run_succeded[run]]
try:
if len(runs_ok) > 1:
dp.compare(path, runs_ok, i,
path_out=path + '/comp',
force=args.force_redo,
interactive=args.interactive,
Q_in_name=args.Q_in_name,
tag=args.tag,
pdf=args.pdf or args.cpdf)
except (KeyError, IOError) as e:
print(e)
if (args.skip):
pass
else:
raise