70 lines
2.2 KiB
Python
70 lines
2.2 KiB
Python
import numpy as np
|
|
from scipy.stats import skew, kurtosis
|
|
from typing import Dict, Any, Optional
|
|
|
|
def analyze_data(data: np.ndarray) -> Dict[str, Any]:
|
|
"""
|
|
Calculates a dictionary of summary statistics for the given NumPy array.
|
|
|
|
Args:
|
|
data: The input NumPy array (can be 1D or 2D).
|
|
|
|
Returns:
|
|
A dictionary containing various statistical metrics. Returns an
|
|
empty dictionary if the input data is empty.
|
|
"""
|
|
if data is None or data.size == 0:
|
|
return {}
|
|
|
|
analysis: Dict[str, Any] = {
|
|
"size": data.size,
|
|
"data_type": data.dtype,
|
|
"unique_values": len(np.unique(data)),
|
|
"maximum": np.max(data),
|
|
"minimum": np.min(data),
|
|
"mean": np.mean(data),
|
|
"standard_deviation": np.std(data),
|
|
"sum": np.sum(data),
|
|
"median": np.median(data),
|
|
"percentile_25": np.percentile(data, 25),
|
|
"percentile_75": np.percentile(data, 75),
|
|
"range": np.max(data) - np.min(data),
|
|
}
|
|
|
|
if data.ndim > 1:
|
|
analysis["shape"] = data.shape
|
|
|
|
# For skewness and kurtosis, the array must be flattened to 1D.
|
|
# These calculations are more meaningful on the flattened data distribution.
|
|
flat_data = data.flatten()
|
|
analysis["skewness"] = skew(flat_data)
|
|
analysis["kurtosis"] = kurtosis(flat_data)
|
|
|
|
return analysis
|
|
|
|
|
|
def inverse_fourier_transform(matrix: np.ndarray) -> Optional[np.ndarray]:
|
|
"""
|
|
Computes the inverse 2D Fourier Transform of a matrix.
|
|
|
|
This function calculates the inverse FFT and returns the absolute
|
|
values of the result, which is suitable for image visualization.
|
|
|
|
Args:
|
|
matrix: The input matrix in the frequency domain.
|
|
|
|
Returns:
|
|
The resulting real-valued image (as a NumPy array) after the
|
|
inverse transform, or None if an error occurs.
|
|
"""
|
|
try:
|
|
# Perform the inverse 2D Fast Fourier Transform
|
|
image = np.fft.ifft2(matrix)
|
|
|
|
# Return the magnitude (absolute value) of the complex result
|
|
# for visualization purposes.
|
|
return np.abs(image)
|
|
except Exception:
|
|
# The logger will be used here in the future.
|
|
# For now, return None to signal an error to the caller.
|
|
return None |