Utility to join pandas tables, select rows, columns and generate groups. Will return a list of dictionaries, every dictionary maps to a row of data in tables. Args: dfs: data table in pandas Dataframe format. if providing a list of tables, will join them. row_indices: i
(
dfs,
row_indices: Sequence[int | str] | None = None,
col_names: Sequence[str] | None = None,
col_types: dict[str, dict[str, Any] | None] | None = None,
col_groups: dict[str, Sequence[str]] | None = None,
**kwargs,
)
| 1406 | |
| 1407 | |
| 1408 | def convert_tables_to_dicts( |
| 1409 | dfs, |
| 1410 | row_indices: Sequence[int | str] | None = None, |
| 1411 | col_names: Sequence[str] | None = None, |
| 1412 | col_types: dict[str, dict[str, Any] | None] | None = None, |
| 1413 | col_groups: dict[str, Sequence[str]] | None = None, |
| 1414 | **kwargs, |
| 1415 | ) -> list[dict[str, Any]]: |
| 1416 | """ |
| 1417 | Utility to join pandas tables, select rows, columns and generate groups. |
| 1418 | Will return a list of dictionaries, every dictionary maps to a row of data in tables. |
| 1419 | |
| 1420 | Args: |
| 1421 | dfs: data table in pandas Dataframe format. if providing a list of tables, will join them. |
| 1422 | row_indices: indices of the expected rows to load. it should be a list, |
| 1423 | every item can be a int number or a range `[start, end)` for the indices. |
| 1424 | for example: `row_indices=[[0, 100], 200, 201, 202, 300]`. if None, |
| 1425 | load all the rows in the file. |
| 1426 | col_names: names of the expected columns to load. if None, load all the columns. |
| 1427 | col_types: `type` and `default value` to convert the loaded columns, if None, use original data. |
| 1428 | it should be a dictionary, every item maps to an expected column, the `key` is the column |
| 1429 | name and the `value` is None or a dictionary to define the default value and data type. |
| 1430 | the supported keys in dictionary are: ["type", "default"], and note that the value of `default` |
| 1431 | should not be `None`. for example:: |
| 1432 | |
| 1433 | col_types = { |
| 1434 | "subject_id": {"type": str}, |
| 1435 | "label": {"type": int, "default": 0}, |
| 1436 | "ehr_0": {"type": float, "default": 0.0}, |
| 1437 | "ehr_1": {"type": float, "default": 0.0}, |
| 1438 | } |
| 1439 | |
| 1440 | col_groups: args to group the loaded columns to generate a new column, |
| 1441 | it should be a dictionary, every item maps to a group, the `key` will |
| 1442 | be the new column name, the `value` is the names of columns to combine. for example: |
| 1443 | `col_groups={"ehr": [f"ehr_{i}" for i in range(10)], "meta": ["meta_1", "meta_2"]}` |
| 1444 | kwargs: additional arguments for `pandas.merge()` API to join tables. |
| 1445 | |
| 1446 | """ |
| 1447 | df = reduce(lambda l, r: pd.merge(l, r, **kwargs), ensure_tuple(dfs)) |
| 1448 | # parse row indices |
| 1449 | rows: list[int | str] = [] |
| 1450 | if row_indices is None: |
| 1451 | rows = df.index.tolist() |
| 1452 | else: |
| 1453 | for i in row_indices: |
| 1454 | if isinstance(i, (tuple, list)): |
| 1455 | if len(i) != 2: |
| 1456 | raise ValueError("range of row indices must contain 2 values: start and end.") |
| 1457 | rows.extend(list(range(i[0], i[1]))) |
| 1458 | else: |
| 1459 | rows.append(i) |
| 1460 | |
| 1461 | # convert to a list of dictionaries corresponding to every row |
| 1462 | data_ = df.loc[rows] if col_names is None else df.loc[rows, col_names] |
| 1463 | if isinstance(col_types, dict): |
| 1464 | # fill default values for NaN |
| 1465 | defaults = {k: v["default"] for k, v in col_types.items() if v is not None and v.get("default") is not None} |
no test coverage detected
searching dependent graphs…