-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathgenerate_number_dictionary.py
More file actions
109 lines (83 loc) · 3.59 KB
/
generate_number_dictionary.py
File metadata and controls
109 lines (83 loc) · 3.59 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
'''
This llibrary defines all structures that will be used in the shape analysis
'''
import os
import glob
import pandas as pd
from treelib import Tree
from utils.data_structure import read_cd_file
def add_number_dict(nucleus_file, max_time):
'''
Construct cell tree structure with cell names
:param nucleus_file: the name list file to the tree initilization
:param max_time: the maximum time point to be considered
:return cell_tree: cell tree structure where each time corresponds to one cell (with specific name)
'''
## Construct cell
# Add unregulized naming
cell_tree = Tree()
cell_tree.create_node('P0', 'P0')
cell_tree.create_node('AB', 'AB', parent='P0')
cell_tree.create_node('P1', 'P1', parent='P0')
cell_tree.create_node('EMS', 'EMS', parent='P1')
cell_tree.create_node('P2', 'P2', parent='P1')
cell_tree.create_node('P3', 'P3', parent='P2')
cell_tree.create_node('C', 'C', parent='P2')
cell_tree.create_node('P4', 'P4', parent='P3')
cell_tree.create_node('D', 'D', parent='P3')
cell_tree.create_node('Z2', 'Z2', parent='P4')
cell_tree.create_node('Z3', 'Z3', parent='P4')
# EMS
cell_tree.create_node('E', 'E', parent='EMS')
cell_tree.create_node('MS', 'MS', parent='EMS')
# Read the name excel and construct the tree with complete segCell
df_time = read_cd_file(nuc_file)
# read and combine all names from different acetrees
## Get cell number
try:
label_name_dict = pd.read_csv(r'necessary_files/name_dictionary_cmap.csv', index_col=0).to_dict()['0']
name_label_dict = {value: key for key, value in label_name_dict.items()}
except:
name_label_dict = {}
# =====================================
# dynamic update the name dictionary
# =====================================
cell_in_dictionary = list(name_label_dict.keys())
ace_pd = read_cd_file(os.path.join(nucleus_file))
ace_pd = ace_pd[ace_pd.time <= max_time]
cell_list = list(ace_pd.cell.unique())
print(len(cell_in_dictionary) , len(cell_list))
add_cell_list = list(set(cell_list) - set(cell_in_dictionary))
add_cell_list.sort()
print(add_cell_list)
if len(add_cell_list) > 0:
print("Name dictionary updated !!!")
add_number_dictionary = dict(zip(add_cell_list, range(len(cell_in_dictionary) + 1, len(cell_in_dictionary) + len(add_cell_list) + 1)))
# --------save name_label_dict csv-------------
name_label_dict.update(add_number_dictionary)
pd_number_dictionary = pd.DataFrame.from_dict(name_label_dict, orient="index")
print(pd_number_dictionary)
pd_number_dictionary.to_csv('./necessary_files/number_dictionary.csv')
# -----------save label_name_dict csv
label_name_dict_saving={value: key for key, value in name_label_dict.items()}
pd_name_dictionary = pd.DataFrame.from_dict(label_name_dict_saving, orient="index")
print(pd_name_dictionary)
pd_name_dictionary.to_csv('./necessary_files/name_dictionary.csv')
class cell_node(object):
# Node Data in cell tree
def __init__(self):
self.number = 0
self.time = 0
def set_number(self, number):
self.number = number
def get_number(self):
return self.number
def set_time(self, time):
self.time = time
def get_time(self):
return self.time
if __name__ == "__main__":
CD_folder = r"./necessary_files/CD_files"
nuc_files = sorted(glob.glob(os.path.join(CD_folder, "*.csv")))
for idx, nuc_file in enumerate(nuc_files):
add_number_dict(nuc_file, max_time=1000)