API Reference — Data¶
The data layer handles loading, processing, analysis, and exporting of CGM time series.
High-level¶
cgmpy.data.GlucoseData
¶
Refactored main class for modular glucose data management.
This class integrates all specialized modules: - DataLoader: Data loading from different sources - DataProcessor: Data processing and validation - DataAnalyzer: Basic data analysis - DataExporter: Data export to different formats
Source code in cgmpy/data/core.py
23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 | |
Loaders¶
cgmpy.data.loader.DataLoader
¶
Class responsible for loading data from different sources (CSV, Parquet, DataFrame).
Source code in cgmpy/data/loader.py
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 46 47 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 | |
Processors¶
cgmpy.data.processor.DataProcessor
¶
Class responsible for glucose data processing, validation, and cleaning.
Source code in cgmpy/data/processor.py
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 46 47 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 | |
process_data
¶
process_data(data: DataFrame, date_col: str, glucose_col: str, log_performance: bool = False, strict_glucose_range: bool = False) -> tuple[DataFrame, Series]
Processes glucose data in an optimized way.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame with data |
required |
date_col
|
str
|
Name of the date column |
required |
glucose_col
|
str
|
Name of the glucose column |
required |
log_performance
|
bool
|
If True, records performance metrics |
False
|
strict_glucose_range
|
bool
|
If |
False
|
Returns:
| Type | Description |
|---|---|
tuple[DataFrame, Series]
|
Tuple with processed DataFrame and time differences Series |
Raises:
| Type | Description |
|---|---|
GlucoseRangeError
|
Only when |
Source code in cgmpy/data/processor.py
Analyzers¶
cgmpy.data.analyzer.DataAnalyzer
¶
Class responsible for basic analysis of glucose data.
Source code in cgmpy/data/analyzer.py
13 14 15 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 46 47 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 | |
calculate_typical_interval
¶
Calculates the typical interval between measurements in minutes.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
time_diffs
|
Series
|
Series with the time differences |
required |
log_performance
|
bool
|
If True, records performance metrics |
False
|
Returns:
| Type | Description |
|---|---|
float
|
Typical interval in minutes |
Source code in cgmpy/data/analyzer.py
get_data_quality_metrics
¶
get_data_quality_metrics(data: DataFrame, time_diffs: Series, typical_interval: float) -> dict[str, Any]
Calculates data quality metrics.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame with data |
required |
time_diffs
|
Series
|
Series with time differences |
required |
typical_interval
|
float
|
Typical interval between measurements |
required |
Returns:
| Type | Description |
|---|---|
dict[str, Any]
|
Dictionary with quality metrics |
Source code in cgmpy/data/analyzer.py
Exporters¶
cgmpy.data.exporter.DataExporter
¶
Class responsible for exporting glucose data in different formats.
Source code in cgmpy/data/exporter.py
12 13 14 15 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 46 47 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 | |
to_parquet
¶
Saves data in optimized Parquet format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame with data |
required |
file_path
|
str
|
Path where to save the file |
required |
compression
|
str
|
Compression algorithm |
'snappy'
|
sort
|
bool
|
Whether to sort data before saving |
True
|
Source code in cgmpy/data/exporter.py
to_csv
¶
Saves data in CSV format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame with data |
required |
file_path
|
str
|
Path where to save the file |
required |
separator
|
str
|
Field separator |
','
|
include_index
|
bool
|
Whether to include the index |
False
|
Source code in cgmpy/data/exporter.py
to_excel
¶
Saves data in Excel format.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
data
|
DataFrame
|
DataFrame with data |
required |
file_path
|
str
|
Path where to save the file |
required |
sheet_name
|
str
|
Sheet name |
'glucose_data'
|
Source code in cgmpy/data/exporter.py
Device-specific loaders¶
cgmpy.data.specialized.Dexcom
¶
Bases: GlucoseData
Specialized class for Dexcom device data.
This class inherits from GlucoseData and automatically configures the specific column names for files exported from Dexcom Clarity.
Source code in cgmpy/data/specialized.py
__init__
¶
__init__(file_path: str, start_date: str | datetime | None = None, end_date: str | datetime | None = None, log: bool = False)
Initializes Dexcom data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to the exported Clarity CSV file |
required |
start_date
|
str | datetime | None
|
Optional start date filter (YYYY-MM-DD) |
None
|
end_date
|
str | datetime | None
|
Optional end date filter (YYYY-MM-DD) |
None
|
log
|
bool
|
If True, enables detailed performance logs |
False
|
Usage example:
dexcom = Dexcom("dexcom_data.csv") print(dexcom.info())
Source code in cgmpy/data/specialized.py
cgmpy.data.specialized.Libreview
¶
Bases: GlucoseData
Specialized class for Libreview device data.
This class inherits from GlucoseData and automatically configures the specific column names for files exported from Libreview.
Source code in cgmpy/data/specialized.py
__init__
¶
__init__(file_path: str, header: int = 2, start_date: str | datetime | None = None, end_date: str | datetime | None = None, log: bool = False)
Initializes Libreview data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to the exported Libreview CSV file |
required |
header
|
int
|
Header row (usually 2 for Libreview) |
2
|
start_date
|
str | datetime | None
|
Optional start date filter (YYYY-MM-DD) |
None
|
end_date
|
str | datetime | None
|
Optional end date filter (YYYY-MM-DD) |
None
|
log
|
bool
|
If True, enables detailed performance logs |
False
|
Usage example:
libreview = Libreview("libreview_data.csv") print(libreview.info())
Source code in cgmpy/data/specialized.py
cgmpy.data.specialized.MedtronicCarelink
¶
Bases: GlucoseData
Specialized class for Medtronic CareLink device data.
This class inherits from GlucoseData and automatically configures the specific column names for files exported from CareLink.
Source code in cgmpy/data/specialized.py
__init__
¶
__init__(file_path: str, start_date: str | datetime | None = None, end_date: str | datetime | None = None, log: bool = False)
Initializes Medtronic CareLink data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to the exported CareLink CSV file |
required |
start_date
|
str | datetime | None
|
Optional start date filter (YYYY-MM-DD) |
None
|
end_date
|
str | datetime | None
|
Optional end date filter (YYYY-MM-DD) |
None
|
log
|
bool
|
If True, enables detailed performance logs |
False
|
Usage example:
carelink = MedtronicCarelink("carelink_data.csv") print(carelink.info())
Source code in cgmpy/data/specialized.py
cgmpy.data.specialized.TandemDiabetes
¶
Bases: GlucoseData
Specialized class for Tandem Diabetes device data.
This class inherits from GlucoseData and automatically configures the specific column names for files exported from Tandem.
Source code in cgmpy/data/specialized.py
__init__
¶
__init__(file_path: str, start_date: str | datetime | None = None, end_date: str | datetime | None = None, log: bool = False)
Initializes Tandem Diabetes data.
Parameters:
| Name | Type | Description | Default |
|---|---|---|---|
file_path
|
str
|
Path to the exported Tandem CSV file |
required |
start_date
|
str | datetime | None
|
Optional start date filter (YYYY-MM-DD) |
None
|
end_date
|
str | datetime | None
|
Optional end date filter (YYYY-MM-DD) |
None
|
log
|
bool
|
If True, enables detailed performance logs |
False
|
Usage example:
tandem = TandemDiabetes("tandem_data.csv") print(tandem.info())
Source code in cgmpy/data/specialized.py
Exceptions¶
All data-loading errors raise a subclass of cgmpy.errors.DataError, which
inherits from both CGMPyError and ValueError. Catch CGMPyError to
handle any CGMPy-raised error. Common ones:
ColumnNotFoundError— required column missing (has.column,.available)InvalidCSVFormatError— CSV cannot be parsed (has.file_path,.reason)DeviceDetectionError— auto-detect failed (has.file_path,.columns_found)GlucoseRangeError— values outside 39-600 mg/dL (has.n_invalid,.total)EmptyDataError— no rows left after filtering
For the full hierarchy (including MetricError, AgataIntegrationError,
AgataNotInstalledError, ConfigurationError), see
cgmpy.errors
in the source tree.
Pregnancy¶
cgmpy.data.pregnancy_data.PregnancyData
¶
Bases: GlucoseData
Class for managing pregnancy-specific data and dates. Inherits from GlucoseData to integrate base loading and processing, adding trimester segmentation.
Source code in cgmpy/data/pregnancy_data.py
12 13 14 15 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 46 47 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 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 | |
__init__
¶
__init__(data_source: str | DataFrame, delivery_date: str, week: int, day: int = 0, date_col: str = 'time', glucose_col: str = 'glucose', delimiter: str | None = None, header: int = 0, start_date: str | datetime | None = None, end_date: str | datetime | None = None, log: bool = False, target_type: str = 'pregnancy', regularize: bool = False, regularize_interval: int = 5, regularize_max_gap: int = 30)
Initializes pregnancy data.
Source code in cgmpy/data/pregnancy_data.py
calculate_dates
staticmethod
¶
Calculates key pregnancy dates.
Source code in cgmpy/data/pregnancy_data.py
get_trimester_data
¶
Filters the DataFrame for a date range.
decimal_to_weeks_days
staticmethod
¶
Converts decimal weeks to (weeks, days).
get_weeks_days
¶
__str__
¶
Overwrites the string representation to include pregnancy-specific information.
Source code in cgmpy/data/pregnancy_data.py
cgmpy.data.pregnancy_data.PregnancyDataHandler
module-attribute
¶
PregnancyDataHandler = PregnancyData