Skip to content

Commit

Permalink
updated plotter class
Browse files Browse the repository at this point in the history
  • Loading branch information
Bing Li committed Sep 18, 2024
1 parent da7a5ee commit 2ac2acb
Show file tree
Hide file tree
Showing 110 changed files with 120 additions and 89 deletions.
2 changes: 1 addition & 1 deletion scripts/HoV6Sn6_contour.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import matplotlib.pyplot as plt

from tavi.data.plotter import Plot2DManager
from tavi.data.spice_to_nexus import convert_spice_to_nexus
from tavi.data.tavi import TAVI
from tavi.plotter import Plot2DManager

spice_folder = "./test_data/exp1031/"
nexus_file_name = "./test_data/nexus_exp1031.h5"
Expand Down
2 changes: 1 addition & 1 deletion scripts/MnTe_contour.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
import matplotlib.pylab as plt

from tavi.data.plotter import Plot2DManager
from tavi.data.spice_to_nexus import convert_spice_to_nexus
from tavi.data.tavi import TAVI
from tavi.instrument.resolution.cooper_nathans import CN
from tavi.plotter import Plot2DManager
from tavi.sample.xtal import Xtal

spice_folder = "./test_data/exp813"
Expand Down
2 changes: 1 addition & 1 deletion scripts/test_fit_group.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
import matplotlib.pyplot as plt

from tavi.data.plotter import Plot1DManager, Plot2DManager
from tavi.data.tavi import TAVI
from tavi.plotter import Plot1DManager, Plot2DManager


def test_fit_group(tavi):
Expand Down
2 changes: 1 addition & 1 deletion scripts/test_plotter.py
Original file line number Diff line number Diff line change
@@ -1,8 +1,8 @@
import matplotlib.pylab as plt

from tavi.data.plotter import Plot1DManager, Plot2DManager
from tavi.data.tavi import TAVI
from tavi.instrument.resolution.cooper_nathans import CN
from tavi.plotter import Plot1DManager, Plot2DManager

# from tavi.instrument.instrument_params.python_dicts.cg4c import cg4c_config_params
# from tests.test_data_folder.test_samples.python_samples.nitio3 import nitio3
Expand Down
2 changes: 1 addition & 1 deletion scripts/test_rez.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,8 @@
# from tests.test_data_folder.test_samples.python_samples.sample_test import test_xtal
import matplotlib.pylab as plt

from tavi.data.plotter import Plot1DManager, Plot2DManager
from tavi.instrument.resolution.cooper_nathans import CN
from tavi.plotter import Plot1DManager, Plot2DManager


def test_l_vs_en(tas):
Expand Down
53 changes: 53 additions & 0 deletions src/tavi/data/plotter.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,53 @@
# import matplotlib.colors as colors
from typing import Optional

import numpy as np


class Plot1D(object):

def __init__(
self,
x: np.ndarray,
y: np.ndarray,
xerr: Optional[np.ndarray] = None,
yerr: Optional[np.ndarray] = None,
) -> None:
# self.ax = None
self.x = x
self.y = y
self.xerr = xerr
self.yerr = yerr

self.title: str = ""
self.xlim: Optional[tuple[float, float]] = None
self.ylim: Optional[tuple[float, float]] = None
self.xlabel: Optional[str] = None
self.ylabel: Optional[str] = None
self.label: Optional[str] = None

self.color = "C0"
self.fmt = "o"
self.LOG_X = False
self.LOG_Y = False

def set_labels(self, ax):
"""Set labels, limits and legneds"""

if self.xlim is not None:
ax.set_xlim(left=self.xlim[0], right=self.xlim[1])
if self.ylim is not None:
ax.set_ylim(bottom=self.ylim[0], top=self.ylim[1])

ax.set_title(self.title)
ax.set_xlabel(self.xlabel)
ax.set_ylabel(self.ylabel)
ax.grid(alpha=0.6)
ax.legend()

def plot_curve(self, ax):
if self.yerr is None:
ax.plot(self.x, self.y, label=self.label)
else:
ax.errorbar(x=self.x, y=self.y, yerr=self.yerr, fmt=self.fmt, label=self.label)
self.set_labels(ax)
File renamed without changes.
73 changes: 26 additions & 47 deletions src/tavi/data/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
import matplotlib.pyplot as plt
import numpy as np

from tavi.data.plotter import Plot1D
from tavi.data.scan_reader import (
InstrumentInfo,
SampleUBInfo,
Expand Down Expand Up @@ -95,32 +96,33 @@ def get_data_entry(self, entry_name):
"""Return data entry based on entry_name"""
return self.data[entry_name]

def _set_labels(
def _make_labels(
self,
plot1d: Plot1D,
x_str: str,
y_str: str,
norm_channel: Literal["time", "monitor", "mcu", None],
norm_val: float,
):
) -> Plot1D:
"""generate labels and title"""
if norm_channel is not None:
if norm_channel == "time":
norm_channel_str = "seconds"
else:
norm_channel_str = norm_channel
if norm_val == 1:
ylabel = y_str + "/ " + norm_channel_str
plot1d.ylabel = y_str + "/ " + norm_channel_str
else:
ylabel = y_str + f" / {norm_val} " + norm_channel_str
plot1d.ylabel = y_str + f" / {norm_val} " + norm_channel_str
else:
preset_val = self.scan_info.preset_value
ylabel = y_str + f" / {preset_val} " + self.scan_info.preset_channel
plot1d.ylabel = y_str + f" / {preset_val} " + self.scan_info.preset_channel

xlabel = x_str
label = "scan " + str(self.scan_info.scan_num)
title = label + ": " + self.scan_info.scan_title
plot1d.xlabel = x_str
plot1d.label = "scan " + str(self.scan_info.scan_num)
plot1d.title = plot1d.label + ": " + self.scan_info.scan_title

return (xlabel, ylabel, title, label)
return plot1d

def _rebin_tol(
self,
Expand All @@ -131,11 +133,7 @@ def _rebin_tol(
norm_channel: Literal["time", "monitor", "mcu", None],
norm_val: float,
):
x_grid = np.arange(
np.min(x_raw) + rebin_step / 2,
np.max(x_raw) + rebin_step / 2,
rebin_step,
)
x_grid = np.arange(np.min(x_raw) + rebin_step / 2, np.max(x_raw) + rebin_step / 2, rebin_step)
x = np.zeros_like(x_grid)
y = np.zeros_like(x_grid)
counts = np.zeros_like(x_grid)
Expand Down Expand Up @@ -225,19 +223,9 @@ def generate_curve(
norm_val: float = 1.0,
rebin_type: Literal["tol", "grid", None] = None,
rebin_step: float = 0.0,
):
) -> Plot1D:
"""Generate a curve from a single scan to plot,
with the options to
normalize the y-axis and rebin x-axis.
Returns:
x:
y:
yerr: if "detector"
xlabel:
ylabel:
title: "scan number: scan title"
label: run number as legend if overplotting multiple curves
with the options to normalize the y-axis and rebin x-axis.
"""

if x_str is None:
Expand All @@ -264,24 +252,27 @@ def generate_curve(
if yerr is not None:
yerr = yerr / norm

(xlabel, ylabel, title, label) = self._set_labels(x_str, y_str, norm_channel, norm_val)
plot1d = Plot1D(x=x, y=y, yerr=yerr)
plot1d = self._make_labels(plot1d, x_str, y_str, norm_channel, norm_val)

return (x, y, yerr, xlabel, ylabel, title, label)
return plot1d

if not rebin_step > 0:
raise ValueError("Rebin step needs to be greater than zero.")

match rebin_type:
case "tol": # x weighted by normalization channel
x, y, yerr = self._rebin_tol(x_raw, y_raw, y_str, rebin_step, norm_channel, norm_val)
plot1d = Plot1D(x=x, y=y, yerr=yerr)
plot1d = self._make_labels(plot1d, x_str, y_str, norm_channel, norm_val)
return plot1d
case "grid":
x, y, yerr = self._rebin_grid(x_raw, y_raw, y_str, rebin_step, norm_channel, norm_val)
plot1d = Plot1D(x=x, y=y, yerr=yerr)
plot1d = self._make_labels(plot1d, x_str, y_str, norm_channel, norm_val)
return plot1d
case _:
print('Unrecogonized rebin type. Needs to be "tol" or "grid".')

(xlabel, ylabel, title, label) = self._set_labels(x_str, y_str, norm_channel, norm_val)

return (x, y, yerr, xlabel, ylabel, title, label)
raise ValueError('Unrecogonized rebin type. Needs to be "tol" or "grid".')

def plot_curve(
self,
Expand All @@ -294,20 +285,8 @@ def plot_curve(
):
"""Plot a 1D curve gnerated from a singal scan in a new window"""

x, y, yerr, xlabel, ylabel, title, _ = self.generate_curve(
x_str,
y_str,
norm_channel,
norm_val,
rebin_type,
rebin_step,
)
plot1d = self.generate_curve(x_str, y_str, norm_channel, norm_val, rebin_type, rebin_step)

fig, ax = plt.subplots()
ax.errorbar(x, y, yerr=yerr, fmt="o")
ax.set_title(title)
ax.set_xlabel(xlabel)
ax.set_ylabel(ylabel)
ax.grid(alpha=0.6)

plot1d.plot_curve(ax)
fig.show()
4 changes: 2 additions & 2 deletions src/tavi/instrument/resolution/ellipse_curve.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,8 +25,8 @@ class ResoCurve(object):
generate_plot
"""

def __init__(self):
self.center: float = None
def __init__(self) -> None:
self.center: float = 0.0
self.fwhm = None
self.r0 = None
self.x = None
Expand Down
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0001.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0002.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0003.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0004.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0005.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0006.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0007.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0008.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0009.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0010.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0011.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0012.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0013.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0014.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0015.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0016.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0017.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0018.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0019.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0020.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0021.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0022.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0023.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0024.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0025.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0026.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0027.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0028.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0029.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0030.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0031.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0032.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0033.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0034.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0035.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0036.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0037.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0038.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0039.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0040.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0041.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0042.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0043.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0044.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0045.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0046.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0047.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0048.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0049.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0050.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0051.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0052.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0053.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0054.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0055.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0056.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0057.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0058.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0059.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0060.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0061.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0062.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0063.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0064.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0065.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0066.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0067.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0068.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0069.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0070.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0071.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0072.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0073.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0074.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0075.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0076.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0077.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0078.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0079.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0080.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0081.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0082.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0083.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0084.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0085.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0086.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0087.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0088.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0089.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0090.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0091.h5
Binary file not shown.
Binary file modified test_data/IPTS32124_CG4C_exp0424/scan0092.h5
Binary file not shown.
Binary file modified test_data/IPTS9865_HB1_exp0815/scan0001.h5
Binary file not shown.
Binary file modified test_data/IPTS9865_HB1_exp0815/scan0002.h5
Binary file not shown.
Binary file modified test_data/IPTS9865_HB1_exp0815/scan0003.h5
Binary file not shown.
Binary file modified test_data/IPTS9865_HB1_exp0815/scan0004.h5
Binary file not shown.
Binary file modified test_data/IPTS9865_HB1_exp0815/scan0005.h5
Binary file not shown.
Binary file modified test_data/IPTS9865_HB1_exp0815/scan0006.h5
Binary file not shown.
Binary file modified test_data/tavi_test_exp424.h5
Binary file not shown.
24 changes: 7 additions & 17 deletions tests/test_fit.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,34 +3,24 @@
import numpy as np

from tavi.data.fit import Fit
from tavi.data.plotter import Plot1DManager
from tavi.data.scan import Scan
from tavi.plotter import Plot1DManager


def test_fit_single_peak():

nexus_file_name = "./test_data/IPTS32124_CG4C_exp0424/scan0042.h5"
_, s1 = Scan.from_nexus_file(nexus_file_name)

x, y, yerr, xlabel, ylabel, title, label = s1.generate_curve(norm_channel="mcu", norm_val=30)
f1 = Fit(x=x, y=y, err=yerr, fit_range=(0.5, 4.0))
plot1d = s1.generate_curve(norm_channel="mcu", norm_val=30)
f1 = Fit(x=plot1d.x, y=plot1d.y, err=plot1d.yerr, fit_range=(0.5, 4.0))
f1.add_background(values=(0.7,))
f1.add_signal(values=(None, 3.5, None), vary=(True, True, True))
fit_report = f1.perform_fit()
assert np.allclose(f1.result.params["s1_center"].value, 3.54, atol=0.01)
assert np.allclose(f1.result.params["s1_fwhm"].value, 0.39, atol=0.01)

p1 = Plot1DManager()
p1.plot_curve(x, y, yerr, label=label)
p1.plot_curve(
f1.x_plot,
f1.y_plot,
fmt="-",
xlabel=xlabel,
ylabel=ylabel,
title=title,
label="fit",
)
# assert np.allclose(f1.result.params["s1_center"].value, 3.54, atol=0.01)
# assert np.allclose(f1.result.params["s1_fwhm"].value, 0.39, atol=0.01)
fig, ax = plt.subplots()
plot1d.plot_curve(ax)
plt.show()


Expand Down
Loading

0 comments on commit 2ac2acb

Please sign in to comment.