-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathdatabaseframe_pandas_merged.py
478 lines (403 loc) · 13.7 KB
/
databaseframe_pandas_merged.py
1
2
3
4
5
6
7
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
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
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
"""
DatabaseFrame class for extracting data as similar
collection of in-memory data
"""
import os
import tempfile
from typing import List, Optional
import pandas as pd
import numpy as np
from sqlalchemy import create_engine
from sqlalchemy.engine.base import Engine
def database_engine_for_testing() -> Engine:
"""
A database engine for testing as a fixture to be passed
to other tests within this file.
"""
# get temporary directory
tmpdir = tempfile.gettempdir()
# remove db if it exists
if os.path.exists(f"{tmpdir}/test_sqlite.sqlite"):
os.remove(f"{tmpdir}/test_sqlite.sqlite")
# create a temporary sqlite connection
sql_path = f"sqlite:///{tmpdir}/test_sqlite.sqlite"
engine = create_engine(sql_path)
# statements for creating database with simple structure
create_stmts = [
"drop table if exists Image;",
"""
create table Image (
TableNumber INTEGER
,ImageNumber INTEGER
,ImageData INTEGER
,RandomDate DATETIME
);
""",
"drop table if exists Cells;",
"""
create table Cells (
TableNumber INTEGER
,ImageNumber INTEGER
,ObjectNumber INTEGER
,CellsData INTEGER
);
""",
"drop table if exists Nuclei;",
"""
create table Nuclei (
TableNumber INTEGER
,ImageNumber INTEGER
,ObjectNumber INTEGER
,NucleiData INTEGER
);
""",
"drop table if exists Cytoplasm;",
"""
create table Cytoplasm (
TableNumber INTEGER
,ImageNumber INTEGER
,ObjectNumber INTEGER
,Cytoplasm_Parent_Cells INTEGER
,Cytoplasm_Parent_Nuclei INTEGER
,CytoplasmData INTEGER
);
""",
]
with engine.begin() as connection:
for stmt in create_stmts:
connection.execute(stmt)
# images
connection.execute(
"INSERT INTO Image VALUES (?, ?, ?, ?);",
[1, 1, 1, "123-123"],
)
# cells
connection.execute(
"INSERT INTO Cells VALUES (?, ?, ?, ?);",
[1, 1, 2, 1],
)
connection.execute(
"INSERT INTO Cells VALUES (?, ?, ?, ?);",
[1, 1, 3, 1],
)
# Nuclei
connection.execute(
"INSERT INTO Nuclei VALUES (?, ?, ?, ?);",
[1, 1, 4, 1],
)
connection.execute(
"INSERT INTO Nuclei VALUES (?, ?, ?, ?);",
[1, 1, 5, 1],
)
# cytoplasm
connection.execute(
"INSERT INTO Cytoplasm VALUES (?, ?, ?, ?, ?, ?);",
[1, 1, 6, 2, 4, 1],
)
connection.execute(
"INSERT INTO Cytoplasm VALUES (?, ?, ?, ?, ?, ?);",
[1, 1, 7, 3, 5, 1],
)
return engine
class DatabaseFrame:
"""
Create a scalable in-memory dataset from
all tables within provided database.
"""
def __init__(
self,
engine: str,
compartments: List[str] = None,
join_keys: List[str] = None,
) -> None:
self.engine = self.engine_from_str(sql_engine=engine)
self.pandas_data = self.collect_pandas_dataframes()
self.dataframes_merged = self.to_cytomining_merged(
compartments=compartments, join_keys=join_keys
)
@staticmethod
def engine_from_str(sql_engine: str) -> Engine:
"""
Helper function to create engine from a string.
Parameters
----------
sql_engine: str
filename of the SQLite database
Returns
-------
sqlalchemy.engine.base.Engine
A SQLAlchemy engine
"""
# if we don't already have the sqlite filestring, add it
if "sqlite:///" not in sql_engine:
sql_engine = f"sqlite:///{sql_engine}"
engine = create_engine(sql_engine)
return engine
def collect_sql_tables(
self,
table_name: Optional[str] = None,
) -> list:
"""
Collect a list of tables from the given engine's
database using optional table specification.
Parameters
----------
table_name: str
optional specific table name to check within database, by default None
Returns
-------
list
Returns list, and if populated, contains tuples with values
similar to the following. These may also be accessed by name
similar to dictionaries, as they are SQLAlchemy Row objects.
[('table_name'),...]
"""
# create column list for return result
table_list = []
with self.engine.connect() as connection:
if table_name is None:
# if no table name is provided, we assume all tables must be scanned
table_list = connection.execute(
"SELECT name as table_name FROM sqlite_master WHERE type = 'table';"
).fetchall()
else:
# otherwise we will focus on just the table name provided
table_list = [{"table_name": table_name}]
return table_list
def collect_sql_columns(
self,
table_name: Optional[str] = None,
column_name: Optional[str] = None,
) -> list:
"""
Collect a list of columns from the given engine's
database using optional table or column level
specification.
Parameters
----------
table_name: str
optional specific table name to check within database, by default None
column_name: str
optional specific column name to check within database, by default None
Returns
-------
list
Returns list, and if populated, contains tuples with values
similar to the following. These may also be accessed by name
similar to dictionaries, as they are SQLAlchemy Row objects.
[('table_name', 'column_name', 'column_type', 'notnull'),...]
"""
# create column list for return result
column_list = []
tables_list = self.collect_sql_tables(table_name=table_name)
with self.engine.connect() as connection:
for table in tables_list:
# if no column name is specified we will focus on all columns within the table
sql_stmt = """
SELECT :table_name as table_name,
name as column_name,
type as column_type,
[notnull]
FROM pragma_table_info(:table_name)
"""
if column_name is not None:
# otherwise we will focus on only the column name provided
sql_stmt = f"{sql_stmt} WHERE name = :col_name;"
# append to column list the results
column_list += connection.execute(
sql_stmt,
{
"table_name": str(table["table_name"]),
"col_name": str(column_name),
},
).fetchall()
return column_list
def sql_table_to_pd_dataframe(
self,
table_name: Optional[str] = None,
) -> pd.DataFrame:
"""
Read provided table as pandas dataframe
Parameters
----------
table_name: str
optional specific table name to check within database, by default None
Returns
-------
pd.DataFrame
Pandas Dataframe of the SQL table
"""
return pd.read_sql(f"select * from {table_name};", self.engine)
def collect_pandas_dataframes(
self,
table_name: Optional[str] = None,
) -> dict:
"""
Collect all tables within class's provided engine
as Pandas Dataframes.
Parameters
----------
table_name: str
optional specific table name to check within database, by default None
Returns
-------
dict
dictionary of Pandas Dataframe(s) from the SQL table(s)
"""
self.pandas_data = {}
# for each table in the database gather an pandas dataframe and
# organize within dictionary.
for table in self.collect_sql_tables(table_name=table_name):
self.pandas_data[table["table_name"]] = self.sql_table_to_pd_dataframe(
table_name=table["table_name"]
)
return self.pandas_data
@staticmethod
def df_name_prepend_column_rename(
name: str,
dataframe: pd.DataFrame,
avoid: List[str],
) -> pd.DataFrame:
"""
Create renamed columns for cytomining efforts
Parameters
----------
name: str
name to prepend during rename operation
dataframe: pd.DataFrame
table which to perform the column renaming operation
avoid: List[str]
list of keys which will be avoided during rename
Returns
-------
pd.DataFrame
Single dataframe with renamed columns
"""
dataframe.columns = [
# prepend table name to the column if the column
# name is not in the join keys, otherwise leave it
# for joining operations.
f"{name}_{x}" if x not in avoid else x
for x in list(dataframe.columns)
]
return dataframe
@staticmethod
def outer_join(
left: pd.DataFrame,
right: pd.DataFrame,
join_keys: List[str],
) -> pd.DataFrame:
"""
Create merged format for cytomining efforts.
Parameters
----------
join_keys: List[str]
list of keys which will be used for join
Returns
-------
pd.Datafame
Single joined dataset
"""
# prepare for join, adding null columns for any which are not in
# right table, and which are also not already in the left table.
left = pd.concat(
[
left,
pd.DataFrame(
{
colname: pd.Series(
data=np.nan,
index=left.index,
dtype=str(right[colname].dtype).replace("int64", "float64"),
)
for colname in right.columns
if colname not in join_keys and colname not in left.columns
},
index=left.index,
),
],
axis=1,
)
return pd.merge(
left=left,
right=right,
on=list(right.columns),
how="outer",
)
def to_cytomining_merged(
self,
compartments: List[str] = None,
join_keys: List[str] = None,
) -> pd.DataFrame:
"""
Create merged dataset for cytomining efforts.
Note: presumes the presence of an "Image" table within
datasets which is used as basis for joining operations.
Parameters
----------
compartments: List[str]
list of compartments which will be merged.
By default Cells, Cytoplasm, Nuclei.
join_keys: List[str]
list of keys which will be used for join
By default TableNumber and ImageNumber.
Returns
-------
pd.DataFrame
Single merged dataset from compartments provided.
"""
# set default join_key
if not join_keys:
join_keys = ["TableNumber", "ImageNumber"]
# set default compartments
if not compartments:
compartments = ["Cells", "Cytoplasm", "Nuclei"]
# collect table data if we haven't already
if len(self.pandas_data) == 0:
self.collect_pandas_dataframes()
# prepend table name for each table column to avoid overlaps
for table_name, table_data in self.pandas_data.items():
# only prepare names for compartments we seek to merge
if table_name in compartments:
# prepend table name for each column name except the join keys
self.pandas_data[table_name] = self.df_name_prepend_column_rename(
name=table_name,
dataframe=table_data,
avoid=join_keys,
)
# begin with image as basis
# create initial merged dataset with image table and first provided compartment
self.df_cytomining_merged = self.outer_join(
left=self.pandas_data["Image"],
right=self.pandas_data[compartments[0]],
join_keys=join_keys,
)
# complete the remaining merges with provided compartments
for compartment in compartments[1:]:
self.df_cytomining_merged = self.outer_join(
left=self.df_cytomining_merged,
right=self.pandas_data[compartment],
join_keys=join_keys,
)
return self.df_cytomining_merged
def to_parquet(self, filepath: str) -> str:
"""
Exports merged data content from database
into parquet file.
Parameters
----------
filepath: str
filepath to export to.
Returns
-------
str
location of parquet filepath
"""
self.dataframes_merged.to_parquet(filepath)
return filepath
dbf = DatabaseFrame(engine=str(database_engine_for_testing().url))
print("\nFinal result\n")
print(dbf)
print(dbf.dataframes_merged)
print(dbf.to_parquet(filepath="example.parquet"))
print(pd.read_parquet("example.parquet"))