This commit is contained in:
Noe Brucy
2020-04-21 10:40:29 +02:00
committed by Noe Brucy
parent 77676ec9a4
commit 9e5fc8b4ad
4 changed files with 141 additions and 170 deletions
+14 -157
View File
@@ -2,26 +2,22 @@
import sys
import os
from functools import partial
import tables
import numpy as np
from scipy.stats import linregress
from numpy.polynomial.polynomial import polyfit
from scipy.ndimage.filters import gaussian_filter1d
from scipy import optimize
import matplotlib as mpl
if os.environ.get("DISPLAY", "") == "":
print("No display found. Using non-interactive Agg backend")
mpl.use("Agg")
from matplotlib.patches import Polygon
import pylab as P
from scipy.stats import linregress
import matplotlib.patches as mpatches
from matplotlib.collections import PatchCollection
from functools import partial
from numpy.polynomial.polynomial import polyfit
from scipy.ndimage.filters import gaussian_filter1d
from scipy import optimize
from comparator import *
P.rcParams["image.cmap"] = "plasma"
P.rcParams["savefig.dpi"] = 400
@@ -105,6 +101,8 @@ class Plotter(Aggregator, BaseProcessor):
# Define rules
self.def_rules()
self.save = None
def _not_self_dep(self, name, dep, dep_arg, overwrite, **kwargs):
if dep in self.comp.rules:
done = self.comp.process(dep, dep_arg, overwrite, overwrite)
@@ -460,7 +458,7 @@ class Plotter(Aggregator, BaseProcessor):
coordinates="figure",
)
def _plot_radial(self, name, label=None, xlog=False, ylog=False):
def _plot_radial(self, name, ax_los, label=None, xlog=False, ylog=False):
radial_bins = self.save.get_node("/radial/radial_bins_" + ax_los).read()
bin_centers = 0.5 * (radial_bins[1:] + radial_bins[:-1])
@@ -481,8 +479,8 @@ class Plotter(Aggregator, BaseProcessor):
def _plot_hist(
self,
name,
ax_los,
run,
ax_los=None,
run="",
label=None,
unit=None,
unit_coeff=1.0,
@@ -500,6 +498,9 @@ class Plotter(Aggregator, BaseProcessor):
**kwargs
):
if not ax_los is None:
name = name + "_" + ax_los
node = self.save.get_node("/hist/" + name)
if xlog is None:
@@ -1045,147 +1046,3 @@ class Plotter(Aggregator, BaseProcessor):
)
super(Plotter, self).def_rules()
class InteractiveGUI:
"""
This is a matplotlib interactive session to restrain analysis to a specific area
"""
def onbuttonrelease(self, event):
"""Deal with click events"""
button = ["left", "middle", "right"]
toolbar = P.get_current_fig_manager().toolbar
if toolbar.mode == "zoom rect" and event.inaxes == self.ax_col:
print("zooming ")
xlim = self.ax_col.get_xlim()
ylim = self.ax_col.get_ylim()
self.reset_mask()
elif self.add_mask and event.inaxes == self.ax_col:
self.plot_side()
P.draw()
def onbuttonpress(self, event):
"""Deal with click events"""
button = ["left", "middle", "right"]
toolbar = P.get_current_fig_manager().toolbar
if toolbar.mode != "":
print(
"You clicked on something, but toolbar is in mode {:s}.".format(
toolbar.mode
)
)
print(self.add_mask)
if self.add_mask and toolbar.mode == "" and event.inaxes == self.ax_col:
ix, iy = event.xdata, event.ydata
print("Add patch {}, {}".format(ix, iy))
xlim = self.ax_col.get_xlim()
ylim = self.ax_col.get_ylim()
radius = 0.05 * min(abs(xlim[1] - xlim[0]), abs(ylim[1] - ylim[0]))
circle = mpatches.Circle(
[ix, iy], radius, color="black", alpha=0.1, ec="none"
)
self.circles.append(circle)
self.ax_col.add_artist(circle)
self.ax_col.draw_artist(circle)
self.patch_mask = self.patch_mask | (
(self.xx - ix) ** 2 + (self.yy - iy) ** 2 < radius ** 2
)
# self.plot_side()
def onkeypress(self, event):
"""whenever a key is pressed"""
if not event.inaxes:
return
if event.key == "t":
self.add_mask = not self.add_mask
print("Add mode is {}".format(self.add_mask))
elif event.key == "r":
self.reset_mask()
def plot_side(self):
if self.add_mask:
mask = (self.patch_mask & self.mask).flatten()
else:
mask = self.mask.flatten()
self.ax_gamma.clear()
P.sca(self.ax_gamma)
plot_dcsdrho(self.fluct_maps, mask, tag=self.tag)
self.ax_pdf.clear()
P.sca(self.ax_pdf)
sigma_pdf(self.fluct_maps, mask, tag=self.tag, nb_bin_hist=self.args.pdf_nb_bin)
def reset_mask(self):
xlim = self.ax_col.get_xlim()
ylim = self.ax_col.get_ylim()
self.mask = (
(self.xx >= xlim[0])
& (self.xx <= xlim[1])
& (self.yy >= ylim[0])
& (self.yy <= ylim[1])
)
self.patch_mask = np.full(self.mask.shape, False)
for circle in self.circles:
circle.remove()
self.circles = []
self.plot_side()
P.draw()
def __init__(self, path, run, num, path_out=None, pp_params=default_params()):
"""
Interactive plotting
"""
self.add_mask = False
self.circles = []
self.tag = tag
# Get plotter object
self.plot = Plotter(path, [run], [num], path_out, pp_params)
im_extent = maps_disk["im_extent"]
fig = P.figure()
self.ax_col = P.subplot(1, 2, 1)
coldens = maps_disk["coldens_z"]
im = self.ax_col.imshow(
coldens, extent=im_extent, origin="lower", norm=mpl.colors.LogNorm()
)
if set_lim:
im.set_clim(0.01, 100)
self.ax_col.set_xlabel(r"$x$")
self.ax_col.set_ylabel(r"$y$")
self.xx, self.yy, self.fluct_maps = disk_pdf(
path,
num,
maps_disk,
tag=self.tag,
force=True,
put_title=False,
interactive=True,
)
coord_flat = zip(self.xx.flatten(), self.yy.flatten())
self.ax_gamma = P.subplot(2, 2, 2)
self.ax_pdf = P.subplot(2, 2, 4)
xlim = self.ax_col.get_xlim()
ylim = self.ax_col.get_ylim()
self.mask = (
(self.xx >= xlim[0])
& (self.xx <= xlim[1])
& (self.yy >= ylim[0])
& (self.yy <= ylim[1])
)
self.patch_mask = np.full(self.mask.shape, False)
self.plot_side()
fig.canvas.mpl_connect("button_release_event", self.onbuttonrelease)
fig.canvas.mpl_connect("button_press_event", self.onbuttonpress)
fig.canvas.mpl_connect("key_press_event", self.onkeypress)
P.tight_layout()
P.show()