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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308 | class HiveData: # pragma: no cover
"""Spark interface for OMOP data stored in a Hive database.
This class provides a simple access to data stored in Hive.
Data is returned as koalas dataframes that match the tables
stored in Hive.
Parameters
----------
database_name : str
The name of you database in Hive. Ex: "cse_82727572"
database_type : str
The type of your database. Must be "OMOP" or "I2B2"
spark_session : pyspark.sql.SparkSession, optional
If None, a SparkSession will be retrieved or created via `SparkSession.builder.enableHiveSupport().getOrCreate()`
person_ids : Optional[Iterable[int]], default: None
An iterable of `person_id` that is used to define a subset of the database.
tables_to_load : Optional[Dict[str, Optional[List[str]]]], default: None
By default (i.e. if ``tables_to_load is None``), loaded tables and columns loaded in each table are those listed in
:py:data:`~edsteva.io.settings.tables_to_load`.
A dictionnary can be provided to complement those default settings. Keys should be table names to load,
and values should be:
- ``None`` to load all columns
- A list of columns to load (or to add to the default loaded columns if the table is already loaded by default)
Attributes
----------
person : koalas dataframe
Hive data for table `person` as a koalas dataframe.
Other OMOP tables can also be accessed as attributes
available_tables : list of str
names of OMOP tables that can be accessed as attributes with this
HiveData object.
Examples
--------
```python
data = HiveData(database_name="edsomop_prod_a")
data.available_tables
# Out: ["person", "care_site", "condition_occurrence", ... ]
person = data.person
type(person)
# Out: databricks.koalas.frame.DataFrame
person["person_id"].count()
# Out: 12670874
```
This class can be used to create a subset of data for a given
list of `person_id`. This is useful because the smaller dataset
can then be used to prototype more rapidly.
```python
my_person_ids = [9226726, 2092082, 5097816]
data = HiveData(
spark_session=spark, database_name="edsomop_prod_a", person_ids=my_person_ids
)
data.person["person_id"].count()
# Out: 1000
tables_to_save = ["person", "visit_occurrence"]
data.persist_tables_to_folder("./cohort_sample_1000", table_names=tables_to_save)
# Out: writing /export/home/USER/cohort_sample_1000/person.parquet
# Out: writing /export/home/USER/cohort_sample_1000/visit_occurrence.parquet
# Out: ...
```
"""
def __init__(
self,
database_name: str,
database_type: str = "OMOP",
spark_session: Optional[SparkSession] = None,
person_ids: Optional[Iterable[int]] = None,
tables_to_load: Optional[
Union[Dict[str, Optional[List[str]]], List[str]]
] = None,
):
if spark_session is not None:
self.spark_session = spark_session
else:
logger.warning(
"""
To improve performances when using Spark and Koalas, please call `edsteva.improve_performances()`
This function optimally configures Spark. Use it as:
`spark, sc, sql = edsteva.improve_performances()`
The functions respectively returns a SparkSession, a SparkContext and an sql method
"""
)
self.spark_session = SparkSession.builder.enableHiveSupport().getOrCreate()
koalas_options()
self.database_name = database_name
self.database_type = database_type
if self.database_type == "I2B2":
self.database_source = "cse" if "cse" in self.database_name else "edsprod"
self.omop_to_i2b2 = settings.i2b2_tables[self.database_source]
self.i2b2_to_omop = {}
for omop_col, i2b2_col in self.omop_to_i2b2.items():
if i2b2_col in self.i2b2_to_omop.keys():
self.i2b2_to_omop[i2b2_col].append(omop_col)
else:
self.i2b2_to_omop[i2b2_col] = [omop_col]
self.person_ids = self._prepare_person_ids(person_ids)
tmp_tables_to_load = settings.tables_to_load
if isinstance(tables_to_load, dict):
for table_name, columns in tables_to_load.items():
if columns is None:
tmp_tables_to_load[table_name] = None
else:
tmp_tables_to_load[table_name] = list(
set(tmp_tables_to_load.get(table_name, []) + columns)
)
elif isinstance(tables_to_load, list):
for table_name in tables_to_load:
tmp_tables_to_load[table_name] = None
self.tables_to_load = tmp_tables_to_load
self.available_tables = self.list_available_tables()
def list_available_tables(self) -> List[str]:
tables_df = self.spark_session.sql(
f"SHOW TABLES IN {self.database_name}"
).toPandas()
available_tables = []
for table_name in tables_df["tableName"].drop_duplicates().to_list():
if (
self.database_type == "OMOP"
and table_name in self.tables_to_load.keys()
):
available_tables.append(table_name)
elif (
self.database_type == "I2B2" and table_name in self.i2b2_to_omop.keys()
):
for omop_table in self.i2b2_to_omop[table_name]:
if omop_table in self.tables_to_load.keys():
available_tables.append(omop_table)
available_tables = list(set(available_tables))
return available_tables
def rename_table(self, old_table_name: str, new_table_name: str) -> None:
if old_table_name in self.available_tables:
setattr(self, new_table_name, getattr(self, old_table_name))
self.available_tables.remove(old_table_name)
self.available_tables.append(new_table_name)
logger.info("Table {} has been renamed {}", old_table_name, new_table_name)
else:
logger.info("Table {} is not available", old_table_name)
def add_table(self, table_name: str, columns: List[str]) -> None:
tables_df = self.spark_session.sql(
f"SHOW TABLES IN {self.database_name}"
).toPandas()
if table_name in tables_df["tableName"].drop_duplicates().to_list():
self.tables_to_load[table_name] = list(
set(self.tables_to_load.get(table_name, []) + columns)
)
self.available_tables = self.list_available_tables()
logger.info("Table {} has been added", table_name)
else:
raise AttributeError(
f"Table '{table_name}' is not in the database '{self.database_name}'"
)
def delete_table(self, table_name: str) -> None:
self.tables_to_load.pop(table_name, None)
self.available_tables = self.list_available_tables()
logger.info("Table {} has been deleted", table_name)
def _prepare_person_ids(self, list_of_person_ids) -> Optional[SparkDataFrame]:
if list_of_person_ids is None:
return None
if hasattr(list_of_person_ids, "to_list"):
# Useful when list_of_person_ids are Koalas (or Pandas) Series
unique_ids = set(list_of_person_ids.to_list())
else:
unique_ids = set(list_of_person_ids)
logger.info("Number of unique patients: {}", len(unique_ids))
schema = StructType([StructField("person_id", LongType(), True)])
return self.spark_session.createDataFrame(
[(int(p),) for p in unique_ids], schema=schema
)
def _read_table(self, table_name, person_ids=None) -> DataFrame:
assert table_name in self.available_tables
if person_ids is None and self.person_ids is not None:
person_ids = self.person_ids
if self.database_type == "OMOP":
df = self.spark_session.sql(
f"select * from {self.database_name}.{table_name}"
)
elif self.database_type == "I2B2":
df = get_i2b2_table(
spark_session=self.spark_session,
db_name=self.database_name,
db_source=self.database_source,
table=table_name,
)
desired_columns = self.tables_to_load[table_name]
selected_columns = (
df.columns
if desired_columns is None
else [col for col in df.columns if col in desired_columns]
)
df = df.select(*selected_columns)
if "person_id" in df.columns and person_ids is not None:
df = df.join(person_ids, on="person_id", how="inner")
return df.to_koalas()
def persist_tables_to_folder(
self,
folder: str,
person_ids: Optional[Iterable[int]] = None,
tables: List[str] = None,
) -> None:
"""Save OMOP tables as parquet files in a given folder.
Parameters
----------
folder : str
path to folder where the tables will be written.
person_ids : iterable
person_ids to keep in the subcohort
tables : list of str, default None
list of table names to save. Default value is
:py:data:`~edsteva.io.settings.default_tables_to_save`
"""
if tables is None:
tables = settings.default_tables_to_save
unknown_tables = [
table for table in tables if table not in self.available_tables
]
if unknown_tables:
raise ValueError(
f"The following tables are not available : {unknown_tables}"
)
folder = Path.resolve(folder)
assert Path.exists(folder) and Path.is_dir(
folder
), f"Folder {folder} not found."
if person_ids is not None:
person_ids = self._prepare_person_ids(person_ids)
for table in tables:
filepath = folder / f"{table}.parquet"
df = self._read_table(table, person_ids=person_ids)
self._write_df_to_parquet(df, filepath)
def _write_df_to_parquet(
self,
df: DataFrame,
filepath: str,
) -> None:
assert Path.is_absolute(filepath)
logger.info(f"writing {filepath}")
spark_filepath = "file://" + filepath
df.to_parquet(spark_filepath, mode="overwrite")
def __getattr__(self, table_name: str) -> DataFrame:
if table_name in self.available_tables:
# the first time it is called, we actually set the attribute
table = self._read_table(table_name)
setattr(self, table_name, table)
return getattr(self, table_name)
raise AttributeError(f"Table '{table_name}' unknown")
def __dir__(self) -> List[str]:
return list(set(list(super().__dir__()) + self.available_tables))
|