cloudos-cb-py 1.2.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.
@@ -0,0 +1,21 @@
1
+ MIT License
2
+
3
+ Copyright (c) 2026 Lifebit
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,400 @@
1
+ Metadata-Version: 2.4
2
+ Name: cloudos-cb-py
3
+ Version: 1.2.0
4
+ Summary: Python client for CloudOS Cohort Browser API
5
+ Author-email: David Pineyro <david.pineyro@lifebit.ai>
6
+ License: MIT License
7
+
8
+ Copyright (c) 2026 Lifebit
9
+
10
+ Permission is hereby granted, free of charge, to any person obtaining a copy
11
+ of this software and associated documentation files (the "Software"), to deal
12
+ in the Software without restriction, including without limitation the rights
13
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
14
+ copies of the Software, and to permit persons to whom the Software is
15
+ furnished to do so, subject to the following conditions:
16
+
17
+ The above copyright notice and this permission notice shall be included in all
18
+ copies or substantial portions of the Software.
19
+
20
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
21
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
22
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
23
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
24
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
25
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
26
+ SOFTWARE.
27
+
28
+ Project-URL: Homepage, https://github.com/lifebit-ai/cloudos-cb-py
29
+ Project-URL: Repository, https://github.com/lifebit-ai/cloudos-cb-py
30
+ Project-URL: Issues, https://github.com/lifebit-ai/cloudos-cb-py/issues
31
+ Project-URL: Changelog, https://github.com/lifebit-ai/cloudos-cb-py/blob/main/CHANGELOG.md
32
+ Keywords: cloudos,cohort-browser,api-client,bioinformatics
33
+ Classifier: Development Status :: 5 - Production/Stable
34
+ Classifier: Intended Audience :: Science/Research
35
+ Classifier: License :: OSI Approved :: MIT License
36
+ Classifier: Programming Language :: Python :: 3
37
+ Classifier: Programming Language :: Python :: 3.9
38
+ Classifier: Programming Language :: Python :: 3.10
39
+ Classifier: Programming Language :: Python :: 3.11
40
+ Classifier: Programming Language :: Python :: 3.12
41
+ Requires-Python: >=3.9
42
+ Description-Content-Type: text/markdown
43
+ License-File: LICENSE
44
+ Requires-Dist: requests>=2.28.0
45
+ Requires-Dist: pandas>=1.5.0
46
+ Provides-Extra: dev
47
+ Requires-Dist: pytest>=7.0.0; extra == "dev"
48
+ Requires-Dist: pytest-mock>=3.10.0; extra == "dev"
49
+ Requires-Dist: flake8>=6.0.0; extra == "dev"
50
+ Requires-Dist: responses>=0.23.0; extra == "dev"
51
+ Dynamic: license-file
52
+
53
+ # cloudos-cb-py
54
+
55
+ Python client for the CloudOS Cohort Browser API. Provides functions for schema discovery,
56
+ table exploration, and SQL query execution with team-based access control.
57
+
58
+ ## Requirements
59
+
60
+ - Python >= 3.9
61
+ - requests >= 2.28.0
62
+ - pandas >= 1.5.0
63
+
64
+ ## Prerequisites
65
+
66
+ **IMPORTANT:** Before using this package, ensure the following requirements are met:
67
+
68
+ - **Bastion must be enabled** for your workspace
69
+ - **You are running the package from within an interactive session**
70
+ - **The interactive session and the cohort queried must be in the same workspace**
71
+
72
+ Without these prerequisites, API calls will fail even with valid credentials.
73
+
74
+ ## Installation
75
+
76
+ ### From PyPI (recommended)
77
+
78
+ ```bash
79
+ pip install cloudos-cb-py
80
+ ```
81
+
82
+ ### From source
83
+
84
+ ```bash
85
+ git clone https://github.com/lifebit-ai/cloudos-cb-py
86
+ cd cloudos-cb-py
87
+ pip install .
88
+ ```
89
+
90
+ ### Development install (includes test dependencies)
91
+
92
+ ```bash
93
+ pip install -e ".[dev]"
94
+ ```
95
+
96
+ ## Quick Start
97
+
98
+ ### 1. Configure a profile
99
+
100
+ ```python
101
+ import cloudos_cb
102
+
103
+ cloudos_cb.configure(
104
+ profilename="production",
105
+ apikey="your-api-key-here",
106
+ workspace_id="953h453uhr73894hhr9348h9",
107
+ set_default=True,
108
+ )
109
+ ```
110
+
111
+ Credentials are stored in `~/.cloudos-cb/config.json` with 0600 permissions.
112
+ Set `CLOUDOS_CONFIG_DIR` to store the file elsewhere.
113
+
114
+ ### 2. List configured profiles
115
+
116
+ ```python
117
+ profiles = cloudos_cb.profile_list()
118
+ print(profiles)
119
+ # Returns a pandas DataFrame with columns:
120
+ # profile_name, workspace_id, base_url, default, created_at, updated_at
121
+ ```
122
+
123
+ ### 3. Discover cohort tables
124
+
125
+ ```python
126
+ tables = cloudos_cb.cohort_tables(cohort_id="1a2b3c4d5e6f7g8h9i10j11k")
127
+ print(tables)
128
+ # Cohort 1a2b3c4d5e6f7g8h9i10j11k:
129
+ # - omop_data.person
130
+ # - person_id (integer)
131
+ # - year_of_birth (integer)
132
+ # - gender_concept_id (integer)
133
+ # ...
134
+ # - omop_data.observation
135
+ # ...
136
+ #
137
+ # Total: 1 database(s), 5 table(s)
138
+
139
+ # Access raw data
140
+ schema_list = tables.schemas
141
+ ```
142
+
143
+ ### 4. Validate SQL (optional but recommended)
144
+
145
+ ```python
146
+ result = cloudos_cb.sql_validate(
147
+ sql="SELECT person_id FROM omop_data.person WHERE year_of_birth >= 1960"
148
+ )
149
+
150
+ if result["isValid"]:
151
+ print("SQL is valid")
152
+ else:
153
+ print("SQL invalid:", result["error"]["message"])
154
+ ```
155
+
156
+ ### 5. Execute a query (high-level)
157
+
158
+ ```python
159
+ df = cloudos_cb.query(
160
+ cohort_id="1a2b3c4d5e6f7g8h9i10j11k",
161
+ sql="SELECT person_id, gender_concept_id FROM omop_data.person LIMIT 100",
162
+ )
163
+ print(df.head())
164
+ print(f"Total rows: {df.attrs['total_rows']}")
165
+ ```
166
+
167
+ By default `query()` fetches all pages automatically. To return only the first page:
168
+
169
+ ```python
170
+ df = cloudos_cb.query(
171
+ cohort_id="1a2b3c4d5e6f7g8h9i10j11k",
172
+ sql="SELECT person_id FROM omop_data.person",
173
+ all_pages=False,
174
+ page_size=500,
175
+ )
176
+ ```
177
+
178
+ ### 6. Manual workflow
179
+
180
+ For fine-grained control over the submit / poll / fetch cycle:
181
+
182
+ ```python
183
+ # Step 1: Submit
184
+ task = cloudos_cb.query_submit_async(
185
+ cohort_id="1a2b3c4d5e6f7g8h9i10j11k",
186
+ sql="SELECT person_id FROM omop_data.person",
187
+ pagination={"pageNumber": 0, "pageSize": 100},
188
+ )
189
+ print("Task ID:", task["task_id"])
190
+
191
+ # Step 2: Poll
192
+ status = cloudos_cb.query_status(task_id=task["task_id"])
193
+ print("Status:", status["status"])
194
+ # status["status"] is one of: "pending", "running", "completed", "failed"
195
+
196
+ # Step 3: Fetch results when completed
197
+ df = cloudos_cb.query_results(task_id=task["task_id"])
198
+ print(df)
199
+ ```
200
+
201
+ ## API Reference
202
+
203
+ ### `configure(profilename, apikey, workspace_id, base_url=..., set_default=False)`
204
+
205
+ Create or update a named credential profile.
206
+
207
+ | Parameter | Type | Description |
208
+ |-----------|------|-------------|
209
+ | `profilename` | str | Profile name (required) |
210
+ | `apikey` | str | API key (required) |
211
+ | `workspace_id` | str | Workspace/team ID (required) |
212
+ | `base_url` | str | CloudOS base URL (default: `https://cloudos.lifebit.ai`) |
213
+ | `set_default` | bool | Mark this profile as the default |
214
+
215
+ ---
216
+
217
+ ### `profile_list()`
218
+
219
+ Return a `pandas.DataFrame` of all configured profiles.
220
+
221
+ ---
222
+
223
+ ### `cohort_tables(cohort_id, profilename="")`
224
+
225
+ Retrieve schemas, tables, and columns for a cohort.
226
+
227
+ Returns a `CohortTables` object. Print it for a human-readable tree, or
228
+ access `.schemas` for the raw list.
229
+
230
+ ---
231
+
232
+ ### `sql_validate(sql, profilename="")`
233
+
234
+ Validate SQL syntax and references before execution.
235
+
236
+ Returns a `dict` with `isValid` (bool), `tableReferences`, `columnReferences`,
237
+ and on failure an `error` dict with a `message` key.
238
+
239
+ ---
240
+
241
+ ### `query_submit_async(cohort_id, sql, pagination=None, profilename="")`
242
+
243
+ Submit an async SQL task. Returns a `dict` with:
244
+
245
+ | Key | Description |
246
+ |-----|-------------|
247
+ | `task_id` | Use this to poll status and fetch results |
248
+ | `status` | Initial status (typically `"pending"`) |
249
+ | `query` | Echo of the submitted SQL |
250
+ | `type` | Task type string |
251
+ | `sync_execution_timeout` | Server-side timeout hint in ms |
252
+ | `full_response` | Raw API response |
253
+
254
+ `pagination` is an optional `dict` with `pageNumber` (int >= 0) and
255
+ `pageSize` (int >= 1).
256
+
257
+ ---
258
+
259
+ ### `query_status(task_id, profilename="")`
260
+
261
+ Check task status. Returns a `dict` with `task_id`, `status`, `type`,
262
+ `count_of_results`, `query`, `created_at`, `started_at`, `ended_at`,
263
+ `user`, `full_response`.
264
+
265
+ ---
266
+
267
+ ### `query_results(task_id, profilename="")`
268
+
269
+ Fetch results for a completed task. Returns a `pandas.DataFrame` with
270
+ metadata in `.attrs`:
271
+
272
+ | Attribute | Description |
273
+ |-----------|-------------|
274
+ | `total_rows` | Total rows across all pages |
275
+ | `page` | Page index returned |
276
+ | `page_size` | Rows in this page |
277
+ | `total_pages` | Total number of pages available |
278
+
279
+ ---
280
+
281
+ ### `query(cohort_id, sql, poll_interval=2, max_wait=600, page_size=1000, all_pages=True, profilename="")`
282
+
283
+ High-level orchestrator. Submits, polls, and fetches results automatically.
284
+ When `all_pages=True`, submits one async task per page and concatenates them.
285
+
286
+ | Parameter | Default | Description |
287
+ |-----------|---------|-------------|
288
+ | `poll_interval` | 2 | Seconds between status checks (minimum 1) |
289
+ | `max_wait` | 600 | Maximum seconds to wait per task |
290
+ | `page_size` | 1000 | Rows per page |
291
+ | `all_pages` | True | Fetch all pages and combine them |
292
+
293
+ ---
294
+
295
+ ## Using multiple profiles
296
+
297
+ ```python
298
+ # Configure multiple profiles
299
+ cloudos_cb.configure(
300
+ profilename="production",
301
+ apikey="prod-key",
302
+ workspace_id="prod-workspace",
303
+ set_default=True,
304
+ )
305
+ cloudos_cb.configure(
306
+ profilename="staging",
307
+ apikey="stage-key",
308
+ workspace_id="stage-workspace",
309
+ )
310
+
311
+ # Use default profile (production)
312
+ df = cloudos_cb.query(cohort_id="cohort-prod", sql="SELECT 1")
313
+
314
+ # Explicitly use staging profile
315
+ df = cloudos_cb.query(
316
+ cohort_id="cohort-stage",
317
+ sql="SELECT 1",
318
+ profilename="staging",
319
+ )
320
+ ```
321
+
322
+ ## Configuration storage
323
+
324
+ The config file is located at:
325
+ - `$CLOUDOS_CONFIG_DIR/config.json` when the env var is set
326
+ - `~/.cloudos/config.json` otherwise (home directory)
327
+
328
+ File permissions are set to 0600 (user read/write only). The default location
329
+ (`~/.cloudos/`) is outside any repository. If you override `CLOUDOS_CONFIG_DIR`
330
+ to a path inside a project, add that directory to your `.gitignore`.
331
+
332
+ ## Error handling
333
+
334
+ ```python
335
+ from cloudos_cb import (
336
+ CloudOSAuthError,
337
+ CloudOSAccessError,
338
+ CloudOSServerError,
339
+ CloudOSConfigError,
340
+ CloudOSValidationError,
341
+ )
342
+
343
+ try:
344
+ df = cloudos_cb.query(cohort_id="...", sql="SELECT 1")
345
+ except CloudOSAuthError:
346
+ print("Authentication failed - check your API key.")
347
+ except CloudOSAccessError:
348
+ print("Access denied or resource not found.")
349
+ except CloudOSServerError:
350
+ print("Server error - try again later.")
351
+ except CloudOSConfigError:
352
+ print("Profile not configured - run configure() first.")
353
+ except CloudOSValidationError as e:
354
+ print(f"Invalid input: {e}")
355
+ ```
356
+
357
+ ## Logging
358
+
359
+ The package uses Python's standard `logging` module under the `cloudos_cb`
360
+ namespace. To see informational messages:
361
+
362
+ ```python
363
+ import logging
364
+ logging.basicConfig(level=logging.INFO)
365
+ ```
366
+
367
+ ## Running tests
368
+
369
+ ```bash
370
+ pip install -e ".[dev]"
371
+ pytest
372
+ ```
373
+
374
+ To check code style:
375
+
376
+ ```bash
377
+ flake8 cloudos_cb tests
378
+ ```
379
+
380
+ ## Package structure
381
+
382
+ ```
383
+ cloudos-cb-py/
384
+ ├── pyproject.toml # Package metadata and build config
385
+ ├── CHANGELOG.md
386
+ ├── README.md
387
+ ├── LICENSE
388
+ ├── cloudos_cb/ # Package source
389
+ │ ├── __init__.py # Public API
390
+ │ ├── exceptions.py # Custom exception classes
391
+ │ ├── config.py # Profile management
392
+ │ ├── http.py # Authenticated HTTP helpers
393
+ │ ├── utils.py # Shared utilities
394
+ │ └── queries.py # Cohort Browser query functions
395
+ └── tests/
396
+ ├── test_config.py
397
+ ├── test_http.py
398
+ ├── test_utils.py
399
+ └── test_query.py
400
+ ```