psr-factory 5.0.0b19__py3-none-win_amd64.whl → 5.0.0b21__py3-none-win_amd64.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.
- psr/execqueue/watcher.py +2 -3
- psr/factory/__init__.py +1 -1
- psr/factory/api.py +454 -411
- psr/factory/factory.dll +0 -0
- psr/factory/factorylib.py +2 -0
- psr/runner/runner.py +7 -1
- {psr_factory-5.0.0b19.dist-info → psr_factory-5.0.0b21.dist-info}/METADATA +1 -1
- {psr_factory-5.0.0b19.dist-info → psr_factory-5.0.0b21.dist-info}/RECORD +11 -11
- {psr_factory-5.0.0b19.dist-info → psr_factory-5.0.0b21.dist-info}/WHEEL +0 -0
- {psr_factory-5.0.0b19.dist-info → psr_factory-5.0.0b21.dist-info}/licenses/LICENSE.txt +0 -0
- {psr_factory-5.0.0b19.dist-info → psr_factory-5.0.0b21.dist-info}/top_level.txt +0 -0
psr/factory/api.py
CHANGED
@@ -31,6 +31,9 @@ polars: Optional[ModuleType] = None
|
|
31
31
|
numpy: Optional[ModuleType] = None
|
32
32
|
|
33
33
|
|
34
|
+
_default_dataframe_type: str = "pandas"
|
35
|
+
|
36
|
+
|
34
37
|
def _has_pandas() -> bool:
|
35
38
|
"""Check if pandas is available."""
|
36
39
|
global _HAS_PANDAS
|
@@ -68,6 +71,24 @@ def _has_polars() -> bool:
|
|
68
71
|
return _HAS_POLARS
|
69
72
|
|
70
73
|
|
74
|
+
def set_default_dataframe_type(df_type: str):
|
75
|
+
"""Set the default dataframe type to be used by the library."""
|
76
|
+
global _default_dataframe_type
|
77
|
+
df_type = df_type.lower()
|
78
|
+
if df_type not in ["pandas", "polars", "factory"]:
|
79
|
+
raise ValueError("Unsupported dataframe type. Supported types are 'pandas' and 'polars'.")
|
80
|
+
if df_type == "pandas" and not _has_pandas():
|
81
|
+
raise ValueError("Pandas is not installed. Please install it to use this dataframe type.")
|
82
|
+
if df_type == "polars" and not _has_polars():
|
83
|
+
raise ValueError("Polars is not installed. Please install it to use this dataframe type.")
|
84
|
+
_default_dataframe_type = df_type
|
85
|
+
|
86
|
+
|
87
|
+
def get_default_dataframe_type() -> str:
|
88
|
+
"""Get the default dataframe type used by the library."""
|
89
|
+
return _default_dataframe_type
|
90
|
+
|
91
|
+
|
71
92
|
# States whether the library is initialized.
|
72
93
|
# It should be to correctly load and build models.
|
73
94
|
_initialized = False
|
@@ -119,6 +140,9 @@ def _bytes(value: str) -> int:
|
|
119
140
|
class FactoryException(Exception):
|
120
141
|
pass
|
121
142
|
|
143
|
+
class FactoryLicenseError(Exception):
|
144
|
+
pass
|
145
|
+
|
122
146
|
|
123
147
|
class LogLevel(enum.Enum):
|
124
148
|
NOTSET = 0
|
@@ -173,6 +197,18 @@ def build_version() -> str:
|
|
173
197
|
return _version_short()
|
174
198
|
|
175
199
|
|
200
|
+
def check_license() -> Tuple[bool, str]:
|
201
|
+
"""Returns True if license is valid and active."""
|
202
|
+
_check_loaded()
|
203
|
+
error = Error()
|
204
|
+
factorylib.lib.psrd_check_license(error.handler())
|
205
|
+
valid = error.code == 0
|
206
|
+
invalid = error.code == 15
|
207
|
+
if not valid and not invalid:
|
208
|
+
raise FactoryException("Error checking license: " + error.what)
|
209
|
+
return valid, error.what
|
210
|
+
|
211
|
+
|
176
212
|
def get_log_level() -> LogLevel:
|
177
213
|
"""Get log level."""
|
178
214
|
_check_loaded()
|
@@ -292,7 +328,7 @@ def _get_context(models_or_context: Union[str, list, dict, "Context", None],
|
|
292
328
|
elif isinstance(models_or_context, str):
|
293
329
|
context["Models"] = [models_or_context, ]
|
294
330
|
else:
|
295
|
-
raise TypeError("Unexpected type for
|
331
|
+
raise TypeError("Unexpected type for model_or_context argument.")
|
296
332
|
if blocks is not None and isinstance(blocks, int):
|
297
333
|
if isinstance(blocks, Context):
|
298
334
|
context.set("Blocks", blocks)
|
@@ -1070,7 +1106,7 @@ class DataObject(_BaseObject):
|
|
1070
1106
|
if error.code != 0:
|
1071
1107
|
raise FactoryException(error.what)
|
1072
1108
|
|
1073
|
-
def get_df(self, expression: str) -> "pandas.DataFrame":
|
1109
|
+
def get_df(self, expression: str) -> Union["pandas.DataFrame", "polars.DataFrame", "DataFrame"]:
|
1074
1110
|
_err = Error()
|
1075
1111
|
_df = DataFrame()
|
1076
1112
|
factorylib.lib.psrd_object_get_table(self._hdr, _df.handler(),
|
@@ -1081,7 +1117,7 @@ class DataObject(_BaseObject):
|
|
1081
1117
|
raise FactoryException(_err.what)
|
1082
1118
|
df_builder = _DataFrameBuilder()
|
1083
1119
|
df_builder.build_dataframe(_df)
|
1084
|
-
return df_builder.
|
1120
|
+
return df_builder.build_desired_dataframe_type()
|
1085
1121
|
|
1086
1122
|
def set(self, expression: str, value):
|
1087
1123
|
_err = Error()
|
@@ -1333,11 +1369,11 @@ class Study(_BaseObject):
|
|
1333
1369
|
return copy.deepcopy(self)
|
1334
1370
|
|
1335
1371
|
@staticmethod
|
1336
|
-
def create_object(
|
1372
|
+
def create_object(model_or_context: Union[str, Context, dict, None],
|
1337
1373
|
blocks: Optional[int] = None):
|
1338
1374
|
_check_initialized()
|
1339
1375
|
err = Error()
|
1340
|
-
context = _get_context(
|
1376
|
+
context = _get_context(model_or_context, blocks)
|
1341
1377
|
study = Study()
|
1342
1378
|
study._hdr = factorylib.lib.psrd_study_create(context.handler(),
|
1343
1379
|
err.handler())
|
@@ -1346,15 +1382,15 @@ class Study(_BaseObject):
|
|
1346
1382
|
return study
|
1347
1383
|
|
1348
1384
|
@staticmethod
|
1349
|
-
def load(study_path: Union[str, pathlib.Path],
|
1385
|
+
def load(study_path: Union[str, pathlib.Path], model_or_context: Union[str, Context, None],
|
1350
1386
|
settings_only: bool = False,
|
1351
1387
|
options: Optional[Union[dict, "Value", "DataObject"]] = None):
|
1352
1388
|
if not isinstance(options, (DataObject, type(None))):
|
1353
1389
|
raise TypeError("options must be a DataObject or None.")
|
1354
1390
|
_check_initialized()
|
1355
1391
|
study_path = str(study_path)
|
1356
|
-
context = _get_context(
|
1357
|
-
|
1392
|
+
context = _get_context(model_or_context, None)
|
1393
|
+
error = Error()
|
1358
1394
|
options_value = _get_arg_object(options)
|
1359
1395
|
study = Study()
|
1360
1396
|
if not settings_only:
|
@@ -1363,73 +1399,73 @@ class Study(_BaseObject):
|
|
1363
1399
|
load_fn = factorylib.lib.psrd_study_load_settings
|
1364
1400
|
study._hdr = load_fn(_c_str(study_path), _bytes(study_path),
|
1365
1401
|
options_value.handler(), context.handler(),
|
1366
|
-
|
1367
|
-
if
|
1368
|
-
raise FactoryException(
|
1402
|
+
error.handler())
|
1403
|
+
if error.code != 0:
|
1404
|
+
raise FactoryException(error.what)
|
1369
1405
|
return study
|
1370
1406
|
|
1371
1407
|
def save(self, output_path: Union[str, pathlib.Path],
|
1372
1408
|
options: Optional[Union[dict, Value, DataObject]] = None):
|
1373
1409
|
output_path = str(output_path)
|
1374
|
-
|
1410
|
+
error = Error()
|
1375
1411
|
options_value = _get_arg_object(options)
|
1376
1412
|
factorylib.lib.psrd_study_save(self._hdr,
|
1377
1413
|
_c_str(output_path), _bytes(output_path),
|
1378
1414
|
options_value.handler(),
|
1379
|
-
|
1380
|
-
if
|
1381
|
-
raise FactoryException(
|
1415
|
+
error.handler())
|
1416
|
+
if error.code != 0:
|
1417
|
+
raise FactoryException(error.what)
|
1382
1418
|
|
1383
1419
|
def save_settings(self, output_path: Union[str, pathlib.Path],
|
1384
1420
|
options: Optional[Union[dict, Value, DataObject]] = None):
|
1385
1421
|
output_path = str(output_path)
|
1386
|
-
|
1422
|
+
error = Error()
|
1387
1423
|
options_value = _get_arg_object(options)
|
1388
1424
|
factorylib.lib.psrd_study_save_settings(self._hdr, _c_str(output_path),
|
1389
1425
|
_bytes(output_path),
|
1390
1426
|
options_value.handler(),
|
1391
|
-
|
1392
|
-
if
|
1393
|
-
raise FactoryException(
|
1427
|
+
error.handler())
|
1428
|
+
if error.code != 0:
|
1429
|
+
raise FactoryException(error.what)
|
1394
1430
|
|
1395
1431
|
@property
|
1396
1432
|
def context(self) -> "Context":
|
1397
1433
|
_check_initialized()
|
1398
1434
|
obj = Context()
|
1399
|
-
|
1435
|
+
error = Error()
|
1400
1436
|
ref = factorylib.lib.psrd_study_context(self._hdr,
|
1401
|
-
|
1402
|
-
if
|
1403
|
-
raise FactoryException(
|
1437
|
+
error.handler())
|
1438
|
+
if error.code != 0 or ref is None:
|
1439
|
+
raise FactoryException(error.what)
|
1404
1440
|
obj._hdr = ref
|
1405
1441
|
return obj
|
1406
1442
|
|
1407
1443
|
def get(self, expression: str) -> Union[int, float, dt.datetime, str, "DataObject", list, None]:
|
1408
1444
|
value = Value()
|
1409
|
-
|
1445
|
+
error = Error()
|
1410
1446
|
factorylib.lib.psrd_study_get_value(self._hdr,
|
1411
1447
|
_c_str(expression),
|
1412
1448
|
value.handler(),
|
1413
|
-
|
1414
|
-
if
|
1415
|
-
raise FactoryException(
|
1449
|
+
error.handler())
|
1450
|
+
if error.code != 0:
|
1451
|
+
raise FactoryException(error.what)
|
1416
1452
|
return value.get()
|
1417
1453
|
|
1418
1454
|
def get_at(self, expression: str, range_expr: Union[str, dt.datetime]) -> Union[int, float, dt.datetime, str, "DataObject", list, None]:
|
1419
1455
|
if not isinstance(range_expr, str):
|
1420
1456
|
raise FactoryException("range_expr must be a string or datetime object.")
|
1421
|
-
|
1422
|
-
|
1423
|
-
|
1424
|
-
|
1457
|
+
value = Value()
|
1458
|
+
error = Error()
|
1459
|
+
range_value = Value()
|
1460
|
+
range_value.set(range_expr)
|
1425
1461
|
factorylib.lib.psrd_study_get_value_at(self._hdr,
|
1426
1462
|
_c_str(expression),
|
1427
|
-
|
1428
|
-
|
1429
|
-
|
1430
|
-
if
|
1431
|
-
raise FactoryException(
|
1432
|
-
return
|
1463
|
+
range_value.handler(),
|
1464
|
+
value.handler(),
|
1465
|
+
error.handler())
|
1466
|
+
if error.code != 0:
|
1467
|
+
raise FactoryException(error.what)
|
1468
|
+
return value.get()
|
1433
1469
|
|
1434
1470
|
def as_dict(self) -> Dict[str, any]:
|
1435
1471
|
value_dict = ValueDict()
|
@@ -1452,22 +1488,22 @@ class Study(_BaseObject):
|
|
1452
1488
|
def add(self, obj: DataObject):
|
1453
1489
|
if not isinstance(obj, DataObject):
|
1454
1490
|
raise TypeError("obj must be a DataObject.")
|
1455
|
-
|
1491
|
+
error = Error()
|
1456
1492
|
factorylib.lib.psrd_study_add(self._hdr,
|
1457
1493
|
obj.handler(),
|
1458
|
-
|
1459
|
-
if
|
1460
|
-
raise FactoryException(
|
1494
|
+
error.handler())
|
1495
|
+
if error.code != 0:
|
1496
|
+
raise FactoryException(error.what)
|
1461
1497
|
|
1462
1498
|
def remove(self, obj: DataObject):
|
1463
1499
|
if not isinstance(obj, DataObject):
|
1464
1500
|
raise TypeError("obj must be a DataObject.")
|
1465
|
-
|
1501
|
+
error = Error()
|
1466
1502
|
factorylib.lib.psrd_study_remove(self._hdr,
|
1467
1503
|
obj.handler(),
|
1468
|
-
|
1469
|
-
if
|
1470
|
-
raise FactoryException(
|
1504
|
+
error.handler())
|
1505
|
+
if error.code != 0:
|
1506
|
+
raise FactoryException(error.what)
|
1471
1507
|
|
1472
1508
|
def get_all_objects(self) -> List[DataObject]:
|
1473
1509
|
object_list = ValueList(False)
|
@@ -1481,159 +1517,156 @@ class Study(_BaseObject):
|
|
1481
1517
|
|
1482
1518
|
def find(self, expression: str) -> List[DataObject]:
|
1483
1519
|
object_list = ValueList(False)
|
1484
|
-
|
1485
|
-
|
1520
|
+
error = Error()
|
1521
|
+
handler = factorylib.lib.psrd_study_find(self._hdr,
|
1486
1522
|
_c_str(expression),
|
1487
|
-
|
1488
|
-
if
|
1489
|
-
raise FactoryException(
|
1490
|
-
object_list._hdr =
|
1523
|
+
error.handler())
|
1524
|
+
if error.code != 0 or handler is None:
|
1525
|
+
raise FactoryException(error.what)
|
1526
|
+
object_list._hdr = handler
|
1491
1527
|
return object_list.to_list()
|
1492
1528
|
|
1493
|
-
def find_by_name(self, type_name: str,
|
1494
|
-
if
|
1495
|
-
|
1496
|
-
|
1497
|
-
|
1498
|
-
|
1499
|
-
type_name, name_str = type_name.split(".", 1)
|
1500
|
-
name = name_str
|
1529
|
+
def find_by_name(self, type_name: str, name_or_pattern: str) -> List[DataObject]:
|
1530
|
+
if name_or_pattern is None or name_or_pattern == "":
|
1531
|
+
raise DeprecationWarning("Starting from Factory 4.0.28 "
|
1532
|
+
"the second argument 'name_or_pattern' must be provided.\n"
|
1533
|
+
"Use find_by_name(type_name, name_or_pattern)")
|
1501
1534
|
object_list = ValueList(False)
|
1502
|
-
|
1503
|
-
|
1504
|
-
|
1505
|
-
|
1506
|
-
|
1507
|
-
|
1508
|
-
|
1509
|
-
|
1510
|
-
raise FactoryException(_err.what)
|
1511
|
-
object_list._hdr = ref
|
1535
|
+
error = Error()
|
1536
|
+
expression = f"{type_name}.{name_or_pattern}"
|
1537
|
+
handler = factorylib.lib.psrd_study_find(self._hdr,
|
1538
|
+
_c_str(expression),
|
1539
|
+
error.handler())
|
1540
|
+
if error.code != 0 or handler is None:
|
1541
|
+
raise FactoryException(error.what)
|
1542
|
+
object_list._hdr = handler
|
1512
1543
|
return object_list.to_list()
|
1513
1544
|
|
1514
|
-
def find_by_code(self, type_name: str, code:
|
1545
|
+
def find_by_code(self, type_name: str, code: int) -> List[DataObject]:
|
1515
1546
|
if code is None:
|
1516
|
-
|
1517
|
-
|
1518
|
-
|
1519
|
-
|
1520
|
-
type_name, code_str = type_name.split(".", 1)
|
1521
|
-
code = int(code_str)
|
1547
|
+
raise DeprecationWarning("Starting from Factory 4.0.9 "
|
1548
|
+
"the second argument 'code' must be provided.\n"
|
1549
|
+
"Use find_by_code(type_name, code)")
|
1522
1550
|
|
1523
1551
|
object_list = ValueList(False)
|
1524
|
-
|
1525
|
-
|
1526
|
-
|
1527
|
-
|
1528
|
-
|
1529
|
-
if
|
1530
|
-
raise FactoryException(
|
1531
|
-
object_list._hdr =
|
1552
|
+
error = Error()
|
1553
|
+
handler = factorylib.lib.psrd_study_find_by_code(self._hdr,
|
1554
|
+
_c_str(type_name),
|
1555
|
+
code,
|
1556
|
+
error.handler())
|
1557
|
+
if error.code != 0 or handler is None:
|
1558
|
+
raise FactoryException(error.what)
|
1559
|
+
object_list._hdr = handler
|
1532
1560
|
return object_list.to_list()
|
1533
1561
|
|
1534
|
-
def find_by_id(self,
|
1562
|
+
def find_by_id(self, type_name: str, id_or_pattern: str) -> List[DataObject]:
|
1563
|
+
if id_or_pattern is None or id_or_pattern == "":
|
1564
|
+
raise DeprecationWarning("Starting from Factory 5.0.0 "
|
1565
|
+
"the second argument 'id' must be provided.\n"
|
1566
|
+
"Use find_by_id(type_name, id)")
|
1567
|
+
expression = f"{type_name}.{id_or_pattern}"
|
1535
1568
|
object_list = ValueList(False)
|
1536
|
-
|
1569
|
+
error = Error()
|
1537
1570
|
ref = factorylib.lib.psrd_study_find_by_id(self._hdr,
|
1538
1571
|
_c_str(expression),
|
1539
|
-
|
1540
|
-
if
|
1541
|
-
raise FactoryException(
|
1572
|
+
error.handler())
|
1573
|
+
if error.code != 0 or ref is None:
|
1574
|
+
raise FactoryException(error.what)
|
1542
1575
|
object_list._hdr = ref
|
1543
1576
|
return object_list.to_list()
|
1544
1577
|
|
1545
|
-
def set(self, expression: str, value):
|
1546
|
-
|
1547
|
-
|
1548
|
-
|
1578
|
+
def set(self, expression: str, value: any):
|
1579
|
+
error = Error()
|
1580
|
+
value_object = Value()
|
1581
|
+
value_object.set(value)
|
1549
1582
|
factorylib.lib.psrd_study_set_value(self._hdr,
|
1550
1583
|
_c_str(expression),
|
1551
1584
|
_bytes(expression),
|
1552
|
-
|
1553
|
-
|
1554
|
-
if
|
1555
|
-
raise FactoryException(
|
1585
|
+
value_object.handler(),
|
1586
|
+
error.handler())
|
1587
|
+
if error.code != 0:
|
1588
|
+
raise FactoryException(error.what)
|
1556
1589
|
|
1557
|
-
def set_at(self, expression: str, range_expr: Union[str, dt.datetime], value):
|
1590
|
+
def set_at(self, expression: str, range_expr: Union[str, dt.datetime], value: any):
|
1558
1591
|
if not isinstance(range_expr, (str, dt.datetime)):
|
1559
1592
|
raise FactoryException("range_expr must be a string or datetime object.")
|
1560
|
-
|
1561
|
-
|
1562
|
-
|
1563
|
-
|
1564
|
-
|
1593
|
+
error = Error()
|
1594
|
+
value_object = Value()
|
1595
|
+
value_object.set(value)
|
1596
|
+
range_value = Value()
|
1597
|
+
range_value.set(range_expr)
|
1565
1598
|
factorylib.lib.psrd_study_set_value_at(self._hdr,
|
1566
1599
|
_c_str(expression),
|
1567
1600
|
_bytes(expression),
|
1568
|
-
|
1569
|
-
|
1570
|
-
|
1571
|
-
if
|
1572
|
-
raise FactoryException(
|
1601
|
+
range_value.handler(),
|
1602
|
+
value_object.handler(),
|
1603
|
+
error.handler())
|
1604
|
+
if error.code != 0:
|
1605
|
+
raise FactoryException(error.what)
|
1573
1606
|
|
1574
|
-
def get_df(self, expression: str) -> "pandas.DataFrame":
|
1575
|
-
|
1576
|
-
|
1577
|
-
factorylib.lib.psrd_study_get_table(self._hdr,
|
1607
|
+
def get_df(self, expression: str) -> Union["pandas.DataFrame", "polars.DataFrame", "DataFrame"]:
|
1608
|
+
error = Error()
|
1609
|
+
df = DataFrame()
|
1610
|
+
factorylib.lib.psrd_study_get_table(self._hdr, df.handler(),
|
1578
1611
|
_c_str(expression),
|
1579
1612
|
_bytes(expression),
|
1580
|
-
|
1581
|
-
if
|
1582
|
-
raise FactoryException(
|
1613
|
+
error.handler())
|
1614
|
+
if error.code != 0:
|
1615
|
+
raise FactoryException(error.what)
|
1583
1616
|
df_builder = _DataFrameBuilder()
|
1584
|
-
df_builder.build_dataframe(
|
1585
|
-
return df_builder.
|
1617
|
+
df_builder.build_dataframe(df)
|
1618
|
+
return df_builder.build_desired_dataframe_type()
|
1586
1619
|
|
1587
|
-
def get_objects_values(self, object_type: str, columns: List[str]) -> "pandas.DataFrame":
|
1588
|
-
|
1589
|
-
|
1620
|
+
def get_objects_values(self, object_type: str, columns: List[str]) -> Union["pandas.DataFrame", "polars.DataFrame", "DataFrame"]:
|
1621
|
+
error = Error()
|
1622
|
+
df = DataFrame()
|
1590
1623
|
columns_list = Value()
|
1591
1624
|
columns_list.set(columns)
|
1592
|
-
factorylib.lib.psrd_study_get_objects_values(self._hdr,
|
1625
|
+
factorylib.lib.psrd_study_get_objects_values(self._hdr, df.handler(),
|
1593
1626
|
_c_str(object_type),
|
1594
1627
|
columns_list.handler(),
|
1595
|
-
|
1596
|
-
if
|
1597
|
-
raise FactoryException(
|
1628
|
+
error.handler())
|
1629
|
+
if error.code != 0:
|
1630
|
+
raise FactoryException(error.what)
|
1598
1631
|
df_builder = _DataFrameBuilder()
|
1599
|
-
df_builder.build_dataframe(
|
1600
|
-
return df_builder.
|
1632
|
+
df_builder.build_dataframe(df)
|
1633
|
+
return df_builder.build_desired_dataframe_type()
|
1601
1634
|
|
1602
|
-
def get_objects_values_at(self, object_type: str, columns: List[str],
|
1603
|
-
|
1604
|
-
|
1605
|
-
|
1606
|
-
|
1635
|
+
def get_objects_values_at(self, object_type: str, columns: List[str], range_value: Union[str, dt.datetime]) -> Union["pandas.DataFrame", "polars.DataFrame", "DataFrame"]:
|
1636
|
+
error = Error()
|
1637
|
+
df = DataFrame()
|
1638
|
+
range_object = Value()
|
1639
|
+
range_object.set(range_value)
|
1607
1640
|
columns_list = Value()
|
1608
1641
|
columns_list.set(columns)
|
1609
|
-
factorylib.lib.psrd_study_get_objects_values_at(self._hdr,
|
1642
|
+
factorylib.lib.psrd_study_get_objects_values_at(self._hdr, df.handler(),
|
1610
1643
|
_c_str(object_type),
|
1611
1644
|
columns_list.handler(),
|
1612
|
-
|
1613
|
-
|
1614
|
-
if
|
1615
|
-
raise FactoryException(
|
1645
|
+
range_object.handler(),
|
1646
|
+
error.handler())
|
1647
|
+
if error.code != 0:
|
1648
|
+
raise FactoryException(error.what)
|
1616
1649
|
df_builder = _DataFrameBuilder()
|
1617
|
-
df_builder.build_dataframe(
|
1618
|
-
return df_builder.
|
1650
|
+
df_builder.build_dataframe(df)
|
1651
|
+
return df_builder.build_desired_dataframe_type()
|
1619
1652
|
|
1620
1653
|
def properties(self) -> Dict[str, PropertyDescription]:
|
1621
|
-
|
1654
|
+
error = Error()
|
1622
1655
|
value = ctypes.c_long()
|
1623
1656
|
factorylib.lib.psrd_study_property_description_count(self._hdr,
|
1624
1657
|
ctypes.byref(value),
|
1625
|
-
|
1626
|
-
if
|
1627
|
-
raise FactoryException(
|
1658
|
+
error.handler())
|
1659
|
+
if error.code != 0:
|
1660
|
+
raise FactoryException(error.what)
|
1628
1661
|
var_count = int(value.value)
|
1629
1662
|
properties = {}
|
1630
1663
|
for i_var in range(var_count):
|
1631
1664
|
var = PropertyDescription()
|
1632
1665
|
ref = factorylib.lib.psrd_study_get_property_description(self._hdr,
|
1633
1666
|
i_var,
|
1634
|
-
|
1635
|
-
if
|
1636
|
-
raise FactoryException(
|
1667
|
+
error.handler())
|
1668
|
+
if error.code != 0 or ref is None:
|
1669
|
+
raise FactoryException(error.what)
|
1637
1670
|
var._hdr = ref
|
1638
1671
|
properties[var.name] = var
|
1639
1672
|
return properties
|
@@ -1643,21 +1676,21 @@ class Study(_BaseObject):
|
|
1643
1676
|
raise ModuleNotFoundError("pandas required.")
|
1644
1677
|
dataframe_like = pandas.api.interchange.from_dataframe(dataframe_like)
|
1645
1678
|
df_builder = _DataFrameBuilder()
|
1646
|
-
|
1647
|
-
|
1648
|
-
factorylib.lib.psrd_study_set_table(self._hdr,
|
1649
|
-
|
1650
|
-
if
|
1651
|
-
raise FactoryException(
|
1679
|
+
df = df_builder.build_from_pandas(dataframe_like)
|
1680
|
+
error = Error()
|
1681
|
+
factorylib.lib.psrd_study_set_table(self._hdr, df.handler(),
|
1682
|
+
error.handler())
|
1683
|
+
if error.code != 0:
|
1684
|
+
raise FactoryException(error.what)
|
1652
1685
|
|
1653
1686
|
def clear_values(self, expression: str):
|
1654
|
-
|
1687
|
+
error = Error()
|
1655
1688
|
factorylib.lib.psrd_study_clear_values(self._hdr,
|
1656
1689
|
_c_str(expression),
|
1657
1690
|
_bytes(expression),
|
1658
|
-
|
1659
|
-
if
|
1660
|
-
raise FactoryException(
|
1691
|
+
error.handler())
|
1692
|
+
if error.code != 0:
|
1693
|
+
raise FactoryException(error.what)
|
1661
1694
|
|
1662
1695
|
|
1663
1696
|
def _is_int64(value):
|
@@ -1670,6 +1703,40 @@ def _is_float64(value):
|
|
1670
1703
|
return isinstance(value, (numpy.float64, float)) or (isinstance(value, numpy.ndarray) and value.dtype == numpy.float64)
|
1671
1704
|
|
1672
1705
|
|
1706
|
+
def _pandas_dtype_to_column_type(dtype: str) -> int:
|
1707
|
+
if dtype == "object":
|
1708
|
+
return 0
|
1709
|
+
elif dtype == "int32":
|
1710
|
+
return 1
|
1711
|
+
elif dtype == "int64":
|
1712
|
+
return 2
|
1713
|
+
elif dtype == "float32":
|
1714
|
+
return 3
|
1715
|
+
elif dtype == "float64":
|
1716
|
+
return 4
|
1717
|
+
elif dtype == "string":
|
1718
|
+
return 5
|
1719
|
+
elif dtype == "datetime64[ns]":
|
1720
|
+
return 6
|
1721
|
+
else:
|
1722
|
+
raise FactoryException(f"Unsupported pandas dtype \"{dtype}\".")
|
1723
|
+
|
1724
|
+
|
1725
|
+
def _polars_dtype_to_column_type(dtype: "polars.datatypes.classes.DataTypeClass") -> int:
|
1726
|
+
if dtype == polars.Int32:
|
1727
|
+
return 1
|
1728
|
+
if dtype == polars.Int64:
|
1729
|
+
return 2
|
1730
|
+
if dtype == polars.Float64:
|
1731
|
+
return 3
|
1732
|
+
if dtype == polars.Float64:
|
1733
|
+
return 4
|
1734
|
+
if dtype == polars.String:
|
1735
|
+
return 5
|
1736
|
+
else:
|
1737
|
+
raise FactoryException(f"Unsupported polars dtype \"{dtype}\".")
|
1738
|
+
|
1739
|
+
|
1673
1740
|
class _DataFrameBuilder:
|
1674
1741
|
def __init__(self):
|
1675
1742
|
self.indices: List[Optional[_TableColumn]] = []
|
@@ -1680,67 +1747,34 @@ class _DataFrameBuilder:
|
|
1680
1747
|
self.index_types: List[int] = []
|
1681
1748
|
self._not_built = True
|
1682
1749
|
self._value: Optional[Value] = None
|
1683
|
-
self.
|
1684
|
-
|
1685
|
-
def _pandas_dtype_to_column_type(self, dtype: str) -> int:
|
1686
|
-
if dtype == "object":
|
1687
|
-
return 0
|
1688
|
-
elif dtype == "int32":
|
1689
|
-
return 1
|
1690
|
-
elif dtype == "int64":
|
1691
|
-
return 2
|
1692
|
-
elif dtype == "float32":
|
1693
|
-
return 3
|
1694
|
-
elif dtype == "float64":
|
1695
|
-
return 4
|
1696
|
-
elif dtype == "string":
|
1697
|
-
return 5
|
1698
|
-
elif dtype == "datetime64[ns]":
|
1699
|
-
return 6
|
1700
|
-
else:
|
1701
|
-
raise FactoryException(f"Unsupported pandas dtype \"{dtype}\".")
|
1702
|
-
|
1703
|
-
def _polars_dtype_to_column_type(self, dtype: "polars.datatypes.classes.DataTypeClass") -> int:
|
1704
|
-
if dtype == polars.Int32:
|
1705
|
-
return 1
|
1706
|
-
if dtype == polars.Int64:
|
1707
|
-
return 2
|
1708
|
-
if dtype == polars.Float64:
|
1709
|
-
return 3
|
1710
|
-
if dtype == polars.Float64:
|
1711
|
-
return 4
|
1712
|
-
if dtype == polars.String:
|
1713
|
-
return 5
|
1714
|
-
else:
|
1715
|
-
raise FactoryException(f"Unsupported polars dtype \"{dtype}\".")
|
1716
|
-
|
1750
|
+
self._error: Optional[Error] = None
|
1717
1751
|
|
1718
1752
|
def build_dataframe(self, df: "DataFrame"):
|
1719
|
-
self.
|
1753
|
+
self._error = Error()
|
1720
1754
|
name_buffer_length = 200
|
1721
|
-
|
1755
|
+
columns_count = ctypes.c_long()
|
1722
1756
|
factorylib.lib.psrd_table_columns_count(df.handler(),
|
1723
|
-
ctypes.byref(
|
1724
|
-
self.
|
1725
|
-
if self.
|
1726
|
-
raise FactoryException(self.
|
1727
|
-
columns_count =
|
1757
|
+
ctypes.byref(columns_count),
|
1758
|
+
self._error.handler())
|
1759
|
+
if self._error.code != 0:
|
1760
|
+
raise FactoryException(self._error.what)
|
1761
|
+
columns_count = columns_count.value
|
1728
1762
|
|
1729
|
-
|
1763
|
+
rows_count = ctypes.c_long()
|
1730
1764
|
factorylib.lib.psrd_table_rows_count(df.handler(),
|
1731
|
-
ctypes.byref(
|
1732
|
-
self.
|
1733
|
-
if self.
|
1734
|
-
raise FactoryException(self.
|
1735
|
-
rows_count =
|
1765
|
+
ctypes.byref(rows_count),
|
1766
|
+
self._error.handler())
|
1767
|
+
if self._error.code != 0:
|
1768
|
+
raise FactoryException(self._error.what)
|
1769
|
+
rows_count = rows_count.value
|
1736
1770
|
|
1737
|
-
|
1771
|
+
indices_count = ctypes.c_long()
|
1738
1772
|
factorylib.lib.psrd_table_index_count(df.handler(),
|
1739
|
-
ctypes.byref(
|
1740
|
-
self.
|
1741
|
-
if self.
|
1742
|
-
raise FactoryException(self.
|
1743
|
-
indices_count =
|
1773
|
+
ctypes.byref(indices_count),
|
1774
|
+
self._error.handler())
|
1775
|
+
if self._error.code != 0:
|
1776
|
+
raise FactoryException(self._error.what)
|
1777
|
+
indices_count = indices_count.value
|
1744
1778
|
|
1745
1779
|
value = Value()
|
1746
1780
|
buffer = ctypes.create_string_buffer(name_buffer_length)
|
@@ -1749,27 +1783,27 @@ class _DataFrameBuilder:
|
|
1749
1783
|
for index in range(indices_count):
|
1750
1784
|
factorylib.lib.psrd_table_index_get_name(df.handler(), index,
|
1751
1785
|
buffer, name_buffer_length,
|
1752
|
-
self.
|
1753
|
-
if self.
|
1754
|
-
raise FactoryException(self.
|
1786
|
+
self._error.handler())
|
1787
|
+
if self._error.code != 0:
|
1788
|
+
raise FactoryException(self._error.what)
|
1755
1789
|
self.indices[index].name = _from_c_str(buffer.value)
|
1756
1790
|
|
1757
1791
|
self.indices[index].values = [None] * rows_count
|
1758
1792
|
for i_row in range(0, rows_count):
|
1759
1793
|
factorylib.lib.psrd_table_index_get_value(df.handler(), index,
|
1760
1794
|
i_row, value.handler(),
|
1761
|
-
self.
|
1762
|
-
if self.
|
1763
|
-
raise FactoryException(self.
|
1795
|
+
self._error.handler())
|
1796
|
+
if self._error.code != 0:
|
1797
|
+
raise FactoryException(self._error.what)
|
1764
1798
|
self.indices[index].values[i_row] = value.get()
|
1765
1799
|
|
1766
1800
|
self.columns = [_TableColumn() for _ in range(columns_count)]
|
1767
1801
|
for column in range(columns_count):
|
1768
1802
|
factorylib.lib.psrd_table_column_get_name(df.handler(), column,
|
1769
1803
|
buffer, name_buffer_length,
|
1770
|
-
self.
|
1771
|
-
if self.
|
1772
|
-
raise FactoryException(self.
|
1804
|
+
self._error.handler())
|
1805
|
+
if self._error.code != 0:
|
1806
|
+
raise FactoryException(self._error.what)
|
1773
1807
|
self.columns[column].name = _from_c_str(buffer.value)
|
1774
1808
|
|
1775
1809
|
self.columns[column].values = [None] * rows_count
|
@@ -1777,38 +1811,38 @@ class _DataFrameBuilder:
|
|
1777
1811
|
factorylib.lib.psrd_table_column_get_value(df.handler(),
|
1778
1812
|
column, row,
|
1779
1813
|
value.handler(),
|
1780
|
-
self.
|
1781
|
-
if self.
|
1782
|
-
raise FactoryException(self.
|
1814
|
+
self._error.handler())
|
1815
|
+
if self._error.code != 0:
|
1816
|
+
raise FactoryException(self._error.what)
|
1783
1817
|
self.columns[column].values[row] = value.get()
|
1784
1818
|
self._not_built = False
|
1785
1819
|
|
1786
1820
|
def build_dataframe_of_integral_types(self, df: "DataFrame"):
|
1787
|
-
self.
|
1821
|
+
self._error = Error()
|
1788
1822
|
name_buffer_length = 200
|
1789
|
-
|
1823
|
+
columns_count = ctypes.c_long()
|
1790
1824
|
factorylib.lib.psrd_table_columns_count(df.handler(),
|
1791
|
-
ctypes.byref(
|
1792
|
-
self.
|
1793
|
-
if self.
|
1794
|
-
raise FactoryException(self.
|
1795
|
-
columns_count =
|
1825
|
+
ctypes.byref(columns_count),
|
1826
|
+
self._error.handler())
|
1827
|
+
if self._error.code != 0:
|
1828
|
+
raise FactoryException(self._error.what)
|
1829
|
+
columns_count = columns_count.value
|
1796
1830
|
|
1797
|
-
|
1831
|
+
rows_count = ctypes.c_long()
|
1798
1832
|
factorylib.lib.psrd_table_rows_count(df.handler(),
|
1799
|
-
ctypes.byref(
|
1800
|
-
self.
|
1801
|
-
if self.
|
1802
|
-
raise FactoryException(self.
|
1803
|
-
rows_count =
|
1833
|
+
ctypes.byref(rows_count),
|
1834
|
+
self._error.handler())
|
1835
|
+
if self._error.code != 0:
|
1836
|
+
raise FactoryException(self._error.what)
|
1837
|
+
rows_count = rows_count.value
|
1804
1838
|
|
1805
|
-
|
1839
|
+
indices_count = ctypes.c_long()
|
1806
1840
|
factorylib.lib.psrd_table_index_count(df.handler(),
|
1807
|
-
ctypes.byref(
|
1808
|
-
self.
|
1809
|
-
if self.
|
1810
|
-
raise FactoryException(self.
|
1811
|
-
indices_count =
|
1841
|
+
ctypes.byref(indices_count),
|
1842
|
+
self._error.handler())
|
1843
|
+
if self._error.code != 0:
|
1844
|
+
raise FactoryException(self._error.what)
|
1845
|
+
indices_count = indices_count.value
|
1812
1846
|
|
1813
1847
|
buffer = ctypes.create_string_buffer(name_buffer_length)
|
1814
1848
|
|
@@ -1816,9 +1850,9 @@ class _DataFrameBuilder:
|
|
1816
1850
|
for index in range(indices_count):
|
1817
1851
|
factorylib.lib.psrd_table_index_get_name(df.handler(), index,
|
1818
1852
|
buffer, name_buffer_length,
|
1819
|
-
self.
|
1820
|
-
if self.
|
1821
|
-
raise FactoryException(self.
|
1853
|
+
self._error.handler())
|
1854
|
+
if self._error.code != 0:
|
1855
|
+
raise FactoryException(self._error.what)
|
1822
1856
|
index_name = _from_c_str(buffer.value)
|
1823
1857
|
self.indices[index].name = index_name
|
1824
1858
|
self.indices[index].values = [None] * rows_count
|
@@ -1827,9 +1861,9 @@ class _DataFrameBuilder:
|
|
1827
1861
|
factorylib.lib.psrd_table_index_get_date_values(df.handler(),
|
1828
1862
|
index,
|
1829
1863
|
array_values,
|
1830
|
-
self.
|
1831
|
-
if self.
|
1832
|
-
raise FactoryException(self.
|
1864
|
+
self._error.handler())
|
1865
|
+
if self._error.code != 0:
|
1866
|
+
raise FactoryException(self._error.what)
|
1833
1867
|
# convert array values to python datetime
|
1834
1868
|
self.indices[index].values = [dt.datetime.fromtimestamp(value - _date_transform, dt.UTC) for value in array_values]
|
1835
1869
|
else:
|
@@ -1837,30 +1871,40 @@ class _DataFrameBuilder:
|
|
1837
1871
|
factorylib.lib.psrd_table_index_get_int32_values(df.handler(),
|
1838
1872
|
index,
|
1839
1873
|
array_values,
|
1840
|
-
self.
|
1841
|
-
if self.
|
1842
|
-
raise FactoryException(self.
|
1874
|
+
self._error.handler())
|
1875
|
+
if self._error.code != 0:
|
1876
|
+
raise FactoryException(self._error.what)
|
1843
1877
|
self.indices[index].values = array_values
|
1844
1878
|
|
1845
1879
|
self.columns = [_TableColumn() for _ in range(columns_count)]
|
1846
1880
|
for column in range(columns_count):
|
1847
1881
|
factorylib.lib.psrd_table_column_get_name(df.handler(), column,
|
1848
1882
|
buffer, name_buffer_length,
|
1849
|
-
self.
|
1850
|
-
if self.
|
1851
|
-
raise FactoryException(self.
|
1883
|
+
self._error.handler())
|
1884
|
+
if self._error.code != 0:
|
1885
|
+
raise FactoryException(self._error.what)
|
1852
1886
|
self.columns[column].name = _from_c_str(buffer.value)
|
1853
1887
|
|
1854
1888
|
array_values = (ctypes.c_double * rows_count)()
|
1855
1889
|
factorylib.lib.psrd_table_column_get_float64_values(df.handler(),
|
1856
1890
|
column,
|
1857
1891
|
array_values,
|
1858
|
-
self.
|
1859
|
-
if self.
|
1860
|
-
raise FactoryException(self.
|
1892
|
+
self._error.handler())
|
1893
|
+
if self._error.code != 0:
|
1894
|
+
raise FactoryException(self._error.what)
|
1861
1895
|
self.columns[column].values = array_values
|
1862
1896
|
self._not_built = False
|
1863
1897
|
|
1898
|
+
def build_desired_dataframe_type(self, **kwargs) -> Union["pandas.DataFrame", "polars.DataFrame", "DataFrame"]:
|
1899
|
+
if _default_dataframe_type == "pandas":
|
1900
|
+
return self.build_pandas_dataframe(**kwargs)
|
1901
|
+
elif _default_dataframe_type == "polars":
|
1902
|
+
return self.build_polars_dataframe(**kwargs)
|
1903
|
+
elif _default_dataframe_type == "factory":
|
1904
|
+
raise NotImplementedError("Returning a psr.factory.DataFrame not implemented yet.")
|
1905
|
+
else:
|
1906
|
+
raise FactoryException(f"Unsupported default dataframe type \"{_default_dataframe_type}\".")
|
1907
|
+
|
1864
1908
|
def build_pandas_dataframe(self, **kwargs) -> "pandas.DataFrame":
|
1865
1909
|
use_object_dtype = kwargs.get("use_object_dtype", True)
|
1866
1910
|
if not _has_pandas():
|
@@ -1906,15 +1950,15 @@ class _DataFrameBuilder:
|
|
1906
1950
|
|
1907
1951
|
self.column_names = table_data.columns
|
1908
1952
|
self.index_names = [index.name for index in table_data_indices]
|
1909
|
-
self.column_types = [
|
1910
|
-
self.index_types = [
|
1953
|
+
self.column_types = [_pandas_dtype_to_column_type(dtype) for dtype in table_data.dtypes]
|
1954
|
+
self.index_types = [_pandas_dtype_to_column_type(index.dtype) for index in table_data_indices]
|
1911
1955
|
replaced_name = False
|
1912
1956
|
for i, name in enumerate(self.index_names):
|
1913
1957
|
if name is None:
|
1914
1958
|
self.index_names[i] = 'date'
|
1915
1959
|
replaced_name = True
|
1916
1960
|
rows = len(table_data.index)
|
1917
|
-
|
1961
|
+
df = self._pre_build_generic(rows, self.index_types, self.column_types)
|
1918
1962
|
|
1919
1963
|
test_conversion_types = {
|
1920
1964
|
pandas.api.types.is_integer_dtype: numpy.int32,
|
@@ -1985,56 +2029,56 @@ class _DataFrameBuilder:
|
|
1985
2029
|
# convert to int64
|
1986
2030
|
index_values = index_values.astype(numpy.int64)
|
1987
2031
|
ptr = index_values.ctypes.data_as(ctypes.POINTER(convert_to_ctype[convert_to_type]))
|
1988
|
-
factorylib.lib.psrd_table_index_set_date_values(
|
2032
|
+
factorylib.lib.psrd_table_index_set_date_values(df.handler(),
|
1989
2033
|
i_index,
|
1990
2034
|
ptr,
|
1991
|
-
self.
|
1992
|
-
if self.
|
1993
|
-
raise FactoryException(self.
|
2035
|
+
self._error.handler())
|
2036
|
+
if self._error.code != 0:
|
2037
|
+
raise FactoryException(self._error.what)
|
1994
2038
|
elif convert_to_type == numpy.int32:
|
1995
2039
|
ptr = index_values.ctypes.data_as(ctypes.POINTER(convert_to_ctype[convert_to_type]))
|
1996
|
-
factorylib.lib.psrd_table_index_set_int32_values(
|
2040
|
+
factorylib.lib.psrd_table_index_set_int32_values(df.handler(),
|
1997
2041
|
i_index,
|
1998
2042
|
ptr,
|
1999
|
-
self.
|
2000
|
-
if self.
|
2001
|
-
raise FactoryException(self.
|
2043
|
+
self._error.handler())
|
2044
|
+
if self._error.code != 0:
|
2045
|
+
raise FactoryException(self._error.what)
|
2002
2046
|
elif convert_to_type == numpy.int64:
|
2003
2047
|
ptr = index_values.ctypes.data_as(ctypes.POINTER(convert_to_ctype[convert_to_type]))
|
2004
|
-
factorylib.lib.psrd_table_index_set_int64_values(
|
2048
|
+
factorylib.lib.psrd_table_index_set_int64_values(df.handler(),
|
2005
2049
|
i_index,
|
2006
2050
|
ptr,
|
2007
|
-
self.
|
2008
|
-
if self.
|
2009
|
-
raise FactoryException(self.
|
2051
|
+
self._error.handler())
|
2052
|
+
if self._error.code != 0:
|
2053
|
+
raise FactoryException(self._error.what)
|
2010
2054
|
elif convert_to_type == numpy.float32:
|
2011
2055
|
ptr = index_values.ctypes.data_as(ctypes.POINTER(convert_to_ctype[convert_to_type]))
|
2012
|
-
factorylib.lib.psrd_table_index_set_float32_values(
|
2013
|
-
|
2014
|
-
|
2015
|
-
|
2016
|
-
if self.
|
2017
|
-
raise FactoryException(self.
|
2056
|
+
factorylib.lib.psrd_table_index_set_float32_values(df.handler(),
|
2057
|
+
i_index,
|
2058
|
+
ptr,
|
2059
|
+
self._error.handler())
|
2060
|
+
if self._error.code != 0:
|
2061
|
+
raise FactoryException(self._error.what)
|
2018
2062
|
elif convert_to_type == numpy.float64:
|
2019
2063
|
ptr = index_values.ctypes.data_as(ctypes.POINTER(convert_to_ctype[convert_to_type]))
|
2020
|
-
factorylib.lib.psrd_table_index_set_float64_values(
|
2064
|
+
factorylib.lib.psrd_table_index_set_float64_values(df.handler(),
|
2021
2065
|
i_index,
|
2022
2066
|
ptr,
|
2023
|
-
self.
|
2024
|
-
if self.
|
2025
|
-
raise FactoryException(self.
|
2067
|
+
self._error.handler())
|
2068
|
+
if self._error.code != 0:
|
2069
|
+
raise FactoryException(self._error.what)
|
2026
2070
|
else:
|
2027
2071
|
raise FactoryException("Unsupported index type: " + str(convert_to_type))
|
2028
2072
|
else:
|
2029
2073
|
for i_row, column_value in enumerate(index_values):
|
2030
2074
|
self._value.set(column_value)
|
2031
|
-
factorylib.lib.psrd_table_index_set_value(
|
2075
|
+
factorylib.lib.psrd_table_index_set_value(df.handler(),
|
2032
2076
|
i_index,
|
2033
2077
|
i_row,
|
2034
2078
|
self._value.handler(),
|
2035
|
-
self.
|
2036
|
-
if self.
|
2037
|
-
raise FactoryException(self.
|
2079
|
+
self._error.handler())
|
2080
|
+
if self._error.code != 0:
|
2081
|
+
raise FactoryException(self._error.what)
|
2038
2082
|
|
2039
2083
|
for i_column, column_name in enumerate(self.column_names):
|
2040
2084
|
if column_name in column_convert_to.keys():
|
@@ -2045,49 +2089,48 @@ class _DataFrameBuilder:
|
|
2045
2089
|
if column_name in column_fast_set.keys() and column_fast_set[column_name]:
|
2046
2090
|
if convert_to_type == numpy.float32:
|
2047
2091
|
ptr = column_values.ctypes.data_as(ctypes.POINTER(convert_to_ctype[convert_to_type]))
|
2048
|
-
factorylib.lib.psrd_table_column_set_float32_values(
|
2092
|
+
factorylib.lib.psrd_table_column_set_float32_values(df.handler(),
|
2049
2093
|
i_column,
|
2050
2094
|
ptr,
|
2051
|
-
self.
|
2052
|
-
if self.
|
2053
|
-
raise FactoryException(self.
|
2095
|
+
self._error.handler())
|
2096
|
+
if self._error.code != 0:
|
2097
|
+
raise FactoryException(self._error.code)
|
2054
2098
|
if convert_to_type == numpy.float64:
|
2055
2099
|
ptr = column_values.ctypes.data_as(ctypes.POINTER(convert_to_ctype[convert_to_type]))
|
2056
|
-
factorylib.lib.psrd_table_column_set_float64_values(
|
2100
|
+
factorylib.lib.psrd_table_column_set_float64_values(df.handler(),
|
2057
2101
|
i_column,
|
2058
2102
|
ptr,
|
2059
|
-
self.
|
2060
|
-
if self.
|
2061
|
-
raise FactoryException(self.
|
2103
|
+
self._error.handler())
|
2104
|
+
if self._error.code != 0:
|
2105
|
+
raise FactoryException(self._error.what)
|
2062
2106
|
elif convert_to_type == numpy.int32:
|
2063
2107
|
ptr = column_values.ctypes.data_as(ctypes.POINTER(convert_to_ctype[convert_to_type]))
|
2064
|
-
factorylib.lib.psrd_table_column_set_int32_values(
|
2108
|
+
factorylib.lib.psrd_table_column_set_int32_values(df.handler(),
|
2065
2109
|
i_column,
|
2066
2110
|
ptr,
|
2067
|
-
self.
|
2068
|
-
if self.
|
2069
|
-
raise FactoryException(self.
|
2111
|
+
self._error.handler())
|
2112
|
+
if self._error.code != 0:
|
2113
|
+
raise FactoryException(self._error.what)
|
2070
2114
|
elif convert_to_type == numpy.int64:
|
2071
2115
|
ptr = column_values.ctypes.data_as(ctypes.POINTER(convert_to_ctype[convert_to_type]))
|
2072
|
-
factorylib.lib.psrd_table_column_set_int64_values(
|
2116
|
+
factorylib.lib.psrd_table_column_set_int64_values(df.handler(),
|
2073
2117
|
i_column,
|
2074
2118
|
ptr,
|
2075
|
-
self.
|
2076
|
-
if self.
|
2077
|
-
raise FactoryException(self.
|
2119
|
+
self._error.handler())
|
2120
|
+
if self._error.code != 0:
|
2121
|
+
raise FactoryException(self._error.what)
|
2078
2122
|
else:
|
2079
2123
|
column_values = table_data[column_name]
|
2080
2124
|
for i_row, column_value in enumerate(column_values):
|
2081
2125
|
self._value.set(column_value)
|
2082
|
-
factorylib.lib.psrd_table_column_set_value(
|
2126
|
+
factorylib.lib.psrd_table_column_set_value(df.handler(),
|
2083
2127
|
i_column,
|
2084
2128
|
i_row,
|
2085
2129
|
self._value.handler(),
|
2086
|
-
self.
|
2087
|
-
if self.
|
2088
|
-
raise FactoryException(self.
|
2089
|
-
return
|
2090
|
-
|
2130
|
+
self._error.handler())
|
2131
|
+
if self._error.code != 0:
|
2132
|
+
raise FactoryException(self._error.what)
|
2133
|
+
return df
|
2091
2134
|
|
2092
2135
|
def build_polars_dataframe(self, **kwargs) -> "polars.DataFrame":
|
2093
2136
|
use_object_dtype = kwargs.get("use_object_dtype", True)
|
@@ -2110,75 +2153,74 @@ class _DataFrameBuilder:
|
|
2110
2153
|
|
2111
2154
|
def build_from_polars(self, table_data: "polars.DataFrame") -> "DataFrame":
|
2112
2155
|
# check if the table has indices and if its multi-index or common index
|
2113
|
-
|
2156
|
+
index_names = ("year", "week", "month", "hour", "scenario", "block", "stage")
|
2114
2157
|
column_index = 0
|
2115
2158
|
data_columns = table_data.columns[:]
|
2116
2159
|
index_columns = []
|
2117
2160
|
while column_index < len(data_columns):
|
2118
|
-
if data_columns[column_index] in
|
2161
|
+
if data_columns[column_index] in index_names:
|
2119
2162
|
index_columns.append(data_columns.pop(column_index))
|
2120
2163
|
continue
|
2121
2164
|
column_index += 1
|
2122
|
-
self.column_types = [
|
2123
|
-
self.index_types = [
|
2165
|
+
self.column_types = [_polars_dtype_to_column_type(table_data[column_name].dtype) for column_name in data_columns]
|
2166
|
+
self.index_types = [_polars_dtype_to_column_type(table_data[index_name].dtype) for index_name in index_columns]
|
2124
2167
|
|
2125
2168
|
self.column_names = data_columns
|
2126
2169
|
self.index_names = index_columns
|
2127
2170
|
rows = table_data.height
|
2128
|
-
|
2171
|
+
df = self._pre_build_generic(rows, self.index_types, self.column_types)
|
2129
2172
|
|
2130
2173
|
for i_row, all_row_values in enumerate(table_data.iter_rows()):
|
2131
2174
|
index = all_row_values[:len(index_columns)]
|
2132
2175
|
row_values = all_row_values[len(index_columns):]
|
2133
|
-
self._set_row_values(
|
2134
|
-
return
|
2176
|
+
self._set_row_values(df, i_row, index, row_values)
|
2177
|
+
return df
|
2135
2178
|
|
2136
2179
|
def _pre_build_generic(self, rows: int, index_types: List[int], column_types: List[int]) -> "DataFrame":
|
2137
|
-
|
2138
|
-
self.
|
2180
|
+
df = DataFrame()
|
2181
|
+
self._error = Error()
|
2139
2182
|
self._value = Value()
|
2140
2183
|
|
2141
|
-
factorylib.lib.psrd_table_resize(
|
2142
|
-
self.
|
2143
|
-
if self.
|
2144
|
-
raise FactoryException(self.
|
2184
|
+
factorylib.lib.psrd_table_resize(df.handler(), rows,
|
2185
|
+
self._error.handler())
|
2186
|
+
if self._error.code != 0:
|
2187
|
+
raise FactoryException(self._error.what)
|
2145
2188
|
|
2146
2189
|
for i_index, index_type in enumerate(index_types):
|
2147
|
-
factorylib.lib.psrd_table_configure_index(
|
2190
|
+
factorylib.lib.psrd_table_configure_index(df.handler(),
|
2148
2191
|
i_index,
|
2149
2192
|
index_type,
|
2150
|
-
self.
|
2151
|
-
if self.
|
2152
|
-
raise FactoryException(self.
|
2193
|
+
self._error.handler())
|
2194
|
+
if self._error.code != 0:
|
2195
|
+
raise FactoryException(self._error.what)
|
2153
2196
|
for i_column, column_type in enumerate(column_types):
|
2154
|
-
factorylib.lib.psrd_table_configure_column(
|
2197
|
+
factorylib.lib.psrd_table_configure_column(df.handler(),
|
2155
2198
|
i_column,
|
2156
2199
|
column_type,
|
2157
|
-
self.
|
2158
|
-
if self.
|
2159
|
-
raise FactoryException(self.
|
2200
|
+
self._error.handler())
|
2201
|
+
if self._error.code != 0:
|
2202
|
+
raise FactoryException(self._error.what)
|
2160
2203
|
|
2161
2204
|
# Set column names
|
2162
2205
|
for i_column, column_name in enumerate(self.column_names):
|
2163
|
-
factorylib.lib.psrd_table_column_set_name(
|
2206
|
+
factorylib.lib.psrd_table_column_set_name(df.handler(),
|
2164
2207
|
i_column,
|
2165
2208
|
_c_str(column_name),
|
2166
2209
|
_bytes(column_name),
|
2167
|
-
self.
|
2168
|
-
if self.
|
2169
|
-
raise FactoryException(self.
|
2210
|
+
self._error.handler())
|
2211
|
+
if self._error.code != 0:
|
2212
|
+
raise FactoryException(self._error.what)
|
2170
2213
|
|
2171
2214
|
# Set index names
|
2172
2215
|
for i_index, index_name in enumerate(self.index_names):
|
2173
|
-
factorylib.lib.psrd_table_index_set_name(
|
2216
|
+
factorylib.lib.psrd_table_index_set_name(df.handler(),
|
2174
2217
|
i_index,
|
2175
2218
|
_c_str(index_name),
|
2176
2219
|
_bytes(index_name),
|
2177
|
-
self.
|
2178
|
-
if self.
|
2179
|
-
raise FactoryException(self.
|
2180
|
-
|
2181
|
-
return _df
|
2220
|
+
self._error.handler())
|
2221
|
+
if self._error.code != 0:
|
2222
|
+
raise FactoryException(self._error.what)
|
2223
|
+
return df
|
2182
2224
|
|
2183
2225
|
def _set_row_values(self, df: "DataFrame", i_row: int, index_values: List[Union[int, float, str]], column_values: List[Union[int, float, str]]):
|
2184
2226
|
self._value = Value()
|
@@ -2188,9 +2230,9 @@ class _DataFrameBuilder:
|
|
2188
2230
|
i_index,
|
2189
2231
|
i_row,
|
2190
2232
|
self._value.handler(),
|
2191
|
-
self.
|
2192
|
-
if self.
|
2193
|
-
raise FactoryException(self.
|
2233
|
+
self._error.handler())
|
2234
|
+
if self._error.code != 0:
|
2235
|
+
raise FactoryException(self._error.what)
|
2194
2236
|
|
2195
2237
|
for i_column, column_value in enumerate(column_values):
|
2196
2238
|
self._value.set(column_value)
|
@@ -2198,9 +2240,9 @@ class _DataFrameBuilder:
|
|
2198
2240
|
i_column,
|
2199
2241
|
i_row,
|
2200
2242
|
self._value.handler(),
|
2201
|
-
self.
|
2202
|
-
if self.
|
2203
|
-
raise FactoryException(self.
|
2243
|
+
self._error.handler())
|
2244
|
+
if self._error.code != 0:
|
2245
|
+
raise FactoryException(self._error.what)
|
2204
2246
|
|
2205
2247
|
|
2206
2248
|
class DataFrame(_BaseObject):
|
@@ -2217,22 +2259,25 @@ class DataFrame(_BaseObject):
|
|
2217
2259
|
def load_from_file(input_file: Union[str, pathlib.Path], options: Optional[Union[dict, Value, DataObject]] = None) -> "DataFrame":
|
2218
2260
|
input_file = str(input_file)
|
2219
2261
|
_check_initialized()
|
2220
|
-
|
2221
|
-
|
2262
|
+
error = Error()
|
2263
|
+
df = DataFrame()
|
2222
2264
|
options_value = _get_arg_object(options)
|
2223
|
-
factorylib.lib.psrd_table_load(
|
2265
|
+
factorylib.lib.psrd_table_load(df.handler(),
|
2224
2266
|
_c_str(input_file),
|
2225
2267
|
_bytes(input_file),
|
2226
2268
|
options_value.handler(),
|
2227
|
-
|
2228
|
-
if
|
2229
|
-
raise FactoryException(
|
2230
|
-
return
|
2269
|
+
error.handler())
|
2270
|
+
if error.code != 0:
|
2271
|
+
raise FactoryException(error.what)
|
2272
|
+
return df
|
2231
2273
|
|
2232
2274
|
@staticmethod
|
2233
|
-
def from_dataframe(df: Union["pandas.DataFrame", "polars.DataFrame"], **kwargs) -> "DataFrame":
|
2275
|
+
def from_dataframe(df: Union["pandas.DataFrame", "polars.DataFrame", "DataFrame"], **kwargs) -> "DataFrame":
|
2234
2276
|
_check_initialized()
|
2235
2277
|
df_builder = _DataFrameBuilder()
|
2278
|
+
if isinstance(df, DataFrame):
|
2279
|
+
# FIXME: implement this
|
2280
|
+
raise NotImplementedError("Creating a DataFrame from another psr.factory.DataFrame is not implemented.")
|
2236
2281
|
if _has_pandas() and isinstance(df, pandas.DataFrame):
|
2237
2282
|
dataframe_like = pandas.api.interchange.from_dataframe(df)
|
2238
2283
|
return df_builder.build_from_pandas(dataframe_like)
|
@@ -2244,14 +2289,14 @@ class DataFrame(_BaseObject):
|
|
2244
2289
|
|
2245
2290
|
def save(self, output_file: Union[str, pathlib.Path], options: Optional[Union[dict, Value, DataObject]] = None):
|
2246
2291
|
output_file = str(output_file)
|
2247
|
-
|
2292
|
+
error = Error()
|
2248
2293
|
options_value = _get_arg_object(options)
|
2249
2294
|
factorylib.lib.psrd_table_save(self._hdr, _c_str(output_file),
|
2250
2295
|
_bytes(output_file),
|
2251
2296
|
options_value.handler(),
|
2252
|
-
|
2253
|
-
if
|
2254
|
-
raise FactoryException(
|
2297
|
+
error.handler())
|
2298
|
+
if error.code != 0:
|
2299
|
+
raise FactoryException(error.what)
|
2255
2300
|
|
2256
2301
|
def to_pandas(self) -> "pandas.DataFrame":
|
2257
2302
|
df_builder = _DataFrameBuilder()
|
@@ -2266,25 +2311,25 @@ class DataFrame(_BaseObject):
|
|
2266
2311
|
|
2267
2312
|
def get(self, expression: str) -> Union[int, float, dt.datetime, str, "DataObject", list, None]:
|
2268
2313
|
value = Value()
|
2269
|
-
|
2314
|
+
error = Error()
|
2270
2315
|
factorylib.lib.psrd_table_get_property(self._hdr,
|
2271
2316
|
_c_str(expression),
|
2272
2317
|
value.handler(),
|
2273
|
-
|
2274
|
-
if
|
2275
|
-
raise FactoryException(
|
2318
|
+
error.handler())
|
2319
|
+
if error.code != 0:
|
2320
|
+
raise FactoryException(error.what)
|
2276
2321
|
return value.get()
|
2277
2322
|
|
2278
|
-
def set(self, expression: str, value):
|
2279
|
-
|
2280
|
-
|
2281
|
-
|
2323
|
+
def set(self, expression: str, value: any):
|
2324
|
+
error = Error()
|
2325
|
+
value_object = Value()
|
2326
|
+
value_object.set(value)
|
2282
2327
|
factorylib.lib.psrd_table_set_property(self._hdr, _c_str(expression),
|
2283
2328
|
_bytes(expression),
|
2284
|
-
|
2285
|
-
|
2286
|
-
if
|
2287
|
-
raise FactoryException(
|
2329
|
+
value_object.handler(),
|
2330
|
+
error.handler())
|
2331
|
+
if error.code != 0:
|
2332
|
+
raise FactoryException(error.what)
|
2288
2333
|
|
2289
2334
|
|
2290
2335
|
def load_dataframe(input_file: Union[str, pathlib.Path], **kwargs) -> DataFrame:
|
@@ -2292,7 +2337,7 @@ def load_dataframe(input_file: Union[str, pathlib.Path], **kwargs) -> DataFrame:
|
|
2292
2337
|
return DataFrame.load_from_file(input_file, options)
|
2293
2338
|
|
2294
2339
|
|
2295
|
-
def create_dataframe(data: "pandas.DataFrame", **kwargs) -> DataFrame:
|
2340
|
+
def create_dataframe(data: Union["pandas.DataFrame", "polars.DataFrame", "DataFrame"], **kwargs) -> DataFrame:
|
2296
2341
|
return DataFrame.from_dataframe(data, **kwargs)
|
2297
2342
|
|
2298
2343
|
|
@@ -2311,7 +2356,7 @@ def _initialize():
|
|
2311
2356
|
global _initialized_lock
|
2312
2357
|
with _initialized_lock:
|
2313
2358
|
_check_loaded()
|
2314
|
-
|
2359
|
+
error = Error()
|
2315
2360
|
|
2316
2361
|
# Set binding info
|
2317
2362
|
map_prop_values = {
|
@@ -2325,44 +2370,44 @@ def _initialize():
|
|
2325
2370
|
"BASE_PREFIX": f"{sys.base_prefix}",
|
2326
2371
|
"REAL_PREFIX": f"{sys.prefix}",
|
2327
2372
|
}
|
2328
|
-
for prop,
|
2329
|
-
|
2330
|
-
|
2373
|
+
for prop, prop_value in map_prop_values.items():
|
2374
|
+
value_object = Value()
|
2375
|
+
value_object.set(prop_value)
|
2331
2376
|
factorylib.lib.psrd_set_binding_property(_c_str(prop),
|
2332
2377
|
_bytes(prop),
|
2333
|
-
|
2334
|
-
|
2335
|
-
if
|
2336
|
-
raise FactoryException(
|
2378
|
+
value_object.handler(),
|
2379
|
+
error.handler())
|
2380
|
+
if error.code != 0:
|
2381
|
+
raise FactoryException(error.what)
|
2337
2382
|
|
2338
2383
|
# Where to look for pmd and pmk files
|
2339
2384
|
module_path = os.path.dirname(__file__)
|
2340
2385
|
factorylib.lib.psrd_initialize(_c_str(module_path),
|
2341
2386
|
_bytes(module_path),
|
2342
|
-
|
2343
|
-
if
|
2344
|
-
raise FactoryException(
|
2387
|
+
error.handler())
|
2388
|
+
if error.code != 0:
|
2389
|
+
raise FactoryException(error.what)
|
2345
2390
|
_initialized = True
|
2346
2391
|
|
2347
2392
|
|
2348
2393
|
def _unload():
|
2349
|
-
|
2350
|
-
factorylib.lib.psrd_unload(
|
2351
|
-
if
|
2352
|
-
raise FactoryException(
|
2394
|
+
error = Error()
|
2395
|
+
factorylib.lib.psrd_unload(error.handler())
|
2396
|
+
if error.code != 0:
|
2397
|
+
raise FactoryException(error.what)
|
2353
2398
|
|
2354
2399
|
|
2355
2400
|
def help(context: str = "") -> str:
|
2356
|
-
|
2401
|
+
error = Error()
|
2357
2402
|
size = factorylib.lib.psrd_help(_c_str(context), _bytes(context),
|
2358
|
-
None, 0,
|
2359
|
-
if
|
2360
|
-
raise FactoryException(
|
2403
|
+
None, 0, error.handler())
|
2404
|
+
if error.code != 0:
|
2405
|
+
raise FactoryException(error.what)
|
2361
2406
|
buffer = ctypes.create_string_buffer(size)
|
2362
2407
|
factorylib.lib.psrd_help(_c_str(context), _bytes(context),
|
2363
|
-
buffer, size,
|
2364
|
-
if
|
2365
|
-
raise FactoryException(
|
2408
|
+
buffer, size, error.handler())
|
2409
|
+
if error.code != 0:
|
2410
|
+
raise FactoryException(error.what)
|
2366
2411
|
return _from_c_str(buffer.value)
|
2367
2412
|
|
2368
2413
|
|
@@ -2370,43 +2415,41 @@ def create_study(*args, **kwargs) -> Study:
|
|
2370
2415
|
blocks = kwargs.get("blocks", None)
|
2371
2416
|
models = kwargs.get("models", None)
|
2372
2417
|
context = kwargs.get("context", None) if len(args) == 0 else args[0]
|
2373
|
-
|
2374
|
-
|
2375
|
-
|
2376
|
-
|
2377
|
-
else context
|
2378
|
-
return Study.create_object(profile_or_context, blocks)
|
2418
|
+
if "profile" in kwargs:
|
2419
|
+
raise DeprecationWarning("The 'profile' argument is deprecated. Use 'models' instead.")
|
2420
|
+
model_or_context = models if models is not None and len(models) > 0 else context
|
2421
|
+
return Study.create_object(model_or_context, blocks)
|
2379
2422
|
|
2380
2423
|
|
2381
2424
|
def load_study(study_path: Union[str, pathlib.Path],
|
2382
|
-
|
2425
|
+
model_or_context: Union[str, Context, None] = None,
|
2383
2426
|
options: Optional[DataObject] = None) -> Study:
|
2384
2427
|
settings_only = False
|
2385
|
-
return Study.load(study_path,
|
2428
|
+
return Study.load(study_path, model_or_context, settings_only, options)
|
2386
2429
|
|
2387
2430
|
|
2388
2431
|
def load_study_settings(study_path: Union[str, pathlib.Path],
|
2389
|
-
|
2432
|
+
model_or_context: Union[str, Context, None] = None,
|
2390
2433
|
options: Optional[DataObject] = None) -> Study:
|
2391
2434
|
settings_only = True
|
2392
|
-
return Study.load(study_path,
|
2435
|
+
return Study.load(study_path, model_or_context, settings_only, options)
|
2393
2436
|
|
2394
2437
|
|
2395
2438
|
def create(class_name: str,
|
2396
|
-
|
2439
|
+
model_or_context: Union[str, Context, None] = None) -> Optional[DataObject]:
|
2397
2440
|
_check_initialized()
|
2398
|
-
|
2399
|
-
|
2400
|
-
context = _get_context(
|
2441
|
+
data_object = DataObject()
|
2442
|
+
error = Error()
|
2443
|
+
context = _get_context(model_or_context, None) \
|
2401
2444
|
if class_name != "Context" else None
|
2402
|
-
|
2403
|
-
|
2404
|
-
|
2405
|
-
|
2406
|
-
if
|
2407
|
-
raise FactoryException(
|
2408
|
-
|
2409
|
-
return
|
2445
|
+
context_handler = context.handler() if context is not None else None
|
2446
|
+
handler = factorylib.lib.psrd_create(_c_str(class_name),
|
2447
|
+
context_handler,
|
2448
|
+
error.handler())
|
2449
|
+
if error.code != 0 or handler is None:
|
2450
|
+
raise FactoryException(error.what)
|
2451
|
+
data_object._hdr = handler
|
2452
|
+
return data_object
|
2410
2453
|
|
2411
2454
|
|
2412
2455
|
def get_default_context() -> "Context":
|