Skip to content

edsteva.io.files

LocalData

Pandas interface to OMOP data stored as local parquet files/folders.

PARAMETER DESCRIPTION
folder

absolute path to a folder containing several parquet files with omop data

TYPE: str

Examples:

>>> data = LocalData(folder="/export/home/USER/my_data/")
>>> person = data.person
>>> person.shape
(100, 10)
ATTRIBUTE DESCRIPTION
person

Pandas dataframe person. All dataframe attributes are dynamically generated to match the content of the selected folder.

TYPE: pd.DataFrame

Source code in edsteva/io/files.py
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
class LocalData:  # pragma: no cover
    """Pandas interface to OMOP data stored as local parquet files/folders.


    Parameters
    ----------
    folder: str
        absolute path to a folder containing several parquet files with omop data

    Examples
    --------
    >>> data = LocalData(folder="/export/home/USER/my_data/")
    >>> person = data.person
    >>> person.shape
    (100, 10)

    Attributes
    ----------
    person: pd.DataFrame
        Pandas dataframe `person`. All dataframe attributes are
        dynamically generated to match the content of the
        selected folder.

    """

    def __init__(
        self,
        folder: str,
    ):
        (
            self.parquet_tables,
            self.pickle_tables,
            self.available_tables,
            self.tables_paths,
        ) = self.list_available_tables(folder)

    @staticmethod
    def list_available_tables(folder: str) -> Tuple[List[str], List[str]]:
        available_tables = []
        pickle_tables = []
        parquet_tables = []
        tables_paths = {}
        for filename in os.listdir(folder):
            file = Path(folder) / filename
            table_name = file.stem
            extension = file.suffix
            if extension == ".parquet":
                abspath = Path.resolve(file)
                tables_paths[table_name] = abspath
                parquet_tables.append(table_name)
                available_tables.append(table_name)
            elif extension == ".pkl" or extension == ".pickle":
                abspath = Path.resolve(file)
                tables_paths[table_name] = abspath
                pickle_tables.append(table_name)
                available_tables.append(table_name)

        return parquet_tables, pickle_tables, available_tables, tables_paths

    def _read_table(self, table_name: str) -> pd.DataFrame:
        path = self.tables_paths[table_name]
        if table_name in self.parquet_tables:
            return pd.read_parquet(path)
        return pd.read_pickle(path)

    def __getattr__(self, table_name: str) -> pd.DataFrame:
        if table_name in self.available_tables:
            return self._read_table(table_name)
        raise AttributeError(f"Table '{table_name}' is not available in chosen folder.")

    def __dir__(self) -> List[str]:
        return list(super().__dir__()) + list(self.available_tables)