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