Skip to content

Commit

Permalink
implement method and classes for post-proc
Browse files Browse the repository at this point in the history
Changes to be committed:
	modified:   src/pytom3d/scan.py
	modified:   src/pytom3d/stats.py
	modified:   src/pytom3d/util.py
	modified:   src/pytom3d/viewer.py -> add canvas contours and plots
  • Loading branch information
aletgn committed May 1, 2024
1 parent cd28ee2 commit 18c3fbd
Show file tree
Hide file tree
Showing 4 changed files with 348 additions and 25 deletions.
29 changes: 26 additions & 3 deletions src/pytom3d/scan.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,30 @@ def __init__(self, **kwargs: Dict['str', Any]) -> None:
except KeyError:
self.name = "Untitled"

try:
self.color = kwargs.pop("color")
except KeyError:
self.color = "k"

try:
self.line = kwargs.pop("line")
except KeyError:
self.line = "-"

try:
self.alpha = kwargs.pop("alpha")
except KeyError:
self.alpha = 0.3

try:
self.err_bar = kwargs.pop("err_bar")
except KeyError:
self.err_bar = False

def config_aspect(self, color, line, alpha):
self.color = color
self.line = line
self.alpha = alpha

def load_file(self, reader: callable, path: str, **kwargs: Dict['str', Any]) -> None:
"""
Expand All @@ -37,8 +61,7 @@ def load_file(self, reader: callable, path: str, **kwargs: Dict['str', Any]) ->
self.y = data.iloc[:, 1].to_numpy()
self.y_err = data.iloc[:, 2].to_numpy()


def load_data(self, x: np.ndarray, y: np.ndarray, y_err) -> None:
def load_data(self, x: np.ndarray, y: np.ndarray, y_err: np.ndarray = None) -> None:
"""
Load data directly.
Expand All @@ -53,7 +76,7 @@ def load_data(self, x: np.ndarray, y: np.ndarray, y_err) -> None:
"""
self.x = x
self.y = y
self.y_err = y
self.y_err = y_err


def export_line_scan(reader: callable, path: str, *scans: List, **kwargs: Dict['str', Any]) -> None:
Expand Down
8 changes: 4 additions & 4 deletions src/pytom3d/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
import numpy as np


def running_mean(inp: List[int], out: int, up_to_id = None, *list_path: List[str]) -> np.ndarray:
def running_mean(out: int, up_to_id = None, *list_path: List[str]) -> np.ndarray:
"""
Calculate the running mean of specified columns across multiple files.
Expand Down Expand Up @@ -40,7 +40,7 @@ def running_mean(inp: List[int], out: int, up_to_id = None, *list_path: List[str
return cumulative_sum/N


def running_std(inp: List[int], out: int, up_to_id = None, ddof: int = 1, *list_path: List[str]) -> np.ndarray:
def running_std(out: int, up_to_id = None, ddof: int = 1, *list_path: List[str]) -> np.ndarray:
"""
Calculate the running standard deviation of specified columns across multiple files.
Expand All @@ -56,7 +56,7 @@ def running_std(inp: List[int], out: int, up_to_id = None, ddof: int = 1, *list_
Index of the last file to include in the calculation. If None, all files are included. Default is None.
ddof : int, optional
Delta degrees of freedom. Default is 1.
Degrees of freedom. Default is 1 (unbiased variance estimator).
list_path : List[str]
Variable number of file paths containing the data.
Expand All @@ -74,7 +74,7 @@ def running_std(inp: List[int], out: int, up_to_id = None, ddof: int = 1, *list_
else:
max_id = up_to_id + 1

mean = running_mean(inp, out, max_id - 1, *list_path)
mean = running_mean(out, max_id - 1, *list_path)
for r in range(0, max_id):
data = np.load(list_path[r])
cumulative_sum += (data[:, out] - mean)**2
Expand Down
64 changes: 53 additions & 11 deletions src/pytom3d/util.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,7 @@
import re
from typing import Tuple, List, Any


def summation(x, y):
return x+y


def distance(x, y):
return (x**2+y**2)**0.5


def distance2(x, y):
return (abs(2*x)+y**2)**0.5
from pytom3d.stats import running_mean, running_std


def export_regressor(regressor, folder: str = "./", filename: str = "my_regressor", extension: str = ".rg",
Expand Down Expand Up @@ -463,3 +453,55 @@ def wrapper(self, *args, **kwargs) -> None:

self.history_.append(event)
return wrapper


def contour_data_wrapper(path: str, match: str) -> Tuple[np.ndarray]:
"""
Wrapper function for generating contour data.
Parameters
----------
path : str
Path to the directory containing data files.
match : str
A string used to match the desired data files.
Returns
-------
Tuple[np.ndarray]
A tuple containing the x-coordinates, y-coordinates, mean value, and standard deviation.
"""
data = recursive_search(path, match=match, pop_first=True, take_first=False)

mean = running_mean(3, None, *data)
std = running_std(3, None, 1, *data)
x, y = get_coordinates([0], *data), get_coordinates([1], *data)

return x.reshape(-1), y.reshape(-1), mean, std


def scan_data_wrapper(path: str, match: str) -> Tuple[np.ndarray]:
"""
Wrapper function for generating scan data.
Parameters
----------
path : str
Path to the directory containing data files.
match : str
A string used to match the desired data files.
Returns
-------
Tuple[np.ndarray]
A tuple containing the x-coordinates, mean value, and standard deviation.
"""
data = recursive_search(path, match=match, pop_first=True, take_first=False)

mean = running_mean(3, None, *data)
std = running_std(3, None, 1, *data)
x = get_coordinates([0], *data)

return x.reshape(-1), mean, std
Loading

0 comments on commit 18c3fbd

Please sign in to comment.