-
Notifications
You must be signed in to change notification settings - Fork 0
/
oncogen.py
99 lines (73 loc) · 2.71 KB
/
oncogen.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
# -*- coding: utf-8 -*-
"""
Created on Fri Jul 19 17:44:53 2013
@author: КТ2
"""
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.colors as mcol
from scipy import signal
from scipy.ndimage import filters
roi = np.zeros([100, 100])
class Tumor():
def __init__(self, matrixsize, tum_size, tumorosity):
if matrixsize % 2 == 0:
raise ValueError()
self.matrix = np.zeros([matrixsize, matrixsize])
self.conturs = np.zeros([matrixsize, matrixsize])
self.ms = matrixsize
self.distances = self.matrix
self.grow(np.array([matrixsize / 2, matrixsize / 2]), tum_size)
for i in range(tumorosity):
self.new_one(tum_size)
def grow(self, startpoint, radius):
x, y = startpoint
log_mrx = np.ogrid[0:self.ms, 0:self.ms] - startpoint
euclid_dist = np.sqrt(log_mrx[0] ** 2 + log_mrx[1] ** 2)
self.matrix[euclid_dist < radius] = 1
def new_one(self, maxsize=10):
x, y = [np.random.randint(0, self.ms), np.random.randint(0, self.ms)]
if self.matrix[x, y] == 1:
self.grow(np.array([x, y]), np.random.randint(1, maxsize))
else:
self.new_one()
def show_tumor(self, data1, data2):
p1 = plt.subplot(121)
p1.imshow(data1 / np.average(data1), cmap='gray')
# p1.colorbar()
p2 = plt.subplot(122)
p2.imshow(data2 / np.average(data2), cmap='gray')
#p2.colorbar()
plt.show()
def make_distanses(self):
self.matrix_1 = self.matrix
for i in range(50):
self.matrix_1 = filters.generic_filter(self.matrix_1, self.erode, 3)
self.distances += self.matrix_1
print i
# self.conturs[self.distances==1 OR self.distances==2 OR self.distances==3 OR self.distances==12]=1
self.conturs[self.conturs == 0] = np.nan
def make_perfusion(self):
dist = np.copy(self.distances)
self.perf_map = 2 / (1 + np.exp(dist + np.random.normal(0, 20, dist.shape)) ** 0.035)
# self.perf_map/=np.average(self.perf_map)
def erode(self, data):
d = data[5]
if d == 1 and 0 in data:
return 0
else:
return d
a = Tumor(101, 20, 50)
tumors = []
cm = plt.get_cmap('bwr')
# cm.set_bad(color = 'k', alpha = 1)
for i in range(2):
a = Tumor(151, np.random.randint(5, 40), 40)
a.make_distanses()
a.make_perfusion()
p = plt.subplot(5, 5, i + 1)
print np.average(a.perf_map), np.min(a.perf_map), np.max(a.perf_map)
p.imshow(filters.gaussian_filter(a.perf_map, 2), alpha=1, cmap='gist_ncar', norm=mcol.Normalize(vmin=0.5, vmax=1.3))
p.imshow(a.conturs, alpha=1, cmap=cm)
plt.axis('off')
plt.show()