faninsar.TripletLoops#
- class faninsar.TripletLoops(loops: Sequence[Sequence[datetime, datetime, datetime]] | Sequence[TripletLoop], sort: bool = True)[source]#
Bases:
objectTripletLoops class to handle loops with three pairs/acquisitions.
- __init__(loops: Sequence[Sequence[datetime, datetime, datetime]] | Sequence[TripletLoop], sort: bool = True) None[source]#
Initialize the triplet loops class.
- Parameters:
loops (Sequence) – Sequence object of triplet loops. Each loop is an Sequence object of three dates with format of datetime or TripletLoop object. For example, [(date1, date2, date3), …].
sort (bool, optional) – Whether to sort the loops. Default is True.
Methods
__init__(loops[, sort])Initialize the triplet loops class.
from_names(names[, parse_function, date_args])Initialize the loops class from a list of loop file names.
sort([order, ascending, inplace])Sort the loops.
to_frame([target])Return the loops as a DataFrame.
Return loop matrix (containing 1, -1, 0) from pairs.
to_names([prefix])Return the string name of each loop.
Return the season of each loop.
where(loop[, return_type])Return the index of the loop.
Attributes
Sorted dates of the loops with format of datetime.
The time span of the first pair in days.
The time span of the third pair in days.
The time span of the second pair in days.
The index of the loops in dates coordinates.
The names (sting format) of the loops.
All sorted pairs of the loops.
The first pairs of the loops.
The third pairs of the loops.
The second pairs of the loops.
The shape of the loop array.
Return the values of the loops.
- classmethod from_names(names: list[str], parse_function: Callable | None = None, date_args: dict | None = None) TripletLoops[source]#
Initialize the loops class from a list of loop file names.
- Parameters:
names (list) – list of loop file names.
parse_function (Callable, optional) – Function to parse the date strings from the loop file name. If None, the loop file name will be split by ‘_’ and the last 3 items will be used. Default is None.
date_args (dict, optional) – Keyword arguments for pd.to_datetime() to convert the date strings to datetime objects. For example, {‘format’: ‘%Y%m%d’}. Default is {}.
- Returns:
loops – unsorted TripletLoops object.
- Return type:
- sort(order: str | list = 'pairs', ascending: bool = True, inplace: bool = True) tuple[TripletLoops, NDArray[np.int64]] | None[source]#
Sort the loops.
- Parameters:
order (str or list of str, optional) –
By which fields to sort the loops. this argument specifies which fields to compare first, second, etc. Default is ‘pairs’.
The available options are one or a list of:
date:: ‘date1’, ‘date2’, ‘date3’
pairs: ‘pairs12’, ‘pairs23’, ‘pairs13’
days: ‘days12’, ‘days23’, ‘days13’
- short name: ‘date’, ‘pairs’, ‘days’. short name will be
treated as a combination of the above options. For example, ‘date’ is equivalent to [‘date1’, ‘date2’, ‘date3’].
ascending (bool, optional) – Whether to sort ascending. Default is True.
inplace (bool, optional) – Whether to sort the loops inplace. Default is True.
- Returns:
sorted – if inplace is True, return the sorted loops and the index of the sorted loops in the original loops. Otherwise, return None.
- Return type:
(TripletLoops, np.ndarray) | None
- to_frame(target: Literal['pairs', 'dates'] = 'pairs') DataFrame[source]#
Return the loops as a DataFrame.
- Parameters:
target (str, one of ['pairs', 'dates']) – Target of the DataFrame. Default is ‘pairs’.
- to_matrix() NDArray[np.int8][source]#
Return loop matrix (containing 1, -1, 0) from pairs.
- Returns:
matrix – TripletLoop matrix with the shape of (n_loop, n_pair). The values of each loop/row in matrix are:
1: pair12 and pair23
-1: pair13
0: otherwise
- Return type:
np.ndarray
- to_names(prefix: str | None = None) NDArray[np.str_][source]#
Return the string name of each loop.
- Parameters:
prefix (str, optional) – Prefix of the output loop names. Default is None.
- Returns:
names – String names of the loops.
- Return type:
np.ndarray
- to_seasons() NDArray[np.int8][source]#
Return the season of each loop.
- Returns:
seasons –
- list of seasons of each loop.
0: not the same season 1: spring 2: summer 3: fall 4: winter
- Return type:
- where(loop: str | TripletLoop, return_type: Literal['index', 'mask'] = 'index') int | None[source]#
Return the index of the loop.
- property dates: NDArray[np.datetime64]#
Sorted dates of the loops with format of datetime.
- property days12: NDArray[np.int64]#
The time span of the first pair in days.
- property days13: NDArray[np.int64]#
The time span of the third pair in days.
- property days23: NDArray[np.int64]#
The time span of the second pair in days.
- property index: NDArray[np.int64]#
The index of the loops in dates coordinates.
- property names: NDArray[np.str_]#
The names (sting format) of the loops.
- property values: NDArray[np.datetime64]#
Return the values of the loops.
- Returns:
values – Values of the loops with format of datetime.
- Return type:
np.ndarray