-
Notifications
You must be signed in to change notification settings - Fork 0
/
2.py
208 lines (159 loc) · 5.81 KB
/
2.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
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
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
# ---
# jupyter:
# jupytext:
# formats: ipynb,py:percent
# text_representation:
# extension: .py
# format_name: percent
# format_version: '1.3'
# jupytext_version: 1.11.4
# kernelspec:
# display_name: Python 3
# language: python
# name: python3
# ---
# %%
# use cdf probability for generator in 1D case
# %%
# %load_ext autoreload
# %autoreload 2
# %%
import porespy as ps
import numpy as np
import scipy as sp
from matplotlib import cm
import matplotlib.pyplot as plt
import seaborn as sns
import helper
# %%
im_size = 128*128
porosity = 0.3
img = helper.one_D_generator(im_size, sigma=4, porosity=porosity, seed=0)
plt.figure(figsize=(20,5))
plt.plot(img[:1024])
print(f'porosity: { helper.image_porosity(img) }')
# %%
segments_lengths = helper.segments_lengths_from_image(img)[1]
# print(segments_lengths)
segments_hists = {}
segments_kdes = {}
segments_pdfs = {}
segments_cdfs = {}
fig, axes = plt.subplots(2, 2, figsize=(20, 10))
pores_lengths = segments_lengths['pores']
p_hist, p_edges = helper.hist_of_lengths(pores_lengths)
p_kde, p_pdf, p_cdf, p_linspace = helper.kde_of_lengths(pores_lengths)
segments_hists['pores'] = p_hist
segments_kdes['pores'] = p_kde
segments_pdfs['pores'] = p_pdf
segments_cdfs['pores'] = p_cdf
axes[0, 0].bar(p_edges[:-1], p_hist, width=np.diff(p_edges), edgecolor="black", align="edge")
axes[0, 0].plot(p_linspace, p_pdf, c='red')
axes[0, 1].plot([p_cdf(x) for x in p_linspace])
axes[0, 1].set_ylim([0, 1])
solid_lengths = segments_lengths['solid']
s_hist, s_edges = helper.hist_of_lengths(solid_lengths)
s_kde, s_pdf, s_cdf, s_linspace = helper.kde_of_lengths(solid_lengths)
segments_hists['solid'] = s_hist
segments_kdes['solid'] = s_kde
segments_pdfs['solid'] = s_pdf
segments_cdfs['solid'] = s_cdf
axes[1, 0].bar(s_edges[:-1], s_hist, width=np.diff(s_edges), edgecolor="black", align="edge")
axes[1, 0].plot(s_linspace, s_pdf, c='red')
axes[1, 1].plot([s_cdf(x) for x in s_linspace])
axes[1, 1].set_ylim([0, 1])
# print(segments_hists)
# %%
synt_img = np.array([])
kdes = segments_kdes
line_length = im_size
# line_length = 100_000
while len(synt_img) < line_length:
segment = helper.get_sample(kdes['pores'])
synt_img = np.append(synt_img, np.zeros(segment))
segment = helper.get_sample(kdes['solid'])
synt_img = np.append(synt_img, np.ones(segment))
synt_img = synt_img[:line_length]
plt.figure(figsize=(20, 5))
plt.plot(synt_img[:1024])
print(f'porosity: { helper.image_porosity(synt_img) }')
# %%
t_segments_lengths = helper.segments_lengths_from_image(np.array([synt_img]))[0]
# print(t_segments_lengths)
t_segments_hists = {}
fig, axes = plt.subplots(2, 2, figsize=(20, 10))
pores_lengths = t_segments_lengths['pores']
p_hist, p_edges = helper.hist_of_lengths(pores_lengths)
p_kde, p_pdf, p_cdf, p_linspace = helper.kde_of_lengths(pores_lengths)
t_segments_hists['pores'] = p_hist
axes[0, 0].bar(p_edges[:-1], p_hist, width=np.diff(p_edges), edgecolor="black", align="edge")
axes[0, 0].plot(p_linspace, p_pdf, c='red')
axes[0, 1].plot([p_cdf(x) for x in p_linspace])
axes[0, 1].set_ylim([0, 1])
solid_lengths = t_segments_lengths['solid']
s_hist, s_edges = helper.hist_of_lengths(solid_lengths)
s_kde, s_pdf, s_cdf, s_linspace = helper.kde_of_lengths(solid_lengths)
t_segments_hists['solid'] = s_hist
axes[1, 0].bar(s_edges[:-1], s_hist, width=np.diff(s_edges), edgecolor="black", align="edge")
axes[1, 0].plot(s_linspace, s_pdf, c='red')
axes[1, 1].plot([s_cdf(x) for x in s_linspace])
axes[1, 1].set_ylim([0, 1])
# print(segments_hists)
# %%
helper.compare_lengths(segments_lengths, t_segments_lengths)
helper.compare_hists(segments_hists, t_segments_hists)
# %%
f = open('/Users/grimax/Desktop/log.txt', 'w')
f.close()
test_img = np.zeros(im_size)
last_seg = np.array([], dtype=np.int32)
for i in np.arange(im_size):
if i == 0:
test_img[i] = sp.stats.bernoulli.rvs(porosity)
last_seg = np.append(last_seg, test_img[i])
else:
f = open('/Users/grimax/Desktop/log.txt', 'a')
f.write(f'i: {i}\n')
f.write(f'last_seg: {last_seg}\n')
kind = 'pores' if 0 in last_seg else 'solid'
p = 1 - segments_cdfs[kind](len(last_seg))
result = sp.stats.bernoulli.rvs(1 - p if kind == 'pores' else p)
# result = (1 - p if kind == 'pores' else p) > 0.5
test_img[i] = result
boundary = result != last_seg[0]
last_seg = np.array([result]) if boundary else np.append(last_seg, [result])
f.write(f'p: {p}\n')
f.write(f'result: {result}\n')
f.write(f'\n')
f.close()
print(test_img)
# %%
plt.figure(figsize=(20, 5))
plt.plot(test_img[:1024])
print(f'porosity: { helper.image_porosity(test_img) }')
# %%
t2_segments_lengths = helper.segments_lengths_from_image(np.array([test_img]))[0]
# print(t2_segments_lengths)
t2_segments_hists = {}
fig, axes = plt.subplots(2, 2, figsize=(20, 10))
pores_lengths = t2_segments_lengths['pores']
p_hist, p_edges = helper.hist_of_lengths(pores_lengths)
p_kde, p_pdf, p_cdf, p_linspace = helper.kde_of_lengths(pores_lengths)
t2_segments_hists['pores'] = p_hist
axes[0, 0].bar(p_edges[:-1], p_hist, width=np.diff(p_edges), edgecolor="black", align="edge")
axes[0, 0].plot(p_linspace, p_pdf, c='red')
axes[0, 1].plot([p_cdf(x) for x in p_linspace])
axes[0, 1].set_ylim([0, 1])
solid_lengths = t2_segments_lengths['solid']
s_hist, s_edges = helper.hist_of_lengths(solid_lengths)
s_kde, s_pdf, s_cdf, s_linspace = helper.kde_of_lengths(solid_lengths)
t2_segments_hists['solid'] = s_hist
axes[1, 0].bar(s_edges[:-1], s_hist, width=np.diff(s_edges), edgecolor="black", align="edge")
axes[1, 0].plot(s_linspace, s_pdf, c='red')
axes[1, 1].plot([s_cdf(x) for x in s_linspace])
axes[1, 1].set_ylim([0, 1])
# print(segments_hists)
# %%
helper.compare_lengths(segments_lengths, t2_segments_lengths)
helper.compare_hists(segments_hists, t2_segments_hists)
# %%