In [153]:
import numpy as np

In [154]:
X = np.random.random((3, 4, 4))

In [155]:
X_reshape = X.reshape((X.shape[0], -1))

In [156]:
X_sort = np.sort(X_reshape, axis=1)

In [168]:
import math
v_frac = [0.3, 0.2,0.3,0.2]
X_segs = np.zeros((X_reshape.shape))
if sum(v_frac)!=1.0:
    raise RuntimeError("Volume fractions do not add up to 1")
v_frac1 = 0.3
for i in range(1,len(v_frac)):
    v = sum(v_frac[0:i])
    print v
    length = X_sort.shape[1]
    ind = int(math.floor(v*length))
    seg = X_sort[:, ind-1]
    print seg
    X_seg = X_reshape >= seg[:, None]
    X_segs = X_segs+X_seg
    
#X_seg = X_seg1.astype(int)+X_seg2.astype(int)


0.3
[ 0.11802031  0.51234915  0.18613017]
0.5
[ 0.25428813  0.7046437   0.31473277]
0.8
[ 0.66595141  0.74683381  0.57860998]

In [162]:
X_segs


Out[162]:
array([[ 2.,  0.,  1.,  2.,  1.,  3.,  2.,  0.,  0.,  3.,  1.,  1.,  3.,
         2.,  3.,  3.],
       [ 1.,  3.,  2.,  0.,  1.,  2.,  3.,  0.,  2.,  3.,  3.,  1.,  0.,
         1.,  2.,  3.],
       [ 3.,  3.,  3.,  0.,  2.,  1.,  1.,  2.,  2.,  3.,  3.,  1.,  0.,
         0.,  2.,  1.]])

In [152]:
X_seg


Out[152]:
array([[False,  True,  True, ...,  True,  True,  True],
       [ True,  True,  True, ..., False,  True, False],
       [ True, False, False, ..., False, False, False]], dtype=bool)

In [116]:
import math
v_frac = 0.5
length = X_sort.shape[1]
ind = int(math.floor(v_frac*length))
ind


Out[116]:
5000

In [117]:
seg = X_sort[:, ind-1]

In [121]:
#X_seg = np.zeros(shape = X_reshape.shape)

#for i in range(X.shape[0]):
#    X_seg[i,:] = X_reshape[i,:] >= seg[i]
#print X_seg
X_seg = X_reshape >= seg[:, None]

In [122]:
X_seg.astype(int)


Out[122]:
array([[1, 1, 0, ..., 1, 0, 0],
       [0, 0, 0, ..., 0, 1, 0],
       [0, 0, 0, ..., 1, 0, 0]])

In [123]:
np.sum(X_seg, axis=1)/float(X_reshape.shape[1])


Out[123]:
array([ 0.5001,  0.5001,  0.5001])

In [76]:
%matplotlib inline
%load_ext autoreload
%autoreload 2

import numpy as np
import matplotlib.pyplot as plt
from pymks.datasets import make_microstructure
n_samples, n_phases = 3, 3
size, grain_size = (100, 100), (10, 1)
v_frac = [0.2,0.8]
sigma1 = 100
sigma2 = 20
X = make_microstructure(n_samples=n_samples, size=size,n_phases=n_phases, grain_size=grain_size,seed=1, v_frac = v_frac, 
                        sigma=sigma1)
Y = make_microstructure(n_samples=n_samples, size=size,n_phases=n_phases, grain_size=grain_size,seed=1, v_frac = v_frac,
                           sigma=sigma2) 
X_reshape = X.reshape(n_samples, -1)


The autoreload extension is already loaded. To reload it, use:
  %reload_ext autoreload
sigma =100
variance = 50.0410458844
Index = 2050.04104588
sigma =20
variance = 10.0082091769
Index = 2010.00820918

In [72]:
X_reshape


Out[72]:
array([[1, 1, 1, ..., 0, 0, 1],
       [1, 1, 1, ..., 1, 1, 1],
       [1, 1, 1, ..., 1, 1, 1]])

In [215]:
np.sum(X_reshape, axis=1)/float(X.shape[1]*X.shape[2])


Out[215]:
array([ 2.,  2.,  2.])

In [216]:
np.count_nonzero(X_reshape)/3


Out[216]:
8000

In [77]:
from pymks.tools import draw_microstructures
draw_microstructures(X)



In [78]:
draw_microstructures(Y)



In [12]:
ind = np.array(range(1, 6))
ind


Out[12]:
array([1, 2, 3, 4, 5])

In [75]:
np.allclose(X, Y)


Out[75]:
False

In [ ]:


In [36]:
import numpy as np
np.rint(2 * np.random.randn(len(ind)))


Out[36]:
array([ 3.,  1., -1.,  2.,  0.])

In [68]:
np.rint(10*np.random.randn())


Out[68]:
16.0

In [ ]:


In [ ]: