-
Notifications
You must be signed in to change notification settings - Fork 5
Expand file tree
/
Copy pathtraining_data_utils.py
More file actions
160 lines (135 loc) · 5.15 KB
/
Copy pathtraining_data_utils.py
File metadata and controls
160 lines (135 loc) · 5.15 KB
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
import pathlib
import pandas as pd
import numpy as np
from shapely.geometry import Point
from shapely.geometry.polygon import Polygon
def get_frame_metadata(frame_details: str):
"""
get frame metadata from features samples movie details string
Parameters
----------
frame_details : str
string from one line of features samples file
ex: PLLT0010_27--ex2005_05_13--sp2005_03_23--tt17--c5___P00173_01___T00082___X0397___Y0618
Returns
-------
plate: str
plate of sample
well_num: int
well number of sample
frame: int
frame of sample
center_x: int
center x coord of cell
center_y: int
center y coord of cell
"""
plate = frame_details.split("--")[0].replace("PL", "")
well_num = int(frame_details.split("___")[1][1:6])
frame = int(frame_details.split("___")[2][1:6]) + 1
center_x = int(frame_details.split("___")[3][1:])
center_y = int(frame_details.split("___")[4][1:])
return plate, well_num, frame, center_x, center_y
def parse_outline_data(raw_outline_data: str) -> np.array:
"""
parse outline data extracted with IDR stream into numpy array format
Parameters
----------
raw_outline_data : str
string of outline data
Returns
-------
np.array
parsed outline data
"""
outline_data = []
raw_outline_data = raw_outline_data[1:-1]
raw_outline_data = raw_outline_data.split("\n ")
for coord_string in raw_outline_data:
x = int(coord_string[1:-1].split()[0])
y = int(coord_string[1:-1].split()[1])
outline_data.append([x, y])
return np.array(outline_data)
def center_in_outline(center_x: int, center_y: int, raw_outline_data: str) -> bool:
"""
use shapely library to see if a point is in the raw outline data
Parameters
----------
center_x : int
x coord of cell center
center_y : int
y coord of cell center
raw_outline_data : str
raw outline data of cell
Returns
-------
bool
whether or not coords are in outline
"""
outline_data = parse_outline_data(raw_outline_data)
point = Point(center_x, center_y)
cell_polygon = Polygon(outline_data)
return cell_polygon.contains(point)
def get_labeled_cells(
training_data: pd.DataFrame,
features_samples_path: pathlib.Path,
outlines_column: str,
) -> pd.DataFrame:
"""
get labeled cells as dataframe from all training data and features samples
Parameters
----------
training_data : pd.DataFrame
all single cell features from all frames with any labeled cells
features_samples_path : pathlib.Path
path to features samples file
outlines_column : str
name of column in training_data that has outline data
Returns
-------
pd.DataFrame
dataframe with all labeled cells
"""
with open(features_samples_path) as labels_file:
labeled_cells = []
# iterate through each sample and see if it has features in the total training data dataframe
for line in labels_file:
# get phenotpic label of cell from feature samples file line
phenotypic_class = line.strip().split("\t")[0]
# get frame info from feature samples file line
frame_details = line.strip().split("\t")[1]
plate, well_num, frame, center_x, center_y = get_frame_metadata(
frame_details
)
# get all single cell features for this particular frame
frame_cells = training_data.loc[
(training_data["Metadata_Plate"] == plate)
& (training_data["Metadata_Well"] == str(well_num))
& (training_data["Metadata_Frame"] == str(frame))
]
included = False
# see if the center coords correspond to any feature data from the frame cells
for _, row in frame_cells.iterrows():
raw_outline_data = row[outlines_column]
if center_in_outline(center_x, center_y, raw_outline_data):
full_row = pd.concat([pd.Series([phenotypic_class]), row])
labeled_cells.append(full_row)
included = True
break
# some cells not found in DP-extracted feature collection because the wells are not hosted by IDR or differences in segmentation
if not included:
print(
f"No feature data derived for cell at: {plate}, {well_num}, {frame}, {center_x}, {center_y}"
)
labeled_cells = pd.DataFrame(labeled_cells)
labeled_cells = labeled_cells.rename(
columns={labeled_cells.columns[0]: "Mitocheck_Phenotypic_Class"}
)
# rename DP column that isnt part of features
# idr_stream's merge function prefixes all data from idrstream_dp with DP__
# but for Mitocheck labeled data, we only want DP features to have DP__ prefix
# thus, we need to rename object outline column to have Metadata_ prefix instead of DP__ prefix
labeled_cells = labeled_cells.rename(
columns={"DP__Object_Outline": "Metadata_Object_Outline"}
)
return labeled_cells