simulacrum-sdk 0.1.0__tar.gz
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.
Potentially problematic release.
This version of simulacrum-sdk might be problematic. Click here for more details.
- simulacrum_sdk-0.1.0/LICENSE +21 -0
- simulacrum_sdk-0.1.0/MANIFEST.in +2 -0
- simulacrum_sdk-0.1.0/PKG-INFO +212 -0
- simulacrum_sdk-0.1.0/README.md +179 -0
- simulacrum_sdk-0.1.0/pyproject.toml +52 -0
- simulacrum_sdk-0.1.0/setup.cfg +4 -0
- simulacrum_sdk-0.1.0/simulacrum/__init__.py +15 -0
- simulacrum_sdk-0.1.0/simulacrum/api.py +48 -0
- simulacrum_sdk-0.1.0/simulacrum/client.py +122 -0
- simulacrum_sdk-0.1.0/simulacrum/config.py +4 -0
- simulacrum_sdk-0.1.0/simulacrum/exceptions.py +49 -0
- simulacrum_sdk-0.1.0/simulacrum/models.py +82 -0
- simulacrum_sdk-0.1.0/simulacrum_sdk.egg-info/PKG-INFO +212 -0
- simulacrum_sdk-0.1.0/simulacrum_sdk.egg-info/SOURCES.txt +16 -0
- simulacrum_sdk-0.1.0/simulacrum_sdk.egg-info/dependency_links.txt +1 -0
- simulacrum_sdk-0.1.0/simulacrum_sdk.egg-info/requires.txt +8 -0
- simulacrum_sdk-0.1.0/simulacrum_sdk.egg-info/top_level.txt +1 -0
- simulacrum_sdk-0.1.0/tests/test_client.py +113 -0
|
@@ -0,0 +1,21 @@
|
|
|
1
|
+
MIT License
|
|
2
|
+
|
|
3
|
+
Copyright (c) 2024 Simulacrum SDK contributors
|
|
4
|
+
|
|
5
|
+
Permission is hereby granted, free of charge, to any person obtaining a copy
|
|
6
|
+
of this software and associated documentation files (the "Software"), to deal
|
|
7
|
+
in the Software without restriction, including without limitation the rights
|
|
8
|
+
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
|
9
|
+
copies of the Software, and to permit persons to whom the Software is
|
|
10
|
+
furnished to do so, subject to the following conditions:
|
|
11
|
+
|
|
12
|
+
The above copyright notice and this permission notice shall be included in all
|
|
13
|
+
copies or substantial portions of the Software.
|
|
14
|
+
|
|
15
|
+
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
|
16
|
+
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
|
17
|
+
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
|
18
|
+
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
|
19
|
+
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
|
20
|
+
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
|
21
|
+
SOFTWARE.
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: simulacrum-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Official Python SDK for accessing the Simulacrum API.
|
|
5
|
+
Author-email: "Simulacrum, Inc." <support@smlcrm.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://smlcrm-tempo-api.readme.io/reference
|
|
8
|
+
Project-URL: Repository, https://github.com/Smlcrm/simulacrum-sdk
|
|
9
|
+
Project-URL: Issues, https://github.com/Smlcrm/simulacrum-sdk/issues
|
|
10
|
+
Keywords: Simulacrum,time-series,forecasting,sdk
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
22
|
+
Requires-Python: >=3.8
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Requires-Dist: requests<3,>=2.28
|
|
26
|
+
Requires-Dist: pydantic<3,>=2.0
|
|
27
|
+
Requires-Dist: numpy>=1.24
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
30
|
+
Requires-Dist: build>=1.2; extra == "dev"
|
|
31
|
+
Requires-Dist: twine>=5.0; extra == "dev"
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+

|
|
35
|
+
|
|
36
|
+
# Simulacrum SDK
|
|
37
|
+
|
|
38
|
+
A lightweight Python client for the Simulacrum time-series forecasting API. The SDK wraps Simulacrum's REST endpoints with type-safe models, error handling, and convenience helpers so you can focus on building forecasting workflows instead of wiring HTTP requests.
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## Features
|
|
43
|
+
|
|
44
|
+
- ๐ **Authenticated client** with automatic bearer-token headers
|
|
45
|
+
- ๐ **Forecast API wrapper** that serialises NumPy arrays transparently
|
|
46
|
+
- โ
**API key validation** to inspect key status, client ID, and expiry
|
|
47
|
+
- ๐ซ **Rich exceptions** that map Simulacrum error codes to Python types
|
|
48
|
+
- ๐งช **Tested models** built on Pydantic for strict data validation
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## Installation
|
|
53
|
+
|
|
54
|
+
### From PyPI (recommended)
|
|
55
|
+
|
|
56
|
+
> Requires Python 3.8 or newer
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pip install simulacrum-sdk
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### From GitHub source
|
|
63
|
+
|
|
64
|
+
Install directly from the latest commit on the main repository:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
pip install git+https://github.com/Smlcrm/simulacrum-sdk.git
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
To work with the sources locally for development (Python 3.8+):
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
git clone https://github.com/Smlcrm/simulacrum-sdk.git
|
|
74
|
+
cd simulacrum-sdk
|
|
75
|
+
python -m venv .venv
|
|
76
|
+
source .venv/bin/activate # On Windows use: .venv\Scripts\activate
|
|
77
|
+
pip install -e .[dev]
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## Usage Overview
|
|
83
|
+
|
|
84
|
+
### Creating a client
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
from simulacrum import Simulacrum
|
|
88
|
+
|
|
89
|
+
client = Simulacrum(api_key="sp_your_api_key")
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
If you are running a local version of the Simulacrum API (i.e., on-premise model hosting), override the base URL:
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
client = Simulacrum(api_key="sp_your_api_key", base_url="https://staging.api.smlcrm.com")
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Validating an API key
|
|
99
|
+
|
|
100
|
+
Your forecast requests will fail if your API key is invalid. To check your API key is valid run the following.
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
validation = client.validate()
|
|
104
|
+
print("Valid:", validation.valid)
|
|
105
|
+
print("Client ID:", validation.client)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Requesting a forecast
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
import numpy as np
|
|
112
|
+
|
|
113
|
+
series = np.array([102.4, 106.0, 108.3, 111.9])
|
|
114
|
+
forecast = client.forecast(series=series, horizon=3, model="default")
|
|
115
|
+
|
|
116
|
+
print("Next periods:", forecast)
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
The SDK returns a `numpy.ndarray` so you can pipe results into downstream analytics or visualisations immediately.
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
### Handling errors
|
|
124
|
+
|
|
125
|
+
All API error codes are mapped to dedicated exceptions. Catch them to distinguish between authentication, quota, and request issues:
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
from simulacrum.exceptions import AuthError, QuotaExceededError, SimulacrumError
|
|
129
|
+
|
|
130
|
+
try:
|
|
131
|
+
client.forecast(series=[1, 2, 3], horizon=5, model="default")
|
|
132
|
+
except AuthError:
|
|
133
|
+
print("Check that your API key is correct and active.")
|
|
134
|
+
except QuotaExceededError:
|
|
135
|
+
print("You have reached your usage limit for the current period.")
|
|
136
|
+
except SimulacrumError as exc:
|
|
137
|
+
print(f"Unhandled Simulacrum error: {exc}")
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
## Tutorial: Forecast a Time Series in Five Steps
|
|
143
|
+
|
|
144
|
+
1. **Install the SDK**
|
|
145
|
+
```bash
|
|
146
|
+
pip install simulacrum-sdk
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
2. **Create a project structure**
|
|
150
|
+
```bash
|
|
151
|
+
mkdir simulacrum-sample && cd simulacrum-sample
|
|
152
|
+
python -m venv .venv
|
|
153
|
+
source .venv/bin/activate
|
|
154
|
+
pip install simulacrum-sdk
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
3. **Write a forecast script (`forecast_example.py`)**
|
|
158
|
+
```python
|
|
159
|
+
from simulacrum import Simulacrum
|
|
160
|
+
|
|
161
|
+
def main() -> None:
|
|
162
|
+
client = Simulacrum(api_key="sp_your_api_key")
|
|
163
|
+
series = [24.5, 25.1, 25.7, 26.2, 26.9]
|
|
164
|
+
forecast = client.forecast(series=series, horizon=3, model="default")
|
|
165
|
+
print("Forecast:", forecast.tolist())
|
|
166
|
+
|
|
167
|
+
if __name__ == "__main__":
|
|
168
|
+
main()
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
4. **Validate your API key (optional)**
|
|
172
|
+
```python
|
|
173
|
+
validation = client.validate()
|
|
174
|
+
if validation.valid:
|
|
175
|
+
print("Key is active until", validation.expires_at)
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
5. **Run the script**
|
|
179
|
+
```bash
|
|
180
|
+
python forecast_example.py
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
This workflow demonstrates the complete loop: initialising the client, requesting a forecast, and checking key status.
|
|
184
|
+
|
|
185
|
+
---
|
|
186
|
+
|
|
187
|
+
## Documentation
|
|
188
|
+
|
|
189
|
+
The public API is intentionally small:
|
|
190
|
+
|
|
191
|
+
| Component | Description |
|
|
192
|
+
|-----------|-------------|
|
|
193
|
+
| `simulacrum.Simulacrum` | High-level client exposing `forecast()` and `validate()` methods. |
|
|
194
|
+
| `simulacrum.models.ForecastRequest` | Pydantic model ensuring forecast payloads are well-formed. |
|
|
195
|
+
| `simulacrum.models.ForecastResponse` | Wraps forecast results and exposes `get_forecast()` to return a `numpy.ndarray`. |
|
|
196
|
+
| `simulacrum.models.ValidateAPIKeyResponse` | Validation metadata returned by `Simulacrum.validate()`. |
|
|
197
|
+
| `simulacrum.exceptions.*` | Custom error hierarchy mapping Simulacrum error codes to Python exceptions. |
|
|
198
|
+
|
|
199
|
+
Explore inline docstrings for detailed parameter and return type information. The tests in [`tests/test_client.py`](tests/test_client.py) demonstrate advanced usage patterns and validation behavior.
|
|
200
|
+
|
|
201
|
+
If you are contributing, run the suite with:
|
|
202
|
+
|
|
203
|
+
```bash
|
|
204
|
+
pip install -e .[dev]
|
|
205
|
+
python -m pytest
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
---
|
|
209
|
+
|
|
210
|
+
## License
|
|
211
|
+
|
|
212
|
+
MIT ยฉ Simulacrum, Inc.
|
|
@@ -0,0 +1,179 @@
|
|
|
1
|
+

|
|
2
|
+
|
|
3
|
+
# Simulacrum SDK
|
|
4
|
+
|
|
5
|
+
A lightweight Python client for the Simulacrum time-series forecasting API. The SDK wraps Simulacrum's REST endpoints with type-safe models, error handling, and convenience helpers so you can focus on building forecasting workflows instead of wiring HTTP requests.
|
|
6
|
+
|
|
7
|
+
---
|
|
8
|
+
|
|
9
|
+
## Features
|
|
10
|
+
|
|
11
|
+
- ๐ **Authenticated client** with automatic bearer-token headers
|
|
12
|
+
- ๐ **Forecast API wrapper** that serialises NumPy arrays transparently
|
|
13
|
+
- โ
**API key validation** to inspect key status, client ID, and expiry
|
|
14
|
+
- ๐ซ **Rich exceptions** that map Simulacrum error codes to Python types
|
|
15
|
+
- ๐งช **Tested models** built on Pydantic for strict data validation
|
|
16
|
+
|
|
17
|
+
---
|
|
18
|
+
|
|
19
|
+
## Installation
|
|
20
|
+
|
|
21
|
+
### From PyPI (recommended)
|
|
22
|
+
|
|
23
|
+
> Requires Python 3.8 or newer
|
|
24
|
+
|
|
25
|
+
```bash
|
|
26
|
+
pip install simulacrum-sdk
|
|
27
|
+
```
|
|
28
|
+
|
|
29
|
+
### From GitHub source
|
|
30
|
+
|
|
31
|
+
Install directly from the latest commit on the main repository:
|
|
32
|
+
|
|
33
|
+
```bash
|
|
34
|
+
pip install git+https://github.com/Smlcrm/simulacrum-sdk.git
|
|
35
|
+
```
|
|
36
|
+
|
|
37
|
+
To work with the sources locally for development (Python 3.8+):
|
|
38
|
+
|
|
39
|
+
```bash
|
|
40
|
+
git clone https://github.com/Smlcrm/simulacrum-sdk.git
|
|
41
|
+
cd simulacrum-sdk
|
|
42
|
+
python -m venv .venv
|
|
43
|
+
source .venv/bin/activate # On Windows use: .venv\Scripts\activate
|
|
44
|
+
pip install -e .[dev]
|
|
45
|
+
```
|
|
46
|
+
|
|
47
|
+
---
|
|
48
|
+
|
|
49
|
+
## Usage Overview
|
|
50
|
+
|
|
51
|
+
### Creating a client
|
|
52
|
+
|
|
53
|
+
```python
|
|
54
|
+
from simulacrum import Simulacrum
|
|
55
|
+
|
|
56
|
+
client = Simulacrum(api_key="sp_your_api_key")
|
|
57
|
+
```
|
|
58
|
+
|
|
59
|
+
If you are running a local version of the Simulacrum API (i.e., on-premise model hosting), override the base URL:
|
|
60
|
+
|
|
61
|
+
```python
|
|
62
|
+
client = Simulacrum(api_key="sp_your_api_key", base_url="https://staging.api.smlcrm.com")
|
|
63
|
+
```
|
|
64
|
+
|
|
65
|
+
### Validating an API key
|
|
66
|
+
|
|
67
|
+
Your forecast requests will fail if your API key is invalid. To check your API key is valid run the following.
|
|
68
|
+
|
|
69
|
+
```python
|
|
70
|
+
validation = client.validate()
|
|
71
|
+
print("Valid:", validation.valid)
|
|
72
|
+
print("Client ID:", validation.client)
|
|
73
|
+
```
|
|
74
|
+
|
|
75
|
+
### Requesting a forecast
|
|
76
|
+
|
|
77
|
+
```python
|
|
78
|
+
import numpy as np
|
|
79
|
+
|
|
80
|
+
series = np.array([102.4, 106.0, 108.3, 111.9])
|
|
81
|
+
forecast = client.forecast(series=series, horizon=3, model="default")
|
|
82
|
+
|
|
83
|
+
print("Next periods:", forecast)
|
|
84
|
+
```
|
|
85
|
+
|
|
86
|
+
The SDK returns a `numpy.ndarray` so you can pipe results into downstream analytics or visualisations immediately.
|
|
87
|
+
|
|
88
|
+
|
|
89
|
+
|
|
90
|
+
### Handling errors
|
|
91
|
+
|
|
92
|
+
All API error codes are mapped to dedicated exceptions. Catch them to distinguish between authentication, quota, and request issues:
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
from simulacrum.exceptions import AuthError, QuotaExceededError, SimulacrumError
|
|
96
|
+
|
|
97
|
+
try:
|
|
98
|
+
client.forecast(series=[1, 2, 3], horizon=5, model="default")
|
|
99
|
+
except AuthError:
|
|
100
|
+
print("Check that your API key is correct and active.")
|
|
101
|
+
except QuotaExceededError:
|
|
102
|
+
print("You have reached your usage limit for the current period.")
|
|
103
|
+
except SimulacrumError as exc:
|
|
104
|
+
print(f"Unhandled Simulacrum error: {exc}")
|
|
105
|
+
```
|
|
106
|
+
|
|
107
|
+
---
|
|
108
|
+
|
|
109
|
+
## Tutorial: Forecast a Time Series in Five Steps
|
|
110
|
+
|
|
111
|
+
1. **Install the SDK**
|
|
112
|
+
```bash
|
|
113
|
+
pip install simulacrum-sdk
|
|
114
|
+
```
|
|
115
|
+
|
|
116
|
+
2. **Create a project structure**
|
|
117
|
+
```bash
|
|
118
|
+
mkdir simulacrum-sample && cd simulacrum-sample
|
|
119
|
+
python -m venv .venv
|
|
120
|
+
source .venv/bin/activate
|
|
121
|
+
pip install simulacrum-sdk
|
|
122
|
+
```
|
|
123
|
+
|
|
124
|
+
3. **Write a forecast script (`forecast_example.py`)**
|
|
125
|
+
```python
|
|
126
|
+
from simulacrum import Simulacrum
|
|
127
|
+
|
|
128
|
+
def main() -> None:
|
|
129
|
+
client = Simulacrum(api_key="sp_your_api_key")
|
|
130
|
+
series = [24.5, 25.1, 25.7, 26.2, 26.9]
|
|
131
|
+
forecast = client.forecast(series=series, horizon=3, model="default")
|
|
132
|
+
print("Forecast:", forecast.tolist())
|
|
133
|
+
|
|
134
|
+
if __name__ == "__main__":
|
|
135
|
+
main()
|
|
136
|
+
```
|
|
137
|
+
|
|
138
|
+
4. **Validate your API key (optional)**
|
|
139
|
+
```python
|
|
140
|
+
validation = client.validate()
|
|
141
|
+
if validation.valid:
|
|
142
|
+
print("Key is active until", validation.expires_at)
|
|
143
|
+
```
|
|
144
|
+
|
|
145
|
+
5. **Run the script**
|
|
146
|
+
```bash
|
|
147
|
+
python forecast_example.py
|
|
148
|
+
```
|
|
149
|
+
|
|
150
|
+
This workflow demonstrates the complete loop: initialising the client, requesting a forecast, and checking key status.
|
|
151
|
+
|
|
152
|
+
---
|
|
153
|
+
|
|
154
|
+
## Documentation
|
|
155
|
+
|
|
156
|
+
The public API is intentionally small:
|
|
157
|
+
|
|
158
|
+
| Component | Description |
|
|
159
|
+
|-----------|-------------|
|
|
160
|
+
| `simulacrum.Simulacrum` | High-level client exposing `forecast()` and `validate()` methods. |
|
|
161
|
+
| `simulacrum.models.ForecastRequest` | Pydantic model ensuring forecast payloads are well-formed. |
|
|
162
|
+
| `simulacrum.models.ForecastResponse` | Wraps forecast results and exposes `get_forecast()` to return a `numpy.ndarray`. |
|
|
163
|
+
| `simulacrum.models.ValidateAPIKeyResponse` | Validation metadata returned by `Simulacrum.validate()`. |
|
|
164
|
+
| `simulacrum.exceptions.*` | Custom error hierarchy mapping Simulacrum error codes to Python exceptions. |
|
|
165
|
+
|
|
166
|
+
Explore inline docstrings for detailed parameter and return type information. The tests in [`tests/test_client.py`](tests/test_client.py) demonstrate advanced usage patterns and validation behavior.
|
|
167
|
+
|
|
168
|
+
If you are contributing, run the suite with:
|
|
169
|
+
|
|
170
|
+
```bash
|
|
171
|
+
pip install -e .[dev]
|
|
172
|
+
python -m pytest
|
|
173
|
+
```
|
|
174
|
+
|
|
175
|
+
---
|
|
176
|
+
|
|
177
|
+
## License
|
|
178
|
+
|
|
179
|
+
MIT ยฉ Simulacrum, Inc.
|
|
@@ -0,0 +1,52 @@
|
|
|
1
|
+
[build-system]
|
|
2
|
+
requires = ["setuptools>=61.0", "wheel"]
|
|
3
|
+
build-backend = "setuptools.build_meta"
|
|
4
|
+
|
|
5
|
+
[project]
|
|
6
|
+
name = "simulacrum-sdk"
|
|
7
|
+
version = "0.1.0"
|
|
8
|
+
description = "Official Python SDK for accessing the Simulacrum API."
|
|
9
|
+
readme = "README.md"
|
|
10
|
+
requires-python = ">=3.8"
|
|
11
|
+
license = "MIT"
|
|
12
|
+
authors = [
|
|
13
|
+
{ name = "Simulacrum, Inc.", email = "support@smlcrm.com" }
|
|
14
|
+
]
|
|
15
|
+
keywords = ["Simulacrum", "time-series", "forecasting", "sdk"]
|
|
16
|
+
classifiers = [
|
|
17
|
+
"Development Status :: 3 - Alpha",
|
|
18
|
+
"Intended Audience :: Developers",
|
|
19
|
+
"Operating System :: OS Independent",
|
|
20
|
+
"Programming Language :: Python :: 3",
|
|
21
|
+
"Programming Language :: Python :: 3 :: Only",
|
|
22
|
+
"Programming Language :: Python :: 3.8",
|
|
23
|
+
"Programming Language :: Python :: 3.9",
|
|
24
|
+
"Programming Language :: Python :: 3.10",
|
|
25
|
+
"Programming Language :: Python :: 3.11",
|
|
26
|
+
"Programming Language :: Python :: 3.12",
|
|
27
|
+
"Topic :: Scientific/Engineering :: Artificial Intelligence"
|
|
28
|
+
]
|
|
29
|
+
dependencies = [
|
|
30
|
+
"requests>=2.28,<3",
|
|
31
|
+
"pydantic>=2.0,<3",
|
|
32
|
+
"numpy>=1.24"
|
|
33
|
+
]
|
|
34
|
+
|
|
35
|
+
[project.optional-dependencies]
|
|
36
|
+
dev = [
|
|
37
|
+
"pytest>=8.0",
|
|
38
|
+
"build>=1.2",
|
|
39
|
+
"twine>=5.0"
|
|
40
|
+
]
|
|
41
|
+
|
|
42
|
+
[project.urls]
|
|
43
|
+
Homepage = "https://smlcrm-tempo-api.readme.io/reference"
|
|
44
|
+
Repository = "https://github.com/Smlcrm/simulacrum-sdk"
|
|
45
|
+
Issues = "https://github.com/Smlcrm/simulacrum-sdk/issues"
|
|
46
|
+
|
|
47
|
+
[tool.setuptools.packages.find]
|
|
48
|
+
include = ["simulacrum*"]
|
|
49
|
+
|
|
50
|
+
[tool.pytest.ini_options]
|
|
51
|
+
addopts = "-ra"
|
|
52
|
+
testpaths = ["tests"]
|
|
@@ -0,0 +1,15 @@
|
|
|
1
|
+
"""Simulacrum SDK public interface.
|
|
2
|
+
|
|
3
|
+
This package exposes the primary client you will use to interact with the
|
|
4
|
+
Simulacrum forecasting API.
|
|
5
|
+
|
|
6
|
+
Example:
|
|
7
|
+
>>> from simulacrum import Simulacrum
|
|
8
|
+
>>> client = Simulacrum(api_key="sp_your_api_key")
|
|
9
|
+
>>> forecast = client.forecast(series=[1.0, 1.5, 2.0], horizon=3, model="default")
|
|
10
|
+
"""
|
|
11
|
+
|
|
12
|
+
from .client import Simulacrum
|
|
13
|
+
|
|
14
|
+
__all__ = ["Simulacrum", "__version__"]
|
|
15
|
+
__version__: str = "0.1.0"
|
|
@@ -0,0 +1,48 @@
|
|
|
1
|
+
"""Low-level HTTP helpers used by the Simulacrum client."""
|
|
2
|
+
|
|
3
|
+
from typing import Any, Dict, Mapping, Optional
|
|
4
|
+
|
|
5
|
+
import requests
|
|
6
|
+
|
|
7
|
+
from simulacrum.exceptions import ApiError, AuthError, ERROR_CODE_MAP
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
def send_request(method: str, url: str, headers: Mapping[str, str], json: Optional[Mapping[str, Any]]) -> Dict[str, Any]:
|
|
11
|
+
"""Execute an HTTP request against the Simulacrum API and handle common errors.
|
|
12
|
+
|
|
13
|
+
Args:
|
|
14
|
+
method (str): HTTP method to invoke (``"GET"``, ``"POST"``, ...).
|
|
15
|
+
url (str): Fully-qualified endpoint URL.
|
|
16
|
+
headers (Mapping[str, str]): HTTP headers that include authorization and content type.
|
|
17
|
+
json (Mapping[str, Any] | None): JSON-serialisable payload for the request body.
|
|
18
|
+
|
|
19
|
+
Returns:
|
|
20
|
+
dict[str, Any]: Parsed JSON payload returned by the API.
|
|
21
|
+
|
|
22
|
+
Raises:
|
|
23
|
+
AuthError: Raised when the API reports an authentication failure.
|
|
24
|
+
ApiError: Raised for all other non-success responses or malformed data.
|
|
25
|
+
"""
|
|
26
|
+
response = requests.request(method=method, url=url, headers=dict(headers), json=json)
|
|
27
|
+
|
|
28
|
+
if not response.ok:
|
|
29
|
+
try:
|
|
30
|
+
data: Dict[str, Any] = response.json()
|
|
31
|
+
error_code: Optional[str] = data.get("error_code")
|
|
32
|
+
message: str = data.get("message", "Unknown error")
|
|
33
|
+
|
|
34
|
+
if error_code in ERROR_CODE_MAP:
|
|
35
|
+
raise ERROR_CODE_MAP[error_code](message)
|
|
36
|
+
|
|
37
|
+
if response.status_code == 401:
|
|
38
|
+
raise AuthError(message)
|
|
39
|
+
|
|
40
|
+
raise ApiError(f"API error {response.status_code}: {message}")
|
|
41
|
+
|
|
42
|
+
except ValueError as exc:
|
|
43
|
+
raise ApiError(f"Unexpected API error: {response.text}") from exc
|
|
44
|
+
|
|
45
|
+
try:
|
|
46
|
+
return response.json() # type: ignore[return-value]
|
|
47
|
+
except ValueError as exc: # requests raises ValueError for JSON decode errors
|
|
48
|
+
raise ApiError(f"Failed to parse response JSON: {exc}") from exc
|
|
@@ -0,0 +1,122 @@
|
|
|
1
|
+
"""High-level client for interacting with the Simulacrum forecasting API."""
|
|
2
|
+
|
|
3
|
+
from typing import Any, Dict, Sequence
|
|
4
|
+
|
|
5
|
+
import numpy as np
|
|
6
|
+
|
|
7
|
+
from simulacrum.api import send_request
|
|
8
|
+
from simulacrum.config import BASE_URL
|
|
9
|
+
from simulacrum.exceptions import ApiError, AuthError
|
|
10
|
+
from simulacrum.models import ForecastRequest, ForecastResponse, ValidateAPIKeyResponse
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
class Simulacrum:
|
|
14
|
+
"""Client wrapper around Simulacrum's REST API.
|
|
15
|
+
|
|
16
|
+
Example:
|
|
17
|
+
>>> from simulacrum import Simulacrum
|
|
18
|
+
>>> client = Simulacrum(api_key="sp_example_key")
|
|
19
|
+
>>> forecast = client.forecast(series=[1.0, 1.1, 1.2], horizon=2, model="default")
|
|
20
|
+
>>> forecast.shape
|
|
21
|
+
(2,)
|
|
22
|
+
"""
|
|
23
|
+
|
|
24
|
+
def __init__(self, api_key: str, base_url: str = BASE_URL) -> None:
|
|
25
|
+
"""Create a client that can issue authenticated requests to Simulacrum.
|
|
26
|
+
|
|
27
|
+
Args:
|
|
28
|
+
api_key (str): Simulacrum API key that authorizes requests.
|
|
29
|
+
base_url (str): Base URL for the API; defaults to the production endpoint.
|
|
30
|
+
"""
|
|
31
|
+
if not isinstance(api_key, str) or not api_key.strip():
|
|
32
|
+
raise TypeError("api_key must be a non-empty string.")
|
|
33
|
+
if not isinstance(base_url, str) or not base_url.strip():
|
|
34
|
+
raise TypeError("base_url must be a non-empty string.")
|
|
35
|
+
|
|
36
|
+
self.api_key: str = api_key
|
|
37
|
+
self.base_url: str = base_url
|
|
38
|
+
self.headers: Dict[str, str] = {
|
|
39
|
+
"Authorization": f"Bearer {self.api_key}",
|
|
40
|
+
"Content-Type": "application/json",
|
|
41
|
+
}
|
|
42
|
+
|
|
43
|
+
def forecast(
|
|
44
|
+
self, series: Sequence[float] | np.ndarray, horizon: int, model: str = "default"
|
|
45
|
+
) -> np.ndarray:
|
|
46
|
+
"""Request a forecast for the provided time series.
|
|
47
|
+
|
|
48
|
+
Args:
|
|
49
|
+
series (Sequence[float] | numpy.ndarray): One-dimensional historical observations used as
|
|
50
|
+
forecast input.
|
|
51
|
+
horizon (int): Number of future periods to predict.
|
|
52
|
+
model (str): Identifier of the forecasting model, for example ``"default"``.
|
|
53
|
+
|
|
54
|
+
Returns:
|
|
55
|
+
numpy.ndarray: Array containing the forecasted values in chronological order.
|
|
56
|
+
|
|
57
|
+
Raises:
|
|
58
|
+
TypeError: ``series`` is not a numpy array or sequence of numeric values.
|
|
59
|
+
ValueError: ``series`` is a numpy array or sequence with dimensionality other than one.
|
|
60
|
+
ApiError: The API returned an error response.
|
|
61
|
+
AuthError: Authentication failed for the provided API key.
|
|
62
|
+
"""
|
|
63
|
+
if isinstance(series, np.ndarray):
|
|
64
|
+
if series.ndim != 1:
|
|
65
|
+
raise ValueError("series must be a one-dimensional numpy array.")
|
|
66
|
+
series_to_send = series.astype(float)
|
|
67
|
+
elif isinstance(series, Sequence):
|
|
68
|
+
if isinstance(series, (str, bytes)):
|
|
69
|
+
raise TypeError(
|
|
70
|
+
"series must be a one-dimensional numpy array or sequence of floats."
|
|
71
|
+
)
|
|
72
|
+
try:
|
|
73
|
+
series_to_send = np.asarray(list(series), dtype=float)
|
|
74
|
+
except (TypeError, ValueError) as exc:
|
|
75
|
+
raise TypeError(
|
|
76
|
+
"series must be a one-dimensional numpy array or sequence of floats."
|
|
77
|
+
) from exc
|
|
78
|
+
if series_to_send.ndim != 1:
|
|
79
|
+
raise ValueError("series must be one-dimensional.")
|
|
80
|
+
else:
|
|
81
|
+
raise TypeError(
|
|
82
|
+
"series must be a one-dimensional numpy array or sequence of floats."
|
|
83
|
+
)
|
|
84
|
+
|
|
85
|
+
payload: ForecastRequest = ForecastRequest(
|
|
86
|
+
series=series_to_send, horizon=horizon, model=model
|
|
87
|
+
)
|
|
88
|
+
request_body: Dict[str, Any] = payload.model_dump()
|
|
89
|
+
response_data: Dict[str, Any] = send_request(
|
|
90
|
+
method="POST",
|
|
91
|
+
url=f"{self.base_url}/v1/forecast",
|
|
92
|
+
headers=self.headers,
|
|
93
|
+
json=request_body,
|
|
94
|
+
)
|
|
95
|
+
validated_response: ForecastResponse = ForecastResponse.model_validate(
|
|
96
|
+
response_data
|
|
97
|
+
)
|
|
98
|
+
return validated_response.get_forecast()
|
|
99
|
+
|
|
100
|
+
def validate(self) -> ValidateAPIKeyResponse:
|
|
101
|
+
"""Validate the configured API key and return its metadata.
|
|
102
|
+
|
|
103
|
+
Returns:
|
|
104
|
+
ValidateAPIKeyResponse: Structured validation details including key status and expiration date.
|
|
105
|
+
|
|
106
|
+
Raises:
|
|
107
|
+
AuthError: The API key is invalid or unauthorized.
|
|
108
|
+
ApiError: An unexpected API error occurred.
|
|
109
|
+
|
|
110
|
+
Example:
|
|
111
|
+
>>> client = Simulacrum(api_key="sp_example_key")
|
|
112
|
+
>>> validation = client.validate()
|
|
113
|
+
>>> validation.valid
|
|
114
|
+
True
|
|
115
|
+
"""
|
|
116
|
+
response_data: Dict[str, Any] = send_request(
|
|
117
|
+
method="GET",
|
|
118
|
+
url=f"{self.base_url}/v1/validate",
|
|
119
|
+
headers=self.headers,
|
|
120
|
+
json=None,
|
|
121
|
+
)
|
|
122
|
+
return ValidateAPIKeyResponse.model_validate(response_data)
|
|
@@ -0,0 +1,49 @@
|
|
|
1
|
+
"""Custom exceptions raised by the Simulacrum SDK."""
|
|
2
|
+
|
|
3
|
+
from typing import Dict, Type
|
|
4
|
+
|
|
5
|
+
|
|
6
|
+
class SimulacrumError(Exception):
|
|
7
|
+
"""Base exception for all SDK errors."""
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class AuthError(SimulacrumError):
|
|
11
|
+
"""Raised when authentication with the API fails."""
|
|
12
|
+
|
|
13
|
+
|
|
14
|
+
class ApiKeyExpiredError(SimulacrumError):
|
|
15
|
+
"""Raised when the API key has expired."""
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
class ApiKeyInactiveError(SimulacrumError):
|
|
19
|
+
"""Raised when the API key has been deactivated."""
|
|
20
|
+
|
|
21
|
+
|
|
22
|
+
class ApiKeyInvalidError(AuthError):
|
|
23
|
+
"""Raised when the API key is not recognised."""
|
|
24
|
+
|
|
25
|
+
|
|
26
|
+
class ForecastAlreadyRunningError(SimulacrumError):
|
|
27
|
+
"""Raised when a forecast job is already in progress."""
|
|
28
|
+
|
|
29
|
+
|
|
30
|
+
class InvalidRequestError(SimulacrumError):
|
|
31
|
+
"""Raised when the request payload is malformed."""
|
|
32
|
+
|
|
33
|
+
|
|
34
|
+
class QuotaExceededError(SimulacrumError):
|
|
35
|
+
"""Raised when the API usage quota has been exhausted."""
|
|
36
|
+
|
|
37
|
+
|
|
38
|
+
class ApiError(SimulacrumError):
|
|
39
|
+
"""Catch-all for unclassified API errors."""
|
|
40
|
+
|
|
41
|
+
|
|
42
|
+
ERROR_CODE_MAP: Dict[str, Type[SimulacrumError]] = {
|
|
43
|
+
"API_KEY_EXPIRED": ApiKeyExpiredError,
|
|
44
|
+
"API_KEY_INVALID": ApiKeyInvalidError,
|
|
45
|
+
"API_KEY_INACTIVE": ApiKeyInactiveError,
|
|
46
|
+
"API_USAGE_LIMIT": QuotaExceededError,
|
|
47
|
+
"REQUEST_INVALID": InvalidRequestError,
|
|
48
|
+
"FORECAST_ALREADY_RUNNING": ForecastAlreadyRunningError,
|
|
49
|
+
}
|
|
@@ -0,0 +1,82 @@
|
|
|
1
|
+
"""Data models that map request and response payloads for the Simulacrum API."""
|
|
2
|
+
|
|
3
|
+
from datetime import datetime
|
|
4
|
+
from typing import List, Optional, Sequence, Union
|
|
5
|
+
|
|
6
|
+
import numpy as np
|
|
7
|
+
from pydantic import BaseModel, field_validator
|
|
8
|
+
|
|
9
|
+
|
|
10
|
+
class ForecastRequest(BaseModel):
|
|
11
|
+
"""Payload submitted to the Simulacrum forecast endpoint.
|
|
12
|
+
|
|
13
|
+
Attributes:
|
|
14
|
+
series (list[float]): Historical observations used as forecast input.
|
|
15
|
+
horizon (int): Number of future periods to predict.
|
|
16
|
+
model (str | None): Identifier of the forecasting model, defaults to ``"default"``.
|
|
17
|
+
|
|
18
|
+
Example:
|
|
19
|
+
>>> from simulacrum.models import ForecastRequest
|
|
20
|
+
>>> payload = ForecastRequest(series=[1.0, 2.0, 3.0], horizon=2, model="default")
|
|
21
|
+
>>> payload.model_dump()
|
|
22
|
+
{'series': [1.0, 2.0, 3.0], 'horizon': 2, 'model': 'default'}
|
|
23
|
+
"""
|
|
24
|
+
|
|
25
|
+
series: List[float]
|
|
26
|
+
horizon: int
|
|
27
|
+
model: Optional[str] = "default"
|
|
28
|
+
|
|
29
|
+
@field_validator("series", mode="before")
|
|
30
|
+
@classmethod
|
|
31
|
+
def _ensure_series_list(cls, value: Union[np.ndarray, Sequence[float]]) -> List[float]: # type: ignore[override]
|
|
32
|
+
"""Normalise the series field to ``list[float]``.
|
|
33
|
+
|
|
34
|
+
Args:
|
|
35
|
+
value (numpy.ndarray | Sequence[float]): Incoming value from caller.
|
|
36
|
+
|
|
37
|
+
Returns:
|
|
38
|
+
list[float]: Serialisable list of floats.
|
|
39
|
+
"""
|
|
40
|
+
if isinstance(value, np.ndarray):
|
|
41
|
+
return value.astype(float).tolist()
|
|
42
|
+
return list(value)
|
|
43
|
+
|
|
44
|
+
|
|
45
|
+
class ForecastResponse(BaseModel):
|
|
46
|
+
"""Forecast output returned by the API.
|
|
47
|
+
|
|
48
|
+
Attributes:
|
|
49
|
+
forecast (list[float]): Forecasted values returned by the service.
|
|
50
|
+
model_used (str): Identifier of the model the backend selected.
|
|
51
|
+
|
|
52
|
+
Example:
|
|
53
|
+
>>> from simulacrum.models import ForecastResponse
|
|
54
|
+
>>> response = ForecastResponse(forecast=[4.2, 4.8], model_used="default")
|
|
55
|
+
>>> response.get_forecast().tolist()
|
|
56
|
+
[4.2, 4.8]
|
|
57
|
+
"""
|
|
58
|
+
|
|
59
|
+
forecast: List[float]
|
|
60
|
+
model_used: str
|
|
61
|
+
|
|
62
|
+
def get_forecast(self) -> np.ndarray:
|
|
63
|
+
"""Return forecast values as a numpy array for downstream processing.
|
|
64
|
+
|
|
65
|
+
Returns:
|
|
66
|
+
numpy.ndarray: Forecast data cast to an array of floats.
|
|
67
|
+
"""
|
|
68
|
+
return np.array(self.forecast, dtype=float)
|
|
69
|
+
|
|
70
|
+
|
|
71
|
+
class ValidateAPIKeyResponse(BaseModel):
|
|
72
|
+
"""Metadata describing the validity of an API key.
|
|
73
|
+
|
|
74
|
+
Attributes:
|
|
75
|
+
valid (bool): Indicates whether the API key is currently valid.
|
|
76
|
+
client (str): Identifier of the owning client account.
|
|
77
|
+
expires_at (datetime | None): Expiration timestamp if provided by the API.
|
|
78
|
+
"""
|
|
79
|
+
|
|
80
|
+
valid: bool
|
|
81
|
+
client: str
|
|
82
|
+
expires_at: Optional[datetime]
|
|
@@ -0,0 +1,212 @@
|
|
|
1
|
+
Metadata-Version: 2.4
|
|
2
|
+
Name: simulacrum-sdk
|
|
3
|
+
Version: 0.1.0
|
|
4
|
+
Summary: Official Python SDK for accessing the Simulacrum API.
|
|
5
|
+
Author-email: "Simulacrum, Inc." <support@smlcrm.com>
|
|
6
|
+
License-Expression: MIT
|
|
7
|
+
Project-URL: Homepage, https://smlcrm-tempo-api.readme.io/reference
|
|
8
|
+
Project-URL: Repository, https://github.com/Smlcrm/simulacrum-sdk
|
|
9
|
+
Project-URL: Issues, https://github.com/Smlcrm/simulacrum-sdk/issues
|
|
10
|
+
Keywords: Simulacrum,time-series,forecasting,sdk
|
|
11
|
+
Classifier: Development Status :: 3 - Alpha
|
|
12
|
+
Classifier: Intended Audience :: Developers
|
|
13
|
+
Classifier: Operating System :: OS Independent
|
|
14
|
+
Classifier: Programming Language :: Python :: 3
|
|
15
|
+
Classifier: Programming Language :: Python :: 3 :: Only
|
|
16
|
+
Classifier: Programming Language :: Python :: 3.8
|
|
17
|
+
Classifier: Programming Language :: Python :: 3.9
|
|
18
|
+
Classifier: Programming Language :: Python :: 3.10
|
|
19
|
+
Classifier: Programming Language :: Python :: 3.11
|
|
20
|
+
Classifier: Programming Language :: Python :: 3.12
|
|
21
|
+
Classifier: Topic :: Scientific/Engineering :: Artificial Intelligence
|
|
22
|
+
Requires-Python: >=3.8
|
|
23
|
+
Description-Content-Type: text/markdown
|
|
24
|
+
License-File: LICENSE
|
|
25
|
+
Requires-Dist: requests<3,>=2.28
|
|
26
|
+
Requires-Dist: pydantic<3,>=2.0
|
|
27
|
+
Requires-Dist: numpy>=1.24
|
|
28
|
+
Provides-Extra: dev
|
|
29
|
+
Requires-Dist: pytest>=8.0; extra == "dev"
|
|
30
|
+
Requires-Dist: build>=1.2; extra == "dev"
|
|
31
|
+
Requires-Dist: twine>=5.0; extra == "dev"
|
|
32
|
+
Dynamic: license-file
|
|
33
|
+
|
|
34
|
+

|
|
35
|
+
|
|
36
|
+
# Simulacrum SDK
|
|
37
|
+
|
|
38
|
+
A lightweight Python client for the Simulacrum time-series forecasting API. The SDK wraps Simulacrum's REST endpoints with type-safe models, error handling, and convenience helpers so you can focus on building forecasting workflows instead of wiring HTTP requests.
|
|
39
|
+
|
|
40
|
+
---
|
|
41
|
+
|
|
42
|
+
## Features
|
|
43
|
+
|
|
44
|
+
- ๐ **Authenticated client** with automatic bearer-token headers
|
|
45
|
+
- ๐ **Forecast API wrapper** that serialises NumPy arrays transparently
|
|
46
|
+
- โ
**API key validation** to inspect key status, client ID, and expiry
|
|
47
|
+
- ๐ซ **Rich exceptions** that map Simulacrum error codes to Python types
|
|
48
|
+
- ๐งช **Tested models** built on Pydantic for strict data validation
|
|
49
|
+
|
|
50
|
+
---
|
|
51
|
+
|
|
52
|
+
## Installation
|
|
53
|
+
|
|
54
|
+
### From PyPI (recommended)
|
|
55
|
+
|
|
56
|
+
> Requires Python 3.8 or newer
|
|
57
|
+
|
|
58
|
+
```bash
|
|
59
|
+
pip install simulacrum-sdk
|
|
60
|
+
```
|
|
61
|
+
|
|
62
|
+
### From GitHub source
|
|
63
|
+
|
|
64
|
+
Install directly from the latest commit on the main repository:
|
|
65
|
+
|
|
66
|
+
```bash
|
|
67
|
+
pip install git+https://github.com/Smlcrm/simulacrum-sdk.git
|
|
68
|
+
```
|
|
69
|
+
|
|
70
|
+
To work with the sources locally for development (Python 3.8+):
|
|
71
|
+
|
|
72
|
+
```bash
|
|
73
|
+
git clone https://github.com/Smlcrm/simulacrum-sdk.git
|
|
74
|
+
cd simulacrum-sdk
|
|
75
|
+
python -m venv .venv
|
|
76
|
+
source .venv/bin/activate # On Windows use: .venv\Scripts\activate
|
|
77
|
+
pip install -e .[dev]
|
|
78
|
+
```
|
|
79
|
+
|
|
80
|
+
---
|
|
81
|
+
|
|
82
|
+
## Usage Overview
|
|
83
|
+
|
|
84
|
+
### Creating a client
|
|
85
|
+
|
|
86
|
+
```python
|
|
87
|
+
from simulacrum import Simulacrum
|
|
88
|
+
|
|
89
|
+
client = Simulacrum(api_key="sp_your_api_key")
|
|
90
|
+
```
|
|
91
|
+
|
|
92
|
+
If you are running a local version of the Simulacrum API (i.e., on-premise model hosting), override the base URL:
|
|
93
|
+
|
|
94
|
+
```python
|
|
95
|
+
client = Simulacrum(api_key="sp_your_api_key", base_url="https://staging.api.smlcrm.com")
|
|
96
|
+
```
|
|
97
|
+
|
|
98
|
+
### Validating an API key
|
|
99
|
+
|
|
100
|
+
Your forecast requests will fail if your API key is invalid. To check your API key is valid run the following.
|
|
101
|
+
|
|
102
|
+
```python
|
|
103
|
+
validation = client.validate()
|
|
104
|
+
print("Valid:", validation.valid)
|
|
105
|
+
print("Client ID:", validation.client)
|
|
106
|
+
```
|
|
107
|
+
|
|
108
|
+
### Requesting a forecast
|
|
109
|
+
|
|
110
|
+
```python
|
|
111
|
+
import numpy as np
|
|
112
|
+
|
|
113
|
+
series = np.array([102.4, 106.0, 108.3, 111.9])
|
|
114
|
+
forecast = client.forecast(series=series, horizon=3, model="default")
|
|
115
|
+
|
|
116
|
+
print("Next periods:", forecast)
|
|
117
|
+
```
|
|
118
|
+
|
|
119
|
+
The SDK returns a `numpy.ndarray` so you can pipe results into downstream analytics or visualisations immediately.
|
|
120
|
+
|
|
121
|
+
|
|
122
|
+
|
|
123
|
+
### Handling errors
|
|
124
|
+
|
|
125
|
+
All API error codes are mapped to dedicated exceptions. Catch them to distinguish between authentication, quota, and request issues:
|
|
126
|
+
|
|
127
|
+
```python
|
|
128
|
+
from simulacrum.exceptions import AuthError, QuotaExceededError, SimulacrumError
|
|
129
|
+
|
|
130
|
+
try:
|
|
131
|
+
client.forecast(series=[1, 2, 3], horizon=5, model="default")
|
|
132
|
+
except AuthError:
|
|
133
|
+
print("Check that your API key is correct and active.")
|
|
134
|
+
except QuotaExceededError:
|
|
135
|
+
print("You have reached your usage limit for the current period.")
|
|
136
|
+
except SimulacrumError as exc:
|
|
137
|
+
print(f"Unhandled Simulacrum error: {exc}")
|
|
138
|
+
```
|
|
139
|
+
|
|
140
|
+
---
|
|
141
|
+
|
|
142
|
+
## Tutorial: Forecast a Time Series in Five Steps
|
|
143
|
+
|
|
144
|
+
1. **Install the SDK**
|
|
145
|
+
```bash
|
|
146
|
+
pip install simulacrum-sdk
|
|
147
|
+
```
|
|
148
|
+
|
|
149
|
+
2. **Create a project structure**
|
|
150
|
+
```bash
|
|
151
|
+
mkdir simulacrum-sample && cd simulacrum-sample
|
|
152
|
+
python -m venv .venv
|
|
153
|
+
source .venv/bin/activate
|
|
154
|
+
pip install simulacrum-sdk
|
|
155
|
+
```
|
|
156
|
+
|
|
157
|
+
3. **Write a forecast script (`forecast_example.py`)**
|
|
158
|
+
```python
|
|
159
|
+
from simulacrum import Simulacrum
|
|
160
|
+
|
|
161
|
+
def main() -> None:
|
|
162
|
+
client = Simulacrum(api_key="sp_your_api_key")
|
|
163
|
+
series = [24.5, 25.1, 25.7, 26.2, 26.9]
|
|
164
|
+
forecast = client.forecast(series=series, horizon=3, model="default")
|
|
165
|
+
print("Forecast:", forecast.tolist())
|
|
166
|
+
|
|
167
|
+
if __name__ == "__main__":
|
|
168
|
+
main()
|
|
169
|
+
```
|
|
170
|
+
|
|
171
|
+
4. **Validate your API key (optional)**
|
|
172
|
+
```python
|
|
173
|
+
validation = client.validate()
|
|
174
|
+
if validation.valid:
|
|
175
|
+
print("Key is active until", validation.expires_at)
|
|
176
|
+
```
|
|
177
|
+
|
|
178
|
+
5. **Run the script**
|
|
179
|
+
```bash
|
|
180
|
+
python forecast_example.py
|
|
181
|
+
```
|
|
182
|
+
|
|
183
|
+
This workflow demonstrates the complete loop: initialising the client, requesting a forecast, and checking key status.
|
|
184
|
+
|
|
185
|
+
---
|
|
186
|
+
|
|
187
|
+
## Documentation
|
|
188
|
+
|
|
189
|
+
The public API is intentionally small:
|
|
190
|
+
|
|
191
|
+
| Component | Description |
|
|
192
|
+
|-----------|-------------|
|
|
193
|
+
| `simulacrum.Simulacrum` | High-level client exposing `forecast()` and `validate()` methods. |
|
|
194
|
+
| `simulacrum.models.ForecastRequest` | Pydantic model ensuring forecast payloads are well-formed. |
|
|
195
|
+
| `simulacrum.models.ForecastResponse` | Wraps forecast results and exposes `get_forecast()` to return a `numpy.ndarray`. |
|
|
196
|
+
| `simulacrum.models.ValidateAPIKeyResponse` | Validation metadata returned by `Simulacrum.validate()`. |
|
|
197
|
+
| `simulacrum.exceptions.*` | Custom error hierarchy mapping Simulacrum error codes to Python exceptions. |
|
|
198
|
+
|
|
199
|
+
Explore inline docstrings for detailed parameter and return type information. The tests in [`tests/test_client.py`](tests/test_client.py) demonstrate advanced usage patterns and validation behavior.
|
|
200
|
+
|
|
201
|
+
If you are contributing, run the suite with:
|
|
202
|
+
|
|
203
|
+
```bash
|
|
204
|
+
pip install -e .[dev]
|
|
205
|
+
python -m pytest
|
|
206
|
+
```
|
|
207
|
+
|
|
208
|
+
---
|
|
209
|
+
|
|
210
|
+
## License
|
|
211
|
+
|
|
212
|
+
MIT ยฉ Simulacrum, Inc.
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
LICENSE
|
|
2
|
+
MANIFEST.in
|
|
3
|
+
README.md
|
|
4
|
+
pyproject.toml
|
|
5
|
+
simulacrum/__init__.py
|
|
6
|
+
simulacrum/api.py
|
|
7
|
+
simulacrum/client.py
|
|
8
|
+
simulacrum/config.py
|
|
9
|
+
simulacrum/exceptions.py
|
|
10
|
+
simulacrum/models.py
|
|
11
|
+
simulacrum_sdk.egg-info/PKG-INFO
|
|
12
|
+
simulacrum_sdk.egg-info/SOURCES.txt
|
|
13
|
+
simulacrum_sdk.egg-info/dependency_links.txt
|
|
14
|
+
simulacrum_sdk.egg-info/requires.txt
|
|
15
|
+
simulacrum_sdk.egg-info/top_level.txt
|
|
16
|
+
tests/test_client.py
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
simulacrum
|
|
@@ -0,0 +1,113 @@
|
|
|
1
|
+
import numpy as np
|
|
2
|
+
import pytest
|
|
3
|
+
from pydantic import ValidationError
|
|
4
|
+
|
|
5
|
+
import simulacrum.client as simulacrum_client
|
|
6
|
+
|
|
7
|
+
|
|
8
|
+
@pytest.fixture
|
|
9
|
+
def client():
|
|
10
|
+
return simulacrum_client.Simulacrum("super-secret-key", base_url="https://api.test")
|
|
11
|
+
|
|
12
|
+
|
|
13
|
+
def test_client_initializes_expected_headers(client):
|
|
14
|
+
assert client.headers["Authorization"] == "Bearer super-secret-key"
|
|
15
|
+
assert client.headers["Content-Type"] == "application/json"
|
|
16
|
+
|
|
17
|
+
|
|
18
|
+
def test_validate_invokes_send_request_and_parses_response(client, monkeypatch):
|
|
19
|
+
captured = {}
|
|
20
|
+
|
|
21
|
+
def fake_send_request(method, url, headers, json):
|
|
22
|
+
captured["method"] = method
|
|
23
|
+
captured["url"] = url
|
|
24
|
+
captured["headers"] = headers
|
|
25
|
+
captured["json"] = json
|
|
26
|
+
return {
|
|
27
|
+
"valid": True,
|
|
28
|
+
"client": "client-123",
|
|
29
|
+
"expires_at": "2024-01-01T00:00:00Z",
|
|
30
|
+
}
|
|
31
|
+
|
|
32
|
+
monkeypatch.setattr(simulacrum_client, "send_request", fake_send_request)
|
|
33
|
+
|
|
34
|
+
response = client.validate()
|
|
35
|
+
|
|
36
|
+
assert captured == {
|
|
37
|
+
"method": "GET",
|
|
38
|
+
"url": "https://api.test/v1/validate",
|
|
39
|
+
"headers": client.headers,
|
|
40
|
+
"json": None,
|
|
41
|
+
}
|
|
42
|
+
assert response.valid is True
|
|
43
|
+
assert response.client == "client-123"
|
|
44
|
+
|
|
45
|
+
|
|
46
|
+
def test_forecast_builds_payload_and_returns_numpy_array(client, monkeypatch):
|
|
47
|
+
captured_payload = {}
|
|
48
|
+
|
|
49
|
+
class DummyRequest:
|
|
50
|
+
def __init__(self, *, series, horizon, model):
|
|
51
|
+
captured_payload["series"] = (
|
|
52
|
+
series.tolist() if hasattr(series, "tolist") else series
|
|
53
|
+
)
|
|
54
|
+
captured_payload["horizon"] = horizon
|
|
55
|
+
captured_payload["model"] = model
|
|
56
|
+
|
|
57
|
+
def model_dump(self):
|
|
58
|
+
return {
|
|
59
|
+
"series": captured_payload["series"],
|
|
60
|
+
"horizon": captured_payload["horizon"],
|
|
61
|
+
"model": captured_payload["model"],
|
|
62
|
+
}
|
|
63
|
+
|
|
64
|
+
def fake_send_request(method, url, headers, json):
|
|
65
|
+
captured_payload["method"] = method
|
|
66
|
+
captured_payload["url"] = url
|
|
67
|
+
captured_payload["headers"] = headers
|
|
68
|
+
captured_payload["json"] = json
|
|
69
|
+
return {
|
|
70
|
+
"forecast": [3.1, 3.9],
|
|
71
|
+
"model_used": "prophet",
|
|
72
|
+
}
|
|
73
|
+
|
|
74
|
+
monkeypatch.setattr(simulacrum_client, "ForecastRequest", DummyRequest)
|
|
75
|
+
monkeypatch.setattr(simulacrum_client, "send_request", fake_send_request)
|
|
76
|
+
|
|
77
|
+
series = np.array([1.0, 2.0, 3.0])
|
|
78
|
+
|
|
79
|
+
forecast = client.forecast(series=series, horizon=2, model="prophet")
|
|
80
|
+
|
|
81
|
+
assert captured_payload["series"] == [1.0, 2.0, 3.0]
|
|
82
|
+
assert captured_payload["horizon"] == 2
|
|
83
|
+
assert captured_payload["model"] == "prophet"
|
|
84
|
+
assert captured_payload["method"] == "POST"
|
|
85
|
+
assert captured_payload["url"] == "https://api.test/v1/forecast"
|
|
86
|
+
assert captured_payload["headers"] == client.headers
|
|
87
|
+
assert captured_payload["json"] == {
|
|
88
|
+
"series": [1.0, 2.0, 3.0],
|
|
89
|
+
"horizon": 2,
|
|
90
|
+
"model": "prophet",
|
|
91
|
+
}
|
|
92
|
+
assert isinstance(forecast, np.ndarray)
|
|
93
|
+
assert np.allclose(forecast, np.array([3.1, 3.9]))
|
|
94
|
+
|
|
95
|
+
|
|
96
|
+
def test_forecast_rejects_non_numeric_series(client):
|
|
97
|
+
with pytest.raises(TypeError):
|
|
98
|
+
client.forecast(series=["a", "b"], horizon=2, model="default")
|
|
99
|
+
|
|
100
|
+
|
|
101
|
+
def test_forecast_rejects_multidimensional_input(client):
|
|
102
|
+
with pytest.raises(ValueError):
|
|
103
|
+
client.forecast(series=[[1.0, 2.0], [3.0, 4.0]], horizon=2, model="default")
|
|
104
|
+
|
|
105
|
+
|
|
106
|
+
def test_forecast_rejects_non_integer_horizon(client):
|
|
107
|
+
with pytest.raises(ValidationError):
|
|
108
|
+
client.forecast(series=[1.0, 2.0], horizon="two", model="default")
|
|
109
|
+
|
|
110
|
+
|
|
111
|
+
def test_client_requires_string_api_key():
|
|
112
|
+
with pytest.raises(TypeError):
|
|
113
|
+
simulacrum_client.Simulacrum(api_key=None) # type: ignore[arg-type]
|