-
Notifications
You must be signed in to change notification settings - Fork 52
/
Copy pathbioapps_tools.py
142 lines (96 loc) · 3.62 KB
/
bioapps_tools.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
# coding: utf-8
import pandas as pd
import csv
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt
import re
# from ipy_progressbar import ProgressBar
import progressbar
try:
from IPython.display import display
from ipywidgets import FloatProgress
except:
print('Could not import IPython.Display or ipywidgets')
import os
def get_well_all_parameters(df, wellid,
colname='all',
wellID_key='WellID',
rowID_key='RowID',
colID_key='ColumnID'):
"""
Gets all or specific columns for specific well.
If the colname was specified, only this specific column will be returned.
:param df: original dataframe
:param wellid: wellID, e.g. 'B4'
:param colname: name of column(s) from the original dataframe to be extracted
:return: new_df - new dataframe containing only data for a specific well
"""
new_df = df.loc[df[wellID_key] == wellid]
if colname != 'all':
new_df = new_df[[wellID_key, rowID_key, colID_key, colname]]
return new_df
def get_well_row(df, rowid, rowID_key='RowID'):
"""
This function extracts all data based on the row index.
:param df: original dataframe containing all wells
:param rowid: index of row to be extracted
:return: df_row - dataframe that only contains data for a specific row af a wellplate
"""
rowindex = convert_row_index(rowid)
df_row = df.loc[df[rowID_key] == rowindex]
return df_row
def rename_columns(dfs, paramlist, verbose=False):
for i in range(0, len(paramlist)):
# rename the columns with measured parameters and correct types
if verbose:
print('Renamed : ', dfs.columns[i], ' to ', paramlist[i])
try:
dfs.rename(columns={dfs.columns[i]: paramlist[i]}, inplace=True)
except:
print('Column not find inside table for renaming. Doing nothing.')
return dfs
def addWellinfoColumns(dataframe):
# add WellID, RowID and ColumnID to the existing dataframe
dataframe.insert(0, 'WellID', 'A1')
dataframe.insert(1, 'RowID', 1)
dataframe.insert(2, 'ColumnID', 1)
return dataframe
def remove_units(df):
# remove units from table
df.drop([0], inplace=True)
return df
def convert_dec_sep(df, np):
for id in range(np, len(df.columns)):
#print('Index: ', id)
try:
df.iloc[:, id] = df.iloc[:, id].str.replace(',', '.').astype('float')
except:
print('No correction of types possible for column: ', df.columns[id])
return df
def check_separator(csvfile):
reader = pd.read_csv(csvfile, sep=None, engine='python', iterator=True)
sep = reader._engine.data.dialect.delimiter
reader.close()
return sep
def determine_plotgrid(num_parameter, columns=2):
if np.mod(num_parameter, columns) == 0:
plotrows = np.int(num_parameter /columns)
empty = False
if np.mod(num_parameter, columns) == 1:
plotrows = np.int(num_parameter / columns) + 1
empty = True
plotgrid = [plotrows, columns]
return plotgrid, empty
def get_csvdata(filename, num_nonmp=2):
# check the used separator
sep = check_separator(filename)
# read the CSV table containing all the single object data
df = pd.read_csv(filename, sep=sep)
# get headers and number of measurement parameters
headers = df.head(0)
# remove rows with units from datafrane
df = remove_units(df)
# convert decimal separators to point "."
df = convert_dec_sep(df, num_nonmp)
return df, headers, sep