Dynamic Factor Model
Module for Dynamic Factor ModelRunner
ModelRunner
¶
A class for running dynamic factor models on batches of data.
Parameters: - ad (AnnData): The AnnData object containing the data. - outdir (Path, optional): The output directory for saving the results. Defaults to "./output". - batch (str, optional): The batch column in the AnnData object. Defaults to None.
Attributes: - ad (AnnData): The AnnData object containing the data. - outdir (Path): The output directory for saving the results. - batch (str): The batch column in the AnnData object. - batches (dict[str, AnnData]): A dictionary of batches extracted from the AnnData object. - results (list): A list to store the results of each model run. - failures (dict): A dictionary to store any failures that occur during model runs.
Source code in dfmdash/dfm.py
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 |
|
get_batches()
¶
Get batches from AnnData object.
Returns: - dict[str, AnnData]: A dictionary of batches extracted from the AnnData object.
Source code in dfmdash/dfm.py
130 131 132 133 134 135 136 137 138 139 |
|
run(maxiter=10000, global_multiplier=1, columns=None)
¶
Run the dynamic factor models on the batches of data.
Parameters: - maxiter (int, optional): The maximum number of iterations for model fitting. Defaults to 10,000. - global_multiplier (int, optional): A global multiplier for the model. Defaults to 1. - columns (list[str], optional): The columns to include in the model. Defaults to None.
Returns: - ModelRunner: The ModelRunner object.
Raises: - Exception: If an error occurs during model fitting.
Source code in dfmdash/dfm.py
77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 |
|
write_failures()
¶
Write the failures to a file.
The failures are written to a file named "failed.txt" in the output directory. Each line in the file contains the batch name and the corresponding failure message.
Source code in dfmdash/dfm.py
119 120 121 122 123 124 125 126 127 128 |
|
Result
dataclass
¶
Represents the result of a dynamic factor model analysis.
Attributes:
Name | Type | Description |
---|---|---|
name |
Optional[str]
|
Name of the batch if batch is specified. |
result |
DynamicFactor
|
The dynamic factor model result. |
model |
DynamicFactorMQ
|
The dynamic factor model. |
factors |
DataFrame
|
The factors obtained from the analysis. |
Source code in dfmdash/dfm.py
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
write(outdir)
¶
Writes the model summary, result summary, and factors to CSV files.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
outdir
|
Path
|
The output directory where the files will be written. |
required |
Source code in dfmdash/dfm.py
33 34 35 36 37 38 39 40 41 42 43 44 45 |
|
process_factors(factors, raw, obs)
¶
Process factors by merging them with raw and obs dataframes.
Parameters:
Name | Type | Description | Default |
---|---|---|---|
factors
|
DataFrame
|
The factors dataframe. |
required |
raw
|
DataFrame
|
The raw dataframe. |
required |
obs
|
DataFrame
|
The obs dataframe. |
required |
Returns:
Type | Description |
---|---|
DataFrame
|
pd.DataFrame: The merged factors dataframe. |
Source code in dfmdash/dfm.py
142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 |
|