datfid 0.1.12__py3-none-any.whl → 0.1.14__py3-none-any.whl

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
datfid/client.py CHANGED
@@ -144,3 +144,105 @@ class DATFIDClient:
144
144
  except Exception as e:
145
145
  self.logger.error(f"Forecast generation failed: {str(e)}")
146
146
  raise
147
+
148
+ def fit_model_ind(
149
+ self,
150
+ df: pd.DataFrame,
151
+ id_col: str,
152
+ time_col: str,
153
+ y: str,
154
+ lag_y: Optional[Union[int, str, list[int]]] = None,
155
+ lagged_features: Optional[Dict[str, int]] = None,
156
+ current_features: Optional[list] = None,
157
+ filter_by_significance: bool = False,
158
+ meanvar_test: bool = False
159
+ ) -> SimpleNamespace:
160
+ """
161
+ Fit a model individual by individual using the DATFID API.
162
+
163
+ Args:
164
+ df: DataFrame containing the data
165
+ id_col: Name of the ID column
166
+ time_col: Name of the time column
167
+ y: Name of the target variable
168
+ lagged_features: Dictionary of features and their lag values
169
+ current_features: List of current features to use
170
+ filter_by_significance: Whether to filter features by significance
171
+ meanvar_test: Whether to perform mean-variance test
172
+
173
+ Returns:
174
+ SimpleNamespace containing the model fit results
175
+ """
176
+
177
+ df = df.copy()
178
+ for col in df.columns:
179
+ if pd.api.types.is_datetime64_any_dtype(df[col]):
180
+ df[col] = df[col].astype(str)
181
+
182
+ data = {
183
+ "df": df.to_dict(orient="records"),
184
+ "id_col": id_col,
185
+ "time_col": time_col,
186
+ "y": y,
187
+ "lag_y": lag_y,
188
+ "lagged_features": lagged_features or {},
189
+ "current_features": current_features or [],
190
+ "filter_by_significance": filter_by_significance,
191
+ "meanvar_test": meanvar_test
192
+ }
193
+
194
+ response = requests.post(
195
+ f"{self.api_url}modelfit_ind/",
196
+ json=data,
197
+ headers=self.headers
198
+ )
199
+
200
+ if response.status_code != 200:
201
+ raise Exception(f"Model fit failed: {response.text}")
202
+
203
+ result_dict = response.json()
204
+ return SimpleNamespace(**result_dict)
205
+
206
+ def forecast_model_ind(
207
+ self,
208
+ df_forecast: pd.DataFrame
209
+ ) -> pd.DataFrame:
210
+ """
211
+ Generate forecasts using the fitted individual by individual model.
212
+
213
+ Args:
214
+ df_forecast: DataFrame containing the forecast data
215
+
216
+ Returns:
217
+ DataFrame containing the forecast results
218
+ """
219
+
220
+ try:
221
+ df_forecast = df_forecast.copy()
222
+ for col in df_forecast.columns:
223
+ if pd.api.types.is_datetime64_any_dtype(df_forecast[col]):
224
+ df_forecast[col] = df_forecast[col].astype(str)
225
+
226
+ # Convert DataFrame to list of records
227
+ data = df_forecast.to_dict(orient="records")
228
+
229
+ response = requests.post(
230
+ f"{self.api_url}modelforecast_ind/",
231
+ json=data,
232
+ headers=self.headers
233
+ )
234
+
235
+ if response.status_code != 200:
236
+ raise Exception(f"Forecast generation failed: {response.text}")
237
+
238
+ result = pd.DataFrame(response.json())
239
+
240
+ # Clean up memory after operation
241
+ del df_forecast
242
+ del data
243
+ self._cleanup_memory()
244
+
245
+ return result
246
+ except Exception as e:
247
+ self.logger.error(f"Forecast generation failed: {str(e)}")
248
+ raise
@@ -0,0 +1,155 @@
1
+ Metadata-Version: 2.4
2
+ Name: datfid
3
+ Version: 0.1.14
4
+ Summary: SDK to access the DATFID API hosted on Hugging Face Spaces
5
+ Author: DATFID
6
+ Author-email: igor.schapiro@datfid.com
7
+ License: MIT
8
+ Classifier: License :: OSI Approved :: MIT License
9
+ Classifier: Programming Language :: Python :: 3
10
+ Requires-Python: >=3.7
11
+ Description-Content-Type: text/markdown
12
+ License-File: LICENSE
13
+ Requires-Dist: requests>=2.31.0
14
+ Requires-Dist: pandas>=1.0.1
15
+ Requires-Dist: numpy<2.1,>=1.22
16
+ Dynamic: author
17
+ Dynamic: author-email
18
+ Dynamic: classifier
19
+ Dynamic: description
20
+ Dynamic: description-content-type
21
+ Dynamic: license
22
+ Dynamic: license-file
23
+ Dynamic: requires-dist
24
+ Dynamic: requires-python
25
+ Dynamic: summary
26
+
27
+ # DATFID SDK
28
+
29
+ A Python SDK to access the DATFID API to forecast your data.
30
+
31
+ ## Features
32
+
33
+ - **Easy model fitting**: Build panel data models with time-dependent and static features.
34
+ - **Flexible lag handling**: Specify lags for the dependent variable and selected features.
35
+ - **Forecasting**: Generate future predictions with aligned timestamps and IDs.
36
+ - **Statistical options**: Filter features by significance and apply mean-variance tests.
37
+ - **White box full interpretability**: Get fully interpretable model with equation, estimated parameters, and standard errors.
38
+
39
+ ## Installation
40
+
41
+ ```bash
42
+ pip install datfid
43
+ ```
44
+
45
+ ## Usage
46
+
47
+ Before using the SDK, please request an access token by emailing **admin@datfid.com** or by visiting our website [datfid.com](https://datfid.com).
48
+
49
+ ```python
50
+ from datfid import DATFIDClient
51
+
52
+ # Initialize the client with your DATFID token
53
+ client = DATFIDClient(token="your_DATFID_token")
54
+
55
+ # Fit a model
56
+ fit_result = client.fit_model(
57
+ df=dataframe,
58
+ id_col="name of id column",
59
+ time_col="name of time column",
60
+ y="name of dependent variable",
61
+ lag_y="starting lag : ending lag",
62
+ lagged_features={
63
+ "feature 1": "starting lag : ending lag",
64
+ "feature 2": "starting lag : ending lag"
65
+ },
66
+ current_features=["feature 3", "feature 4"],
67
+ filter_by_significance=True/False,
68
+ meanvar_test=True/False
69
+ )
70
+
71
+ # Generate forecasts
72
+ forecast_df = client.forecast_model(
73
+ df_forecast=dataframe
74
+ )
75
+
76
+ # The forecast DataFrame contains the individual IDs and timestamps
77
+ # from the original data plus a "forecast" column with predicted values.
78
+ ```
79
+
80
+ ## Example 1
81
+
82
+ Sample dataset from GitHub (Food and Beverages demand forecasting):
83
+
84
+ ```python
85
+ import pandas as pd
86
+ from datfid import DATFIDClient
87
+
88
+ # Initialize the client with your DATFID token
89
+ client = DATFIDClient(token="your_DATFID_token")
90
+
91
+ # Load dataset for model fitting
92
+ url_fit = "https://raw.githubusercontent.com/datfid-valeriidashuk/sample-datasets/main/Food_Beverages.xlsx"
93
+ df = pd.read_excel(url_fit)
94
+
95
+ # Fit the model
96
+ result = client.fit_model(df=df,
97
+ id_col="Product",
98
+ time_col="Time",
99
+ y="Revenue",
100
+ current_features='all',
101
+ filter_by_significance=True
102
+ )
103
+
104
+ # Load dataset for forecasting
105
+ url_forecast = "https://raw.githubusercontent.com/datfid-valeriidashuk/sample-datasets/main/Food_Beverages_forecast.xlsx"
106
+ df_forecast = pd.read_excel(url_forecast)
107
+
108
+ # Forecast revenue using the fitted model
109
+ forecast = client.forecast_model(df_forecast=df_forecast)
110
+ ```
111
+
112
+ ## Example 2
113
+
114
+ Slightly larger sample dataset from GitHub (Banking sector, forecasting loan probability):
115
+
116
+ ```python
117
+ import pandas as pd
118
+ from datfid import DATFIDClient
119
+
120
+ # Initialize the client with your DATFID token
121
+ client = DATFIDClient(token="your_DATFID_token")
122
+
123
+ # Load dataset for model fitting
124
+ url_fit = "https://raw.githubusercontent.com/datfid-valeriidashuk/sample-datasets/main/Banking_extended.xlsx"
125
+ df = pd.read_excel(url_fit)
126
+
127
+ # Fit the model
128
+ result = client.fit_model(df=df,
129
+ id_col="Individual",
130
+ time_col="Time",
131
+ y="Loan Probability",
132
+ lag_y="1:3",
133
+ lagged_features={"Income Level": "1:3"},
134
+ filter_by_significance=True)
135
+
136
+ # Load dataset for forecasting
137
+ url_forecast = "https://raw.githubusercontent.com/datfid-valeriidashuk/sample-datasets/main/Banking_extended_forecast.xlsx"
138
+ df_forecast = pd.read_excel(url_forecast)
139
+
140
+ # Forecast loan probability using the fitted model
141
+ forecast = client.forecast_model(df_forecast=df_forecast)
142
+ ```
143
+
144
+ ## API Reference
145
+
146
+ ### DATFIDClient
147
+
148
+ #### `client = DATFIDClient(token: str)`
149
+ Initialize the client with your DATFID token.
150
+
151
+ #### `client.fit_model(df: pd.DataFrame, id_col: str, time_col: str, y: str, lag_y: Optional[Union[int, str, list[int]]] = None, lagged_features: Optional[Dict[str, int]] = None, current_features: Optional[list] = None, filter_by_significance: bool = False, meanvar_test: bool = False) -> SimpleNamespace`
152
+ Fit a model using the provided dataset.
153
+
154
+ #### `client.forecast_model(df_forecast: pd.DataFrame) -> pd.DataFrame`
155
+ Generate forecasts using the fitted model.
@@ -0,0 +1,7 @@
1
+ datfid/__init__.py,sha256=PojdUQsimZvawR8lgqVpzs3EkRtkA7iwuDk7fovJF54,34
2
+ datfid/client.py,sha256=U2fuj2nPadXAQ0lGdVinu0ShWdeC4pHWwK1nNhPQD0E,8045
3
+ datfid-0.1.14.dist-info/licenses/LICENSE,sha256=5b2JLb1-P8Nt919phjUmP0umS82SxGMlIF2QFn9ILgM,1084
4
+ datfid-0.1.14.dist-info/METADATA,sha256=KHLb4wwu5ZIaFfI5R_GMa-_nhVFXo053t3CqYi_C6VE,5146
5
+ datfid-0.1.14.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
6
+ datfid-0.1.14.dist-info/top_level.txt,sha256=VFG23u9NMNQJ8S_IFfW-S5d8oSUd71SFA346ovh_uxA,7
7
+ datfid-0.1.14.dist-info/RECORD,,
@@ -1,87 +0,0 @@
1
- Metadata-Version: 2.4
2
- Name: datfid
3
- Version: 0.1.12
4
- Summary: SDK to access the DATFID API hosted on Hugging Face Spaces
5
- Author: DATFID
6
- Author-email: igor.schapiro@datfid.com
7
- License: MIT
8
- Classifier: License :: OSI Approved :: MIT License
9
- Classifier: Programming Language :: Python :: 3
10
- Requires-Python: >=3.7
11
- Description-Content-Type: text/markdown
12
- License-File: LICENSE
13
- Requires-Dist: requests>=2.31.0
14
- Requires-Dist: pandas>=1.0.1
15
- Requires-Dist: numpy<2.1,>=1.22
16
- Dynamic: author
17
- Dynamic: author-email
18
- Dynamic: classifier
19
- Dynamic: description
20
- Dynamic: description-content-type
21
- Dynamic: license
22
- Dynamic: license-file
23
- Dynamic: requires-dist
24
- Dynamic: requires-python
25
- Dynamic: summary
26
-
27
- # DATFID SDK
28
-
29
- A Python SDK to access the DATFID API running on Hugging Face Spaces.
30
-
31
- ## Installation
32
-
33
- ```bash
34
- pip install datfid
35
- ```
36
-
37
- ## Usage
38
-
39
- ```python
40
- from datfid import DATFIDClient
41
-
42
- # Initialize the client with your Hugging Face token
43
- client = DATFIDClient(token="your_huggingface_token")
44
-
45
- # Fit a model
46
- fit_result = client.fit_model(
47
- file_path="path/to/your/data.xlsx",
48
- id_col="Individual",
49
- time_col="Time",
50
- y="Loan Probability",
51
- lagged_features={"Repayment Amount": 1, "Missed Payments": 2},
52
- current_features=["Credit Score", "Unemployment Rate"],
53
- filter_by_significance=True,
54
- meanvar_test=False
55
- )
56
-
57
- # Generate forecasts
58
- forecast_df = client.generate_forecast(
59
- file_path="path/to/your/forecast_data.xlsx",
60
- id_col="Individual",
61
- time_col="Time",
62
- y="Loan Probability",
63
- lagged_features={"Repayment Amount": 1, "Missed Payments": 2},
64
- current_features=["Credit Score", "Unemployment Rate"],
65
- filter_by_significance=True,
66
- meanvar_test=False
67
- )
68
-
69
- # The forecast DataFrame includes the original data plus forecast columns:
70
- # - forecast: The predicted values
71
- # - forecast_lower: Lower bound of the prediction interval
72
- # - forecast_upper: Upper bound of the prediction interval
73
- # - forecast_error: Standard error of the forecast
74
- ```
75
-
76
- ## API Reference
77
-
78
- ### DATFIDClient
79
-
80
- #### `__init__(token: str)`
81
- Initialize the client with your Hugging Face token.
82
-
83
- #### `fit_model(file_path: str, id_col: str, time_col: str, y: str, lagged_features: Optional[Dict[str, int]] = None, current_features: Optional[list] = None, filter_by_significance: bool = False, meanvar_test: bool = False) -> Dict[str, Any]`
84
- Fit a model using the provided data.
85
-
86
- #### `generate_forecast(file_path: str, id_col: str, time_col: str, y: str, lagged_features: Optional[Dict[str, int]] = None, current_features: Optional[list] = None, filter_by_significance: bool = False, meanvar_test: bool = False) -> pd.DataFrame`
87
- Generate forecasts using the fitted model.
@@ -1,7 +0,0 @@
1
- datfid/__init__.py,sha256=PojdUQsimZvawR8lgqVpzs3EkRtkA7iwuDk7fovJF54,34
2
- datfid/client.py,sha256=XH8wTBsHOHT3Fl1DzDWP9PfIUxJGg24eLH7_cWuymic,4656
3
- datfid-0.1.12.dist-info/licenses/LICENSE,sha256=5b2JLb1-P8Nt919phjUmP0umS82SxGMlIF2QFn9ILgM,1084
4
- datfid-0.1.12.dist-info/METADATA,sha256=cTQxUumLGxbsYwS09JxmJJrOIcit1ZCgeKE-nenwd-I,2750
5
- datfid-0.1.12.dist-info/WHEEL,sha256=_zCd3N1l69ArxyTb8rzEoP9TpbYXkqRFSNOD5OuxnTs,91
6
- datfid-0.1.12.dist-info/top_level.txt,sha256=VFG23u9NMNQJ8S_IFfW-S5d8oSUd71SFA346ovh_uxA,7
7
- datfid-0.1.12.dist-info/RECORD,,