Skip to content

Commit

Permalink
Work towards rebinning methods
Browse files Browse the repository at this point in the history
  • Loading branch information
lucas-wilkins committed Oct 10, 2024
1 parent 5b35614 commit 090808e
Show file tree
Hide file tree
Showing 2 changed files with 77 additions and 10 deletions.
21 changes: 12 additions & 9 deletions sasdata/data.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,29 +2,32 @@
from typing import TypeVar, Any, Self
from dataclasses import dataclass

import numpy as np

from quantities.quantity import NamedQuantity
from sasdata.metadata import Metadata
from sasdata.quantities.accessors import AccessorTarget
from sasdata.data_backing import Group, key_tree


class SasData:
def __init__(self, name: str, data_contents: list[NamedQuantity], raw_metadata: Group, verbose: bool=False):
def __init__(self, name: str,
data_contents: list[NamedQuantity],
raw_metadata: Group,
verbose: bool=False):

self.name = name
self._data_contents = data_contents
self._raw_metadata = raw_metadata
self._verbose = verbose

self.metadata = Metadata(AccessorTarget(raw_metadata, verbose=verbose))

# TO IMPLEMENT

# abscissae: list[NamedQuantity[np.ndarray]]
# ordinate: NamedQuantity[np.ndarray]
# other: list[NamedQuantity[np.ndarray]]
#
# metadata: Metadata
# model_requirements: ModellingRequirements
# Components that need to be organised after creation
self.ordinate: NamedQuantity[np.ndarray] = None # TODO: fill out
self.abscissae: list[NamedQuantity[np.ndarray]] = None # TODO: fill out
self.mask = None # TODO: fill out
self.model_requirements = None # TODO: fill out

def summary(self, indent = " ", include_raw=False):
s = f"{self.name}\n"
Expand Down
66 changes: 65 additions & 1 deletion sasdata/transforms/rebinning.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,73 @@
""" Algorithms for interpolation and rebinning """
from typing import TypeVar

import numpy as np
from numpy._typing import ArrayLike

from sasdata.quantities.quantity import Quantity
from scipy.sparse import coo_matrix

from enum import Enum

class InterpolationOptions(Enum):
NEAREST_NEIGHBOUR = 0
LINEAR = 1



def calculate_interpolation_matrix_1d(input_axis: Quantity[ArrayLike],
output_axis: Quantity[ArrayLike],
mask: ArrayLike | None = None,
order: InterpolationOptions = InterpolationOptions.NEAREST_NEIGHBOUR,
is_density=False):

# We want the input values in terms of the output units, will implicitly check compatability

working_units = output_axis.units

input_x = input_axis.in_units_of(working_units)
output_x = output_axis.in_units_of(working_units)

# Get the array indices that will map the array to a sorted one
input_sort = np.argsort(input_x)
output_sort = np.argsort(output_x)

output_unsort = np.arange(len(input_x), dtype=int)[output_sort]
sorted_in = input_x[input_sort]
sorted_out = output_x[output_sort]

match order:
case InterpolationOptions.NEAREST_NEIGHBOUR:

# COO Sparse matrix definition data
values = []
j_entries = []
i_entries = []

# Find the output values nearest to each of the input values
for x_in in sorted_in:


case _:
raise ValueError(f"Unsupported interpolation order: {order}")

def calculate_interpolation_matrix(input_axes: list[Quantity[ArrayLike]],
output_axes: list[Quantity[ArrayLike]],
data: ArrayLike | None = None,
mask: ArrayLike | None = None):

pass



def rebin(data: Quantity[ArrayLike],
axes: list[Quantity[ArrayLike]],
new_axes: list[Quantity[ArrayLike]],
mask: ArrayLike | None = None,
interpolation_order: int = 1):

""" This algorithm is only for operations that preserve dimensionality,
i.e. non-projective rebinning.
"""

def rebin(data: Quantity[ArrayLike], axes: list[Quantity[ArrayLike]], new_axes: list[Quantity[ArrayLike]], interpolation_order=1):
pass

0 comments on commit 090808e

Please sign in to comment.