Skip to content

edsteva.probes.utils.prepare_df

prepare_care_site_relationship

prepare_care_site_relationship(data: Data) -> pd.DataFrame

Computes hierarchical care site structure

PARAMETER DESCRIPTION
data

Instantiated HiveData, PostgresData or LocalData

TYPE: Data

Example
care_site_id care_site_level care_site_short_name parent_care_site_id parent_care_site_level parent_care_site_short_name
8312056386 Unité Fonctionnelle (UF) UF A 8312027648 Pôle/DMU Pole A
8312022130 Pôle/DMU Pole B 8312033550 Hôpital Hospital A
8312016782 Service/Département Service A 8312033550 Hôpital Hospital A
8312010155 Unité Fonctionnelle (UF) UF B 8312022130 Pôle/DMU Pole B
8312067829 Unité de consultation (UC) UC A 8312051097 Unité de consultation (UC) UC B
Source code in edsteva/probes/utils/prepare_df.py
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
def prepare_care_site_relationship(data: Data) -> pd.DataFrame:
    """Computes hierarchical care site structure

    Parameters
    ----------
    data : Data
        Instantiated [``HiveData``][edsteva.io.hive.HiveData], [``PostgresData``][edsteva.io.postgres.PostgresData] or [``LocalData``][edsteva.io.files.LocalData]

    Example
    -------

    | care_site_id | care_site_level            | care_site_short_name | parent_care_site_id | parent_care_site_level     | parent_care_site_short_name |
    | :----------- | :------------------------- | :------------------- | :------------------ | :------------------------- | :-------------------------- |
    | 8312056386   | Unité Fonctionnelle (UF)   | UF A                 | 8312027648          | Pôle/DMU                   | Pole A                      |
    | 8312022130   | Pôle/DMU                   | Pole B               | 8312033550          | Hôpital                    | Hospital A                  |
    | 8312016782   | Service/Département        | Service A            | 8312033550          | Hôpital                    | Hospital A                  |
    | 8312010155   | Unité Fonctionnelle (UF)   | UF B                 | 8312022130          | Pôle/DMU                   | Pole B                      |
    | 8312067829   | Unité de consultation (UC) | UC A                 | 8312051097          | Unité de consultation (UC) | UC B                        |

    """
    fact_relationship = data.fact_relationship[
        [
            "fact_id_1",
            "fact_id_2",
            "domain_concept_id_1",
            "relationship_concept_id",
        ]
    ]
    fact_relationship = to("pandas", fact_relationship)

    care_site_relationship = fact_relationship[
        (fact_relationship["domain_concept_id_1"] == 57)  # Care_site domain
        & (fact_relationship["relationship_concept_id"] == 46233688)  # Included in
    ]
    care_site_relationship = care_site_relationship.drop(
        columns=["domain_concept_id_1", "relationship_concept_id"]
    )
    care_site_relationship = care_site_relationship.rename(
        columns={"fact_id_1": "care_site_id", "fact_id_2": "parent_care_site_id"}
    )

    care_site = data.care_site[
        [
            "care_site_id",
            "care_site_type_source_value",
            "care_site_short_name",
            "place_of_service_source_value",
        ]
    ]
    care_site = to("pandas", care_site)
    care_site = care_site.rename(
        columns={
            "care_site_type_source_value": "care_site_level",
            "place_of_service_source_value": "care_site_specialty",
        }
    )
    care_site_relationship = care_site.merge(
        care_site_relationship, on="care_site_id", how="left"
    )

    parent_care_site = care_site.rename(
        columns={
            "care_site_level": "parent_care_site_level",
            "care_site_id": "parent_care_site_id",
            "care_site_short_name": "parent_care_site_short_name",
            "care_site_specialty": "parent_care_site_specialty",
        }
    )
    logger.debug("Create care site relationship to link UC to UF and UF to Pole")

    return care_site_relationship.merge(
        parent_care_site, on="parent_care_site_id", how="left"
    )

prepare_biology_relationship

prepare_biology_relationship(
    data: Data,
    standard_terminologies: List[str],
    source_terminologies: Dict[str, str],
    mapping: List[Tuple[str, str, str]],
) -> pd.DataFrame

Computes biology relationship

PARAMETER DESCRIPTION
data

Instantiated HiveData, PostgresData or LocalData

TYPE: Data

Example
care_site_id care_site_level care_site_short_name parent_care_site_id parent_care_site_level parent_care_site_short_name
8312056386 Unité Fonctionnelle (UF) UF A 8312027648 Pôle/DMU Pole A
8312022130 Pôle/DMU Pole B 8312033550 Hôpital Hospital A
8312016782 Service/Département Service A 8312033550 Hôpital Hospital A
8312010155 Unité Fonctionnelle (UF) UF B 8312022130 Pôle/DMU Pole B
8312067829 Unité de consultation (UC) UC A 8312051097 Unité de consultation (UC) UC B
Source code in edsteva/probes/utils/prepare_df.py
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
def prepare_biology_relationship(
    data: Data,
    standard_terminologies: List[str],
    source_terminologies: Dict[str, str],
    mapping: List[Tuple[str, str, str]],
) -> pd.DataFrame:
    """Computes biology relationship

    Parameters
    ----------
    data : Data
        Instantiated [``HiveData``][edsteva.io.hive.HiveData], [``PostgresData``][edsteva.io.postgres.PostgresData] or [``LocalData``][edsteva.io.files.LocalData]

    Example
    -------

    | care_site_id | care_site_level            | care_site_short_name | parent_care_site_id | parent_care_site_level     | parent_care_site_short_name |
    | :----------- | :------------------------- | :------------------- | :------------------ | :------------------------- | :-------------------------- |
    | 8312056386   | Unité Fonctionnelle (UF)   | UF A                 | 8312027648          | Pôle/DMU                   | Pole A                      |
    | 8312022130   | Pôle/DMU                   | Pole B               | 8312033550          | Hôpital                    | Hospital A                  |
    | 8312016782   | Service/Département        | Service A            | 8312033550          | Hôpital                    | Hospital A                  |
    | 8312010155   | Unité Fonctionnelle (UF)   | UF B                 | 8312022130          | Pôle/DMU                   | Pole B                      |
    | 8312067829   | Unité de consultation (UC) | UC A                 | 8312051097          | Unité de consultation (UC) | UC B                        |

    """

    logger.debug(
        "Create biology relationship to link ANALYSES LABORATOIRE to ANABIO to LOINC"
    )

    check_tables(data=data, required_tables=["concept", "concept_relationship"])
    concept_columns = [
        "concept_id",
        "concept_name",
        "concept_code",
        "vocabulary_id",
    ]

    concept_relationship_columns = [
        "concept_id_1",
        "concept_id_2",
        "relationship_id",
    ]
    check_columns(
        data.concept,
        required_columns=concept_columns,
        df_name="concept",
    )

    check_columns(
        data.concept_relationship,
        required_columns=concept_relationship_columns,
        df_name="concept_relationship",
    )
    concept = to("pandas", data.concept[concept_columns])
    concept_relationship = to(
        "pandas", data.concept_relationship[concept_relationship_columns]
    )
    concept_by_terminology = {}
    for terminology, regex in source_terminologies.items():
        concept_by_terminology[terminology] = (
            concept[concept.vocabulary_id.str.contains(regex)]
            .rename(
                columns={
                    "concept_id": "{}_concept_id".format(terminology),
                    "concept_name": "{}_concept_name".format(terminology),
                    "concept_code": "{}_concept_code".format(terminology),
                }
            )
            .drop(columns="vocabulary_id")
        )
    root_terminology = mapping[0][0]
    biology_relationship = concept_by_terminology[root_terminology]
    for source, target, relationship_id in mapping:
        relationship = concept_relationship.rename(
            columns={
                "concept_id_1": "{}_concept_id".format(source),
                "concept_id_2": "{}_concept_id".format(target),
            }
        )[concept_relationship.relationship_id == relationship_id].drop(
            columns="relationship_id"
        )
        relationship = relationship.merge(
            concept_by_terminology[target], on="{}_concept_id".format(target)
        )
        biology_relationship = biology_relationship.merge(
            relationship, on="{}_concept_id".format(source), how="left"
        )

    # Get ITM code in priority and if not get GLIMS code
    for standard_terminology in standard_terminologies:
        biology_relationship[
            "{}_concept_code".format(standard_terminology)
        ] = biology_relationship[
            "{}_ITM_concept_code".format(standard_terminology)
        ].mask(
            biology_relationship[
                "{}_ITM_concept_code".format(standard_terminology)
            ].isna(),
            biology_relationship["GLIMS_{}_concept_code".format(standard_terminology)],
        )
        biology_relationship[
            "{}_concept_name".format(standard_terminology)
        ] = biology_relationship[
            "{}_ITM_concept_name".format(standard_terminology)
        ].mask(
            biology_relationship[
                "{}_ITM_concept_name".format(standard_terminology)
            ].isna(),
            biology_relationship["GLIMS_{}_concept_name".format(standard_terminology)],
        )

        biology_relationship["{}_vocabulary".format(standard_terminology)] = "ITM"
        biology_relationship[
            "{}_vocabulary".format(standard_terminology)
        ] = biology_relationship["{}_vocabulary".format(standard_terminology)].mask(
            biology_relationship[
                "{}_ITM_concept_code".format(standard_terminology)
            ].isna(),
            "GLIMS",
        )

    return biology_relationship