operations module

ariastrotools.operations.ari_operations(arr1, arr2, var_arr1=None, var_arr2=None, operation='+')[source]

Perform element-wise arithmetic operations on two input arrays with optional variance propagation.

Parameters:
  • arr1 (numpy.ndarray) – First input array.

  • arr2 (numpy.ndarray) – Second input array, must be broadcastable to the shape of arr1.

  • var_arr1 (numpy.ndarray or None, optional) – Variance (uncertainty) array corresponding to arr1. Default is None.

  • var_arr2 (numpy.ndarray or None, optional) – Variance (uncertainty) array corresponding to arr2. Default is None.

  • operation (str, optional) – Arithmetic operation to apply (default is ‘sum’). Supported values: - ‘+’ : element-wise addition - ‘-’ : element-wise subtraction (arr1 - arr2) - ‘*’ : element-wise multiplication - ‘/’ : element-wise division (arr1 / arr2)

Returns:

  • numpy.ndarray or tuple of numpy.ndarray – If var_arr1 and var_arr2 are not provided, returns the result of element-wise operation on arr1 and arr2. If both variances are provided, returns the propagated variance array computed according to the operation: - For ‘+’ and ‘-’: variances are added. - For ‘*’ and ‘/’: variance propagated as

  • product.

Raises:
  • ZeroDivisionError – When division by zero occurs in ‘div’ operation.

  • ValueError – If operation is not one of the supported strings.

Examples

>>> import numpy as np
>>> a = np.array([1.0, 2.0, 3.0])
>>> b = np.array([4.0, 5.0, 6.0])
>>> ari_operations(a, b, operation='+')
array([5., 7., 9.])
>>> var_a = np.array([0.1, 0.1, 0.1])
>>> var_b = np.array([0.2, 0.2, 0.2])
>>> ari_operations(a, b, var_a, var_b, operation='+')
array([0.3, 0.3, 0.3])
ariastrotools.operations.combine_bintable(dataarr, value_cols=None, uncertainity_cols=None, combine_cols=None, method='mean')[source]
ariastrotools.operations.combine_data(dataarr, var=None, method='mean', mask=None)[source]

Combine multiple arrays along the first axis using a specified method.

Parameters:
  • dataarr (array_like) – Input data array of shape (N, …), where N is the number of individual datasets to combine. The combination is performed along axis=0.

  • var (array_like, optional) – Variance array of the same shape as dataarr. If provided, error propagation is performed assuming independent errors, yielding the variance of the combined data. Default is None.

  • method ({'mean', 'median', 'biweight', 'weightedavg'}, optional) –

    Method used for combining the data:

    • ’mean’ : arithmetic mean ignoring NaNs.

    • ’median’ : median ignoring NaNs.

    • ’biweight’ : robust biweight location (from astropy.stats).

    Default is ‘mean’.

Returns:

  • comb_data (ndarray) – Combined data array, same shape as a single input array (i.e., shape of dataarr[0]).

  • comb_var (ndarray, optional) – Combined variance array of the same shape as comb_data. Returned only if var is provided.

Notes

  • NaN values in dataarr are ignored during combination.

  • Variance is propagated as if the combination method were the mean, even if median or biweight are chosen. This provides an approximate uncertainty estimate.

  • The biweight method is less sensitive to outliers than the mean or median.

ariastrotools.operations.combine_data_full(datadict, dataext=[1, 2, 3], varext=[4, 5, 6], extras=[], table_info=None, method='mean')[source]

Combine data from multiple FITS files into a single dictionary.

This function combines data arrays, variance arrays, additional numeric arrays, and binary tables stored in a dictionary (typically produced by reading multiple FITS files). Flux and variance arrays are combined using combine_data, while binary tables are combined using combine_bintable.

Parameters:
  • datadict (dict) – Dictionary containing data from multiple FITS files. Each key corresponds to a FITS extension or metadata item. Arrays to be combined are expected to be stacked along the first axis (i.e., shape (n_files, ...)).

  • dataext (list of int, optional) – Indices of datadict.keys() corresponding to data arrays (e.g., flux) that should be combined. Default is [1, 2, 3].

  • varext (list of int, optional) – Indices of datadict.keys() corresponding to variance arrays. Each entry must correspond to the matching entry in dataext. Default is [4, 5, 6].

  • extras (list of int, optional) – Indices of additional numeric arrays that should be combined using combine_data without associated variance arrays. Default is [].

  • table_info (dict, optional) –

    Dictionary describing binary table extensions to combine. Keys are indices into datadict.keys() and values are dictionaries passed to combine_bintable.

    Each value may contain the following entries:

    • value_cols : list of columns whose values are combined with propagated uncertainties.

    • uncertainty_cols : list of uncertainty columns corresponding to value_cols.

    • combine_cols : list of numeric columns that are combined without uncertainty propagation.

    Columns not listed above are assumed to be identical in all input tables and are copied from the first table after verifying they are unchanged.

    Default is None.

  • method ({'mean', 'median', 'biweight'}, optional) –

    Method used to combine the data.

    • 'mean' : arithmetic mean.

    • 'median' : median.

    • 'biweight' : biweight location.

Returns:

comb_dicts – Dictionary containing the combined data.

  • Data arrays in dataext are combined.

  • Variance arrays in varext are propagated.

  • Arrays in extras are combined.

  • Binary tables in table_info are combined using combine_bintable.

  • All remaining entries are copied from the first input file.

Return type:

dict

Notes

  • The order of dataext and varext must correspond.

  • Dictionary insertion order is assumed to match the FITS extension order.

  • Entries not listed in dataext, varext, extras, or table_info are copied from the first input file.

  • table_info provides a generic mechanism for combining arbitrary FITS binary tables without requiring instrument-specific code.

Examples

>>> table_info = {
...     7: {
...         "value_cols": ["VALUE"],
...         "uncertainty_cols": ["UNCERTAINTY"],
...         "combine_cols": []
...     }
... }
>>> combined = combine_data_full(
...     datadict,
...     dataext=[1],
...     varext=[2],
...     table_info=table_info,
...     method="mean",
... )
ariastrotools.operations.weighted_mean_and_variance(values, variances)[source]

Compute the weighted mean and variance of the mean, given measurements and their variances.

Parameters:
  • values (array-like) – Measured values (x_i)

  • variances (array-like) – Variances of the measurements.

Returns:

  • mean (float) – Weighted mean.

  • variance_of_mean (float) – Variance of the weighted mean

Raises:
  • ValueError – If variances is None.

  • TypeError – If values or variances are not array-like.

Notes

The weighted mean is computed as:

\[\bar{x} = \frac{\sum_i w_i x_i}{\sum_i w_i}, \quad w_i = \frac{1}{\sigma_i^2}\]

The variance of the weighted mean is:

\[\sigma_{\bar{x}}^2 = \frac{1}{\sum_i w_i}\]