Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions src/labcore/analysis/__init__.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from .analysis_base import DatasetAnalysis as DatasetAnalysis
from .fit import Fit as Fit
from .fit import FitResult as FitResult
from .fit import fit_and_add_to_ds
from .hvplotting import ComplexHist as ComplexHist
from .hvplotting import Node as Node
from .hvplotting import ValuePlot as ValuePlot
Expand Down
39 changes: 39 additions & 0 deletions src/labcore/analysis/analysis_base.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from pathlib import Path
from types import TracebackType
from typing import Any, Dict, List, Optional, Type, Union
from shutil import copy2

# Needed to generate hvplot from a script
import holoviews as hv
Expand All @@ -21,6 +22,7 @@


class AnalysisExistsError(Exception):
logger.info('Analysis already exists, adding another')
pass


Expand Down Expand Up @@ -161,6 +163,43 @@ def add_figure(

make_figure = add_figure

def archive_file(self, file_path, name: Optional[str] = None, folder: Optional[Path] = None):
"""Copy a source file into the analysis output folder(s).

Parameters
----------
file_path
Path to the file that should be archived.
name
Optional base name for the archived copy. Defaults to the source stem.
folder
Optional destination folder. Defaults to every folder managed by this analysis.

Returns
-------
list[Path]
Paths of the archived copies.
"""
source = Path(file_path)
if not source.exists():
raise FileNotFoundError(f"source file does not exist: {source}")

archive_name = source.stem if name is None else name
suffix = source.suffix.lstrip(".")
target_folders = self.savefolders if folder is None else [Path(folder)]

archived_paths = []
for target_folder in target_folders:
if not target_folder.exists():
target_folder.mkdir(parents=True, exist_ok=True)

archived_path = self._new_file_path(target_folder, archive_name, suffix)
copy2(source, archived_path)
archived_paths.append(archived_path)

self.files.extend(path for path in archived_paths if path not in self.files)
return archived_paths

def to_table(self, name: str, data: Dict[str, Any]) -> None:
data.update(
{
Expand Down
37 changes: 37 additions & 0 deletions src/labcore/analysis/fitfuncs/generic.py
Original file line number Diff line number Diff line change
Expand Up @@ -144,6 +144,43 @@ def guess(

return dict(A=A, of=of, phi=phi, f=f, tau=tau)

class ExponentialDecayCosinetoground(Fit):
@staticmethod
def model(coordinates, A, of, f, phi, tau) -> np.ndarray:
"""$A \sin(2*\pi*(f*x + \phi/360)) \exp(-x/\tau) + of$"""
return A * (1+np.sin(2 * np.pi * (f * coordinates + phi/360))) * np.exp(-coordinates/tau) + of

@staticmethod
def guess(coordinates, data):
"""This guess will ignore the first value because since it usually is not relaiable."""

# offset guess: The mean of the data
of = np.mean(data)

# amplitude guess: difference between max and min.
A = np.abs(np.max(data) - np.min(data)) / 2.
if data[0] < data[-1]:
A *= -1

# f guess: Maximum of the absolute value of the fourier transform.
fft_data = np.fft.rfft(data)[1:]
fft_coordinates = np.fft.rfftfreq(data.size, coordinates[1] - coordinates[0])[1:]

# note to confirm, could there be multiple peaks? I am always taking the first one here.
f_max_index = np.argmax(fft_data)
f = fft_coordinates[f_max_index]

# phi guess
phi = -np.angle(fft_data[f_max_index], deg=True)

# tau guess: pick the point where we reach roughly 1/e
one_over_e_val = of + A/3.
one_over_e_idx = np.argmin(np.abs(data-one_over_e_val))
tau = coordinates[one_over_e_idx]

return dict(A=A, of=of, phi=phi, f=f, tau=tau)



class Gaussian(Fit):
@staticmethod
Expand Down
17 changes: 17 additions & 0 deletions src/labcore/data/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
from .datadict import DataDict, MeshgridDataDict, dd2xr
from .tools import split_complex
from .datadict_storage import datadict_from_hdf5, datadict_to_hdf5, all_datadicts_from_hdf5, DDH5Writer, load_as_xr
from .ddh5_xr import (
ddh5_to_xarray,
ddh5_to_gridded_ddh5,
ddh5_schema,
ddh5_info,
validate_ddh5,
MissingMode,
DDH5Schema,
DDH5FieldInfo,
GridInfo,
DDH5ValidationReport,
GridInferenceError,
DDH5Writer_swmr,
)
Loading