Repository navigation
Expand file tree
/
Copy pathUMAPVisualisation.py
More file actions
122 lines (70 loc) · 2.6 KB
/
Copy pathUMAPVisualisation.py
File metadata and controls
122 lines (70 loc) · 2.6 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
#!/bin/python3
# -*- coding:utf-8 -*-
""" Importing librairies """
import os
import pandas as pd
import umap
import matplotlib.pyplot as plt
from sklearn.preprocessing import StandardScaler
"""Creating filename of the file"""
dataFile = "../output/CompleteData.csv"
""" Creating a counter Line"""
counterLine = 0
"""Reading the file"""
openingDataFile = open(dataFile, 'r')
"""Counting the lines"""
while(openingDataFile.readline()):
counterLine += 1
"""Closing the file"""
openingDataFile.close()
"""ReOpening the file"""
openingDataFile = open(dataFile, 'r')
"""Creating a dictionnary of data"""
dictionnaryData = {}
""" Reading header line and creating items of dictionnary"""
line = openingDataFile.readline().replace('\n', '')
lineSplitted = line.split(',')
lineSplitted[0] = "X"
listOfHeaders = lineSplitted
listOfData = []
"""Reading file and assigning data to item"""
for i in range(0, counterLine - 1):
line = openingDataFile.readline().replace('\n', '')
lineSplitted = line.split(',')
listOfData.append(lineSplitted)
openingDataFile.close()
"""Creating Dataframe"""
dataFrameOfData = pd.DataFrame(listOfData, columns= listOfHeaders)
"""Deleting variables which one don't serve anymore"""
del(listOfData)
del(listOfHeaders)
"""Saving name of Labels in a list"""
columnLabels = dataFrameOfData["Labels"]
uniqColumnLabels = list(columnLabels.drop_duplicates())
"""Selecting only data and not labels in the dataframe"""
dataFrameOfData = dataFrameOfData.iloc[:, 1:len(dataFrameOfData.columns) - 1]
"""Standardise data"""
dataFrameOfData = StandardScaler().fit_transform(dataFrameOfData)
"""Making UMAP"""
reducer = umap.UMAP()
embedding = reducer.fit_transform(dataFrameOfData)
"""Creating a new dataframe after the use of UMAP"""
dataFrameOfData = pd.DataFrame(data = embedding, columns = ["UMAP0", "UMAP1"])
"""Concatening UMAP values with Labels for each patient"""
dataFrameOfData = pd.concat([dataFrameOfData, columnLabels] , axis = 1)
"""Drawing figure"""
listOfColors = ['b', 'g', 'r', 'c', 'm']
plt.style.use("ggplot")
plt.xlabel('UMAP0', fontsize = 15)
plt.ylabel('UMAP1', fontsize = 15)
plt.title('UMAP', fontsize = 20)
for target, color in zip(uniqColumnLabels,listOfColors):
indicesToKeep = dataFrameOfData['Labels'] == target
plt.scatter(dataFrameOfData.loc[indicesToKeep, "UMAP0"],
dataFrameOfData.loc[indicesToKeep, "UMAP1"],
c = color,
s = 50)
plt.legend(uniqColumnLabels)
plt.grid()
"""Saving figure"""
plt.savefig("../figures/UMAPFigure.eps", dpi = 600, format = "eps", bbox_inches='tight')