litmus.litmusclass
litmus.py
Contains the main litmus object class, which acts as a user-friendly interface with the models statistical models and fitting procedure. In future versions, this will also give access to the GUI.
todo - This entire class to be re-done to take multiple models instead of multiple lightcurves - Possibly add hdf5 saving to chain output - Maybe add save_litmus() /w pickling? - Need to have better handling of the "fitting method inherit" feature, especially with refactor / redo
LITMUS(fitproc: fitting_procedure = None)
Bases: logger
A front-facing UI class for interfacing with the fitting procedures.
Source code in litmus/litmusclass.py
model = fitproc.stat_model
instance-attribute
fitproc = fitproc
instance-attribute
lightcurves = []
instance-attribute
data = None
instance-attribute
Nsamples = 50000
instance-attribute
samples = {}
instance-attribute
prior_samples = self.model.prior_sample(self.Nsamples)
instance-attribute
C = ChainConsumer()
instance-attribute
add_lightcurve(lc: lightcurve)
remove_lightcurve(i: int) -> None
Remove lightcurve of index 'i' from the LITMUS object
Source code in litmus/litmusclass.py
prefit(i=0, j=1)
Performs the full fit for the chosen stats model and fitting method.
Source code in litmus/litmusclass.py
fit(i=0, j=1) -> None
Performs the full fit for the chosen stats model and fitting method.
Source code in litmus/litmusclass.py
save_chain(path: str = None, headings: bool = True) -> None
Saves the litmus's output chains to a .csv file at "path" If headings=True (default) then the names of the parameters will be written to the first row of the tile
todo - this needs updating
Source code in litmus/litmusclass.py
read_chain(path: str, header: _types.Iterable[str] | None = None)
todo needs updating
Source code in litmus/litmusclass.py
config(**kwargs)
Quick and easy way to pass arguments to the chainconsumer object. Allows editing while prote
plot_lightcurves(model_no: int = 0, Nsamples: int = 1, Tspan: None | list[float, float] = None, Nplot: int = 1024, dir: str | None = None, show: bool = True) -> matplotlib.figure.Figure()
Plots the interpolated lightcurves for one of the fitted models
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_no
|
int
|
Which model to plot the lightcurves for |
0
|
Nsamples
|
int
|
Number of posterior samples to draw from when plotting |
1
|
Tspan
|
None | list[float, float]
|
Span of time values to plot over. If None, will use the max / min times of lc_1 and lc_2 |
None
|
Nplot
|
int
|
Number of points in the interpolated lightcurve |
1024
|
dir
|
str | None
|
If not None, will save to this filepath |
None
|
show
|
bool
|
If True, will plt.show() the plot |
True
|
Source code in litmus/litmusclass.py
plot_parameters(model_no: int | None = None, Nsamples: int = None, CC_kwargs: dict = {}, truth: dict = None, params: [str] = None, show: bool = True, prior_extents: bool = False, dir: str | None = None) -> matplotlib.figure.Figure
Creates a nicely formatted chainconsumer plot of the parameters
Parameters:
Name | Type | Description | Default |
---|---|---|---|
model_no
|
int | None
|
Which model to plot the lightcurves for. If None, will plot for all |
None
|
Nsamples
|
int
|
Number of posterior samples to draw from when plotting |
None
|
CC_kwargs
|
dict
|
Keyword arguments to pass to the chainconsumer constructor |
{}
|
truth
|
dict
|
Dictionary of parameter names to truth values |
None
|
params
|
[str]
|
List of parameters to plot |
None
|
show
|
bool
|
If True, will show the plot |
True
|
prior_extents
|
bool
|
If True, will use the model prior range for the axes limits (Defaults to false if multiple models used) |
False
|
dir
|
str | None
|
If not None, will save to this filepath |
None
|
Returns:
Type | Description |
---|---|
Figure
|
Returns the matplotlib figure |
Source code in litmus/litmusclass.py
lag_plot(Nsamples: int = None, truth: dict = None, show: bool = True, extras: bool = True, prior_extents=False, dir: str | None = None) -> matplotlib.figure.Figure
Creates a nicely formatted chainconsumer plot of the marginalized lag plot
Parameters:
Name | Type | Description | Default |
---|---|---|---|
Nsamples
|
int
|
Number of posterior samples to draw from when plotting |
None
|
truth
|
dict
|
Dictionary of parameter names to truth values :param show: If True, will show the plot |
None
|
extras
|
bool
|
If True, will add any fitting method specific extras to the plot |
True
|
dir
|
str | None
|
If not None, will save to this filepath |
None
|
prior_extents
|
If True, will use the model prior range for the axes limits (Defaults to false if multiple models used) Returns the matplotlib figure |
False
|
Source code in litmus/litmusclass.py
diagnostic_plots(dir: str | None = None, show: bool = False, **kwargs)
Generates a diagnostic plot window
Parameters:
Name | Type | Description | Default |
---|---|---|---|
dir
|
str | None
|
If not None, will save to this filepath |
None
|
show
|
bool
|
If True, will show the plot If dir!=None, will plt.savefig to the filepath 'dir' with **kwargs |
False
|
Source code in litmus/litmusclass.py
suppress_stdout()
Source code in litmus/_utils.py
isiter(x: any) -> bool
isiter_dict(DICT: dict) -> bool
like isiter but for a dictionary. Checks only the first element in DICT.keys
dict_dim(DICT: dict) -> (int, int)
Checks the first element of a dictionary and returns its length
Source code in litmus/_utils.py
dict_pack(DICT: dict, keys=None, recursive=True, H=None, d0={}) -> np.array
Packs a dictionary into an array format
Parameters:
Name | Type | Description | Default |
---|---|---|---|
DICT
|
dict
|
the dict to unpack |
required |
keys
|
the order in which to index the keyed elements. If none, will use DICT.keys(). Can be partial |
None
|
|
recursive
|
whether to recurse into arrays |
True
|
|
H
|
Matrix to scale parameters by |
None
|
|
d0
|
Value to offset by before packing |
{}
|
Returns:
Type | Description |
---|---|
array
|
(nkeys x len_array) np.arrayobject X = H (d-d0) |
Source code in litmus/_utils.py
dict_unpack(X: np.array, keys: [str], recursive=True, Hinv=None, x0=None) -> np.array
Unpacks an array into a dict
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
array
|
Array to unpack |
required |
keys
|
[str]
|
keys to unpack with |
required |
Returns:
Type | Description |
---|---|
array
|
Hinv(X) + x0 |
Source code in litmus/_utils.py
dict_sortby(A: dict, B: dict, match_only=True) -> dict
Sorts dict A to match keys of dict B.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
A
|
dict
|
Dict to be sorted |
required |
B
|
dict
|
Dict whose keys are will provide the ordering |
required |
match_only
|
If true, returns only for keys common to both A and B. Else, append un-sorted entries to end |
True
|
Returns:
Type | Description |
---|---|
dict
|
{key: A[key] for key in B if key in A} |
Source code in litmus/_utils.py
dict_extend(A: dict, B: dict = None) -> dict
Extends all single-length entries of a dict to match the length of a non-singular element
Parameters:
Name | Type | Description | Default |
---|---|---|---|
A
|
dict
|
Dictionary whose elements are to be extended |
required |
B
|
dict
|
(optional) the array to extend by, equivalent to dict_extend(A|B) |
None
|
Returns:
Type | Description |
---|---|
dict
|
Dict A with any singleton elements extended to the longest entry in A or B |
Source code in litmus/_utils.py
dict_combine(X: [dict]) -> {str: [float]}
Combines an array, list etc. of dictionaries into a dictionary of arrays
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
[dict]
|
1D Iterable of dicts |
required |
Returns:
Type | Description |
---|---|
{str: [float]}
|
Dict of 1D iterables |
Source code in litmus/_utils.py
dict_divide(X: dict) -> [dict]
dict_split(X: dict, keys: [str]) -> (dict, dict)
Splits a dict in two based on keys
Parameters:
Name | Type | Description | Default |
---|---|---|---|
X
|
dict
|
Dict to be split into A,B |
required |
keys
|
[str]
|
Keys to be present in A, but not in B |
required |
Returns:
Type | Description |
---|---|
(dict, dict)
|
tuple of dicts (A,B) |
Source code in litmus/_utils.py
pack_function(func, packed_keys: [str], fixed_values: dict = {}, invert: bool = False, jit: bool = False, H: np.array = None, d0: dict = {}) -> _types.FunctionType
Re-arranges a function that takes dict arguments to tak array-like arguments instead, so as to be autograd friendly Takes a function f(D:dict, arg, kwargs) and returns f(X, D2, args, **kwargs), D2 is all elements of D not listed in 'packed_keys' or fixed_values.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
func
|
Function to be unpacked |
required | |
packed_keys
|
[str]
|
Keys in 'D' to be packed in an array |
required |
fixed_values
|
dict
|
Elements of 'D' to be fixed |
{}
|
invert
|
bool
|
If true, will 'flip' the function upside down |
False
|
jit
|
bool
|
If true, will 'jit' the function |
False
|
H
|
array
|
(optional) scaling matrix to reparameterize H with |
None
|
d0
|
dict
|
(optional) If given, will center the reparameterized function at x0 |
{}
|