Skip to content

Commit

Permalink
resolving merging conflicts
Browse files Browse the repository at this point in the history
  • Loading branch information
Bing Li committed Oct 31, 2024
1 parent e05f56a commit 1664df0
Show file tree
Hide file tree
Showing 556 changed files with 63,924 additions and 105 deletions.
54 changes: 54 additions & 0 deletions scripts/MnTe_new.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,54 @@
import matplotlib.pyplot as plt

from tavi.data.plotter import Plot2D
from tavi.data.tavi import TAVI

tavi = TAVI()

# load two experiments from SPICE the first time
# tavi.load_spice_data_from_disk("./test_data/IPTS-34735/exp813/")
# tavi.load_spice_data_from_disk("./test_data/IPTS-34735/exp823/")
# tavi.save("./test_data/tavi_MnTe.h5")

tavi.open_file("./test_data/tavi_MnTe.h5")

# -------------- H0L const Q scans ------------
# scan_list1 = [37, 39, 40] + list(range(44, 64)) + list(range(65, 69)) + list(range(84, 97))
# sg1 = tavi.combine_scans(scan_list1, name="dispH")


# scan_data_2d = sg1.get_data(
# axes=("qh", "en", "detector"),
# norm_to=(1, "mcu"),
# grid=(0.025, 0.3),
# )

# p1 = Plot2D()
# p1.add_contour(scan_data_2d, cmap="turbo", vmax=1)
# p1.zlim = [0, 0.5]
# p1.ylim = [0, 50]
# fig, ax = plt.subplots()
# p1.plot(ax)


# -------------- H0L const Q scans ------------

scans = list(range(83, 167))

scan_list2 = [("IPTS34735_HB3_exp0823", scan) for scan in scans]
sg2 = tavi.combine_scans(scan_list2, name="dispH_2")
scan_data_2d = sg2.get_data(
axes=("qh", "en", "detector"),
norm_to=(1, "mcu"),
grid=(0.025, 0.5),
)

p2 = Plot2D()
p2.add_contour(scan_data_2d, cmap="turbo", vmax=1)
p2.zlim = [0, 0.01]
p2.ylim = [0, 50]
fig, ax = plt.subplots()
p2.plot(ax)


plt.show()
7 changes: 0 additions & 7 deletions scripts/tavi_loader.jl

This file was deleted.

214 changes: 158 additions & 56 deletions src/tavi/data/scan_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -13,15 +13,15 @@ def __init__(
self,
x: np.ndarray,
y: np.ndarray,
norm_col: Optional[np.ndarray] = None,
norm: Optional[np.ndarray] = None,
) -> None:

# ind = np.argsort(x)
# self.x = x[ind]
# self.y = y[ind]
self.x = x
self.y = y
self.norm_col = norm_col
self.norm = norm
self.err = np.sqrt(y)
# self._ind = ind
self.label = ""
Expand Down Expand Up @@ -72,62 +72,56 @@ def make_labels(self, axes: tuple[str, str], norm_to: tuple[float, str], label:
# scan_data_1d.err = np.sqrt(self.err**2 + other.err**2)
# return scan_data_1d

# TODO how to normalize?
# TODO rebin
def __sub__(self, other):
# check x length, rebin other
# check x length, rebin other if do not match
if len(self.x) != len(other.x):
pass
# rebin_intervals = np.diff(self.x)
# rebin_intervals = np.append(rebin_intervals, rebin_intervals[-1])
# rebin_boundary = self.x + rebin_intervals / 2

# y = np.zeros_like(rebin_boundary)
# counts = np.zeros_like(rebin_boundary)
# err = np.zeros_like(rebin_boundary)
# (x_min, x_max) = (self.x[0] - rebin_intervals[0] / 2, self.x[-1] + rebin_intervals[-1] / 2)

# for i, x0 in enumerate(other.x):
# if x0 > x_max or x0 < x_min:
# continue
# idx = np.nanargmax(rebin_boundary + ScanData1D.ZERO >= x0)
# y[idx] += other.y[i]
# err[idx] += other.err[i] ** 2
# counts[idx] += 1

# other.err = err / counts
# other.y = y / counts
rebin_intervals = np.diff(self.x)
rebin_intervals = np.append(rebin_intervals, rebin_intervals[-1])
rebin_boundary = self.x + rebin_intervals / 2

y = np.zeros_like(rebin_boundary)
counts = np.zeros_like(rebin_boundary)
err = np.zeros_like(rebin_boundary)
(x_min, x_max) = (self.x[0] - rebin_intervals[0] / 2, self.x[-1] + rebin_intervals[-1] / 2)

for i, x0 in enumerate(other.x):
if x0 > x_max or x0 < x_min:
continue
idx = np.nanargmax(rebin_boundary + ScanData1D.ZERO >= x0)
y[idx] += other.y[i]
err[idx] += other.err[i] ** 2
counts[idx] += 1

other.err = err / counts
other.y = y / counts

scan_data_1d = ScanData1D(self.x, self.y - other.y)
scan_data_1d.err = np.sqrt(self.err**2 + other.err**2)
return scan_data_1d

def renorm(self, norm: Optional[np.ndarray] = None, val: float = 1.0):
"""Renormalized to norm_val
def renorm(self, norm_col: Optional[np.ndarray] = None, norm_val: float = 1.0):
"""Renormalized to norm_val"""

Use norm if given, otherwise use self.norm
"""

if norm is not None:
norm_col = norm
elif self.norm_col is not None:
norm_col = self.norm_col
if norm_col is not None:
norm_col = norm_col
elif self.norm is not None:
norm_col = self.norm
else:
raise ValueError("Normalizaion columns cannot be None.")
self.y = self.y / norm_col * val
self.err = self.err / norm_col * val

def rebin_tol(self, rebin_params: tuple, weight_col: Optional[np.ndarray] = None):
"""Rebin with tolerance
self.y = self.y / norm_col * norm_val
self.err = self.err / norm_col * norm_val

Note:
This should be used only if all data are measured with the same weight/counting time"""
def rebin_tol(self, rebin_params: tuple, weight_col: Optional[np.ndarray] = None):
"""Rebin with tolerance"""

if weight_col is not None:
norm_col = weight_col
elif self.norm_col is not None:
norm_col = self.norm_col
elif self.norm is not None:
norm_col = self.norm
else:
raise ValueError("Normalizaion columns cannot be None.")
raise ValueError("Normalizaion collumns cannot be None.")

rebin_min, rebin_max, rebin_step = rebin_params
rebin_min = np.min(self.x) if rebin_min is None else rebin_min
Expand Down Expand Up @@ -160,8 +154,8 @@ def rebin_tol_renorm(self, rebin_params: tuple, norm_col: Optional[np.ndarray] =

if norm_col is not None:
norm_col = norm_col
elif self.norm_col is not None:
norm_col = self.norm_col
elif self.norm is not None:
norm_col = self.norm
else:
raise ValueError("Normalizaion columns cannot be None.")

Expand All @@ -175,19 +169,19 @@ def rebin_tol_renorm(self, rebin_params: tuple, norm_col: Optional[np.ndarray] =
x_boundary = np.linspace(rebin_min - rebin_step / 2, rebin_min + rebin_step * (num - 1 / 2), num + 1)
x = np.zeros_like(x_boundary[1:])
y = np.zeros_like(x)
counts = np.zeros_like(x)
weights = np.zeros_like(x)

for i, x0 in enumerate(self.x):
# Return the indices of the maximum values in the specified axis ignoring NaNs.
idx = np.nanargmax(x_boundary - ZERO > x0)
if idx > 0: # ignore first and last bin box
y[idx - 1] += self.y[i]
x[idx - 1] += self.x[i] * norm_col[i]
counts[idx - 1] += norm_col[i]
weights[idx - 1] += norm_col[i]

self.err = np.sqrt(y) / counts * norm_val
self.y = y / counts * norm_val
self.x = x / counts
self.err = np.sqrt(y) / weights * norm_val
self.y = y / weights * norm_val
self.x = x / weights

def rebin_grid(self, rebin_params: tuple):
"""Rebin with a regular grid"""
Expand All @@ -204,6 +198,7 @@ def rebin_grid(self, rebin_params: tuple):
counts = np.zeros_like(x)

for i, x0 in enumerate(self.x):

# Return the indices of the maximum values in the specified axis ignoring NaNs.
idx = np.nanargmax(x_boundary - ZERO > x0)
if idx > 0: # ignore first and last bin box
Expand All @@ -214,9 +209,16 @@ def rebin_grid(self, rebin_params: tuple):
self.err = np.sqrt(y) / counts
self.y = y / counts

def rebin_grid_renorm(self, rebin_params: tuple, norm_col: np.ndarray, norm_val: float = 1.0):
def rebin_grid_renorm(self, rebin_params: tuple, norm_col: Optional[np.ndarray] = None, norm_val: float = 1.0):
"""Rebin with a regular grid and renormalize"""

if norm_col is not None:
norm_col = norm_col
elif self.norm is not None:
norm_col = self.norm
else:
raise ValueError("Normalizaion columns cannot be None.")

rebin_min, rebin_max, rebin_step = rebin_params
rebin_min = np.min(self.x) if rebin_min is None else rebin_min
rebin_max = np.max(self.x) if rebin_max is None else rebin_max
Expand Down Expand Up @@ -247,10 +249,18 @@ class ScanData2D(object):

ZEROS = 1e-6

def __init__(self, x: np.ndarray, y: np.ndarray, z: np.ndarray) -> None:
def __init__(
self,
x: np.ndarray,
y: np.ndarray,
z: np.ndarray,
norm: Optional[np.ndarray] = None,
) -> None:
"""x, y, z must be 1D lists of the same length"""
self.x = x
self.y = y
self.z = z
self.norm = norm

self.err = np.sqrt(z)
self.title = ""
Expand Down Expand Up @@ -280,14 +290,106 @@ def make_labels(
self.label = label
self.title = title + self.zlabel

# TODO
def __sub__(self, other):
pass

def renorm(self, norm_col: np.ndarray, norm_val: float = 1.0):
pass
def renorm(self, norm_col: Optional[np.ndarray] = None, norm_val: float = 1.0):
if norm_col is not None:
norm_col = norm_col
elif self.norm is not None:
norm_col = self.norm
else:
raise ValueError("Normalizaion columns cannot be None.")

self.z = self.z / norm_col * norm_val
self.err = self.err / norm_col * norm_val

def rebin_grid(self, rebin_params: tuple):
pass
"""Rebin with a regular grid"""

def rebin_grid_renorm(self, rebin_params: tuple, norm_col: np.ndarray, norm_val: float = 1.0):
pass
rebin_x, rebin_y = rebin_params
x_min, x_max, x_step = rebin_x
x_min = np.min(self.x) if x_min is None else x_min
x_max = np.max(self.x) if x_max is None else x_max

ZERO_x = x_step / 100 # helps with the rounding error
x_num = math.floor((x_max + ZERO_x - x_min) / x_step) + 1
x_boundary = np.linspace(x_min - x_step / 2, x_min + x_step * (x_num - 1 / 2), x_num + 1)

y_min, y_max, y_step = rebin_y
y_min = np.min(self.y) if y_min is None else y_min
y_max = np.max(self.y) if y_max is None else y_max

ZERO_y = y_step / 100 # helps with the rounding error
y_num = math.floor((y_max + ZERO_y - y_min) / y_step) + 1
y_boundary = np.linspace(y_min - y_step / 2, y_min + y_step * (y_num - 1 / 2), y_num + 1)

x_list = np.linspace(x_min, x_min + x_step * (x_num - 1), x_num)
y_list = np.linspace(y_min, y_min + y_step * (y_num - 1), y_num)
xv, yv = np.meshgrid(x_list, y_list, indexing="ij")

z = np.zeros_like(xv)
counts = np.zeros_like(xv)

for i in range(len(self.x)):
x0 = self.x[i]
y0 = self.y[i]
# Return the indices of the maximum values in the specified axis ignoring NaNs.
idx = np.nanargmax(x_boundary - ZERO_x > x0)
idy = np.nanargmax(y_boundary - ZERO_y > y0)
if idx > 0 and idy > 0: # ignore first and last bin box
z[idx - 1, idy - 1] += self.z[i]
counts[idx - 1, idy - 1] += 1

self.x = xv
self.y = yv
self.z = z / counts
self.err = np.sqrt(z) / counts

def rebin_grid_renorm(self, rebin_params: tuple, norm_col: Optional[np.ndarray] = None, norm_val: float = 1.0):
if norm_col is not None:
norm_col = norm_col
elif self.norm is not None:
norm_col = self.norm
else:
raise ValueError("Normalizaion columns cannot be None.")

rebin_x, rebin_y = rebin_params
x_min, x_max, x_step = rebin_x
x_min = np.min(self.x) if x_min is None else x_min
x_max = np.max(self.x) if x_max is None else x_max

ZERO_x = x_step / 100 # helps with the rounding error
x_num = math.floor((x_max + ZERO_x - x_min) / x_step) + 1
x_boundary = np.linspace(x_min - x_step / 2, x_min + x_step * (x_num - 1 / 2), x_num + 1)

y_min, y_max, y_step = rebin_y
y_min = np.min(self.y) if y_min is None else y_min
y_max = np.max(self.y) if y_max is None else y_max

ZERO_y = y_step / 100 # helps with the rounding error
y_num = math.floor((y_max + ZERO_y - y_min) / y_step) + 1
y_boundary = np.linspace(y_min - y_step / 2, y_min + y_step * (y_num - 1 / 2), y_num + 1)

x_list = np.linspace(x_min, x_min + x_step * (x_num - 1), x_num)
y_list = np.linspace(y_min, y_min + y_step * (y_num - 1), y_num)
xv, yv = np.meshgrid(x_list, y_list, indexing="ij")

z = np.zeros_like(xv)
counts = np.zeros_like(xv)

for i in range(len(self.x)):
x0 = self.x[i]
y0 = self.y[i]
# Return the indices of the maximum values in the specified axis ignoring NaNs.
idx = np.nanargmax(x_boundary - ZERO_x > x0)
idy = np.nanargmax(y_boundary - ZERO_y > y0)
if idx > 0 and idy > 0: # ignore first and last bin box
z[idx - 1, idy - 1] += self.z[i]
counts[idx - 1, idy - 1] += norm_col[i]

self.x = xv
self.y = yv
self.z = z / counts * norm_val
self.err = np.sqrt(z) / counts * norm_val
Loading

0 comments on commit 1664df0

Please sign in to comment.