-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathData.lua
156 lines (136 loc) · 3.6 KB
/
Data.lua
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
local M={}
require 'paths';
torch.manualSeed(123)
local function LoadCSV(proteinName)
local file = io.open(proteinName, 'r')
local header = file:read()
local dat = {}
for l in file:lines() do
local row = l:split(',')
table.insert(dat,row)
end
return torch.Tensor(dat)
end
function M.Maper(cancerType)
local pros = paths.dir('Data/' .. cancerType)
os.execute("mkdir " .. "Data/" .. cancerType .. "_Map")
table.remove(pros,1)
table.remove(pros,1)
table.sort(pros)
local n = #pros
for i=1,n,3 do
local tens = {}
print(pros[i])
for j=1,3 do
local map = torch.zeros(16,200,200)
gene = LoadCSV('Data/' .. cancerType .. '/' .. pros[i+j-1])
atoms = gene:size(1)
for a=1,atoms do
--print(gene[a][17] , gene[a][18])
map[{ {}, {math.min(gene[a][17]+1,200)}, {math.min(gene[a][18]+1,200)} }] = gene[{ {a}, {1,16} }]
end
tens[j] = map:clone()
end
torch.save('Data/' .. cancerType .. '_Map/' .. pros[i]:sub(1,4) .. '.dat',tens)
end
end
local function Shuffle(...)
local args = {...}
local n = #args[1]
local count = 0
count=(torch.type(n)=='number' and n or n[1])
for t=1,count do
local k = math.random(count)
for i,v in ipairs{...} do
v[t],v[k] = v[k],v[t]
end
end
return {...}
end
function M.DataCollect(positive,negative,neutral)
local posProtein = paths.dir('Data/' .. positive)
table.remove(posProtein,1)
table.remove(posProtein,1)
local negProtein = paths.dir('Data/' .. negative)
table.remove(negProtein,1)
table.remove(negProtein,1)
--local nutProtein = paths.dir('Data/' .. neutral)
--table.remove(nutProtein,1)
--table.remove(nutProtein,1)
local label = torch.zeros(#posProtein+#negProtein)
local allProtein = {}
local index=1
for i=1,#posProtein do
table.insert(allProtein, 'Data/' .. positive .. '/' .. posProtein[i])
label[index]=1
index=index+1
end
for i=1,#negProtein do
table.insert(allProtein, 'Data/' .. negative .. '/' .. negProtein[i])
end
--for i=1,#nutProtein do
-- table.insert(allProtein, 'Data/' .. neutral .. '/' .. nutProtein[i])
--end
_ = Shuffle(allProtein,label)
return allProtein,label
end
function M.Slice(tbl, first, last, step)
local sliced = {}
for i = first or 1, last or #tbl, step or 1 do
sliced[#sliced+1] = tbl[i]
end
return sliced
end
function M.Fetch(names,h,w)
local pros = torch.Tensor(3,#names,16,h,w)
local threads = require 'threads'
local pool = threads.Threads(4)
for i=1,#names do
pool:addjob(function()
local temp = torch.load(names[i])
return i,temp
end,
function(id,pr)
--print(pr,pros:size())
pros[{ {1}, {id}, {}, {}, {} }] = pr[1]:clone()
pros[{ {2}, {id}, {}, {}, {} }] = pr[2]:clone()
pros[{ {3}, {id}, {}, {}, {} }] = pr[3]:clone()
end
)
end
pool:synchronize()
pool:terminate()
return pros
end
function M.Dataset(proteins,labels,meani,stdvi)
local dataset = {}
dataset.data = proteins
dataset.label = labels
setmetatable(dataset,{__index = function(t, i)
return {
t.data[i],
t.label[i]
}
end}
);
function dataset:size()
return self.data:size(1)
end
local mean = torch.zeros(16)
local stdv = torch.zeros(16)
if meani~=nil then
for i=1,16 do
dataset.data:select(3, i):add(-meani[i])
dataset.data:select(3, i):div(stdvi[i])
end
else
for i=1,16 do
mean[i] = dataset.data:select(3, i):mean()
dataset.data:select(3, i):add(-mean[i])
stdv[i] = dataset.data:select(3, i):std()
dataset.data:select(3, i):div(stdv[i])
end
end
return dataset,mean,stdv
end
return M