import numpy as np from matplotlib import pyplot import rft1d #(0) Set parameters: np.random.seed(12345) nResponses = 5 nNodes = 101 FWHM = 20.0 ### create a boolean mask: nodes = np.array([True]*nNodes) #nothing masked out nodes[20:30] = False #this region will be masked out nodes[60:80] = False #this region will be masked out #(1) Generate Gaussian 1D fields: y = rft1d.randn1d(nResponses, nodes, FWHM) #(2) Plot: pyplot.close('all') pyplot.plot(y.T) pyplot.plot([0,100], [0,0], 'k:') pyplot.xlabel('Field position', size=16) pyplot.ylabel('z', size=20) pyplot.title('Broken (piecewise continuous) random fields', size=20) pyplot.show()
from matplotlib import pyplot import rft1d eps = np.finfo(float).eps #smallest float #(0) Set parameters: np.random.seed(0) nResponses = 2000 nNodes = 101 FWHM = 8.5 interp = True wrap = True heights = [2.0, 2.2, 2.4] c = 2 ### generate data: y = rft1d.randn1d(nResponses, nNodes, FWHM) calc = rft1d.geom.ClusterMetricCalculator() rftcalc = rft1d.prob.RFTCalculator(STAT='Z', nodes=nNodes, FWHM=FWHM) #(1) Maximum region size: K0 = np.linspace(eps, 8, 21) K = [[calc.cluster_extents(yy, h, interp, wrap) for yy in y] for h in heights] ### compute number of upcrossings above a threshold: C = np.array([[[sum([kkk >= k0 for kkk in kk]) for kk in k] for k in K] for k0 in K0]) P = np.mean(C >= c, axis=2).T P0 = np.array([[rftcalc.p.set(c, k0, h) for h in heights] for k0 in K0 / FWHM]).T #(2) Plot results: pyplot.close('all')
def here_expected_ec_1d(Q, FWHM, u): return (Q-1)/FWHM * sqrt(4*log(2)) / (2*pi) * exp(-0.5*(u*u)) ### actual EC: def here_ec(b): L,n = rft1d.geom.bwlabel(b) return n ### simulate: np.random.seed(0) nNodes = 101 FWHM = [5, 10, 25] nIterations = 100 #increase this to 1000 to reproduce the results from the paper heights = np.linspace(0, 4, 21) ### simulate random fields and compute their EC: EC = [] for W in FWHM: y = rft1d.randn1d(nIterations, nNodes, W, pad=True) ec = np.array([[here_ec(yy>u) for u in heights] for yy in y]).mean(axis=0) EC.append(ec) EC = np.array(EC) ### expected EC: E0,E1 = [],[] for W in FWHM: e0 = np.array([here_expected_ec_0d(u) for u in heights]) e1 = np.array([here_expected_ec_1d(nNodes, W, u) for u in heights]) E0.append(e0) E1.append(e1) ### plot: pyplot.figure(3) ax = pyplot.axes([0.11,0.14,0.86,0.84]) colors = ['b', 'g', 'r'] for color,e0,e1,ec in zip(colors, E0, E1, EC):
np.random.seed(123456789) nResponsesA = 5 nResponsesB = 5 nNodes = 101 FWHM = 10.0 nIterations = 5000 ### derived parameters: nA,nB = nResponsesA, nResponsesB nTotal = nA + nB df = nA + nB - 2 #(1) Generate Gaussian 1D fields, compute test stat, store field maximum: T = [] for i in range(nIterations): y = rft1d.randn1d(nTotal, nNodes, FWHM) #compute test stat: yA,yB = y[:nA], y[nA:] mA,mB = yA.mean(axis=0), yB.mean(axis=0) sA,sB = yA.std(ddof=1, axis=0), yB.std(ddof=1, axis=0) s = np.sqrt( ((nA-1)*sA*sA + (nB-1)*sB*sB) / df ) t = (mA-mB) / ( s *np.sqrt(1.0/nA + 1.0/nB)) T.append( t.max() ) T = np.asarray(T) #(2) Survival functions: heights = np.linspace(2, 5, 21) sf = np.array( [ (T>h).mean() for h in heights] ) sfE = rft1d.t.sf(heights, df, nNodes, FWHM) #theoretical sf0D = rft1d.t.sf0d(heights, df) #theoretical (0D)
### actual EC: def here_ec(b): L, n = rft1d.geom.bwlabel(b) return n ### simulate: np.random.seed(0) nNodes = 101 FWHM = [5, 10, 25] nIterations = 100 #increase this to 1000 to reproduce the results from the paper heights = np.linspace(0, 4, 21) ### simulate random fields and compute their EC: EC = [] for W in FWHM: y = rft1d.randn1d(nIterations, nNodes, W, pad=True) ec = np.array([[here_ec(yy > u) for u in heights] for yy in y]).mean(axis=0) EC.append(ec) EC = np.array(EC) ### expected EC: E0, E1 = [], [] for W in FWHM: e0 = np.array([here_expected_ec_0d(u) for u in heights]) e1 = np.array([here_expected_ec_1d(nNodes, W, u) for u in heights]) E0.append(e0) E1.append(e1) ### plot: pyplot.figure(3) ax = pyplot.axes([0.11, 0.14, 0.86, 0.84]) colors = ['b', 'g', 'r']
''' Random field generation using rft1d.randn1d Note: When FWHM gets large (2FWHM>nNodes), the data should be padded using the *pad* keyword. ''' import numpy as np from matplotlib import pyplot import rft1d #(0) Set parameters: np.random.seed(12345) nResponses = 5 nNodes = 101 FWHM = 20.0 #(1) Generate Gaussian 1D fields: y = rft1d.randn1d(nResponses, nNodes, FWHM, pad=False) #(2) Plot: pyplot.close('all') pyplot.plot(y.T) pyplot.plot([0, 100], [0, 0], 'k:') pyplot.xlabel('Field position', size=16) pyplot.ylabel('z', size=20) pyplot.title('Random (Gaussian) fields', size=20) pyplot.show()
#(0) Set parameters: np.random.seed(0) nResponses = 10000 nNodes = 101 FWHM = 13.1877 ### generate a field mask: nodes_full = np.array([True]*nNodes) #nothing masked out nodes = nodes_full.copy() nodes[20:45] = False #this region will be masked out nodes[60:80] = False #(1) Generate Gaussian 1D fields and extract maxima:: y_full = rft1d.randn1d(nResponses, nNodes, FWHM) np.random.seed(0) y_broken = rft1d.randn1d(nResponses, nodes, FWHM) ymax_full = y_full.max(axis=1) ymax_broken = np.nanmax(y_broken, axis=1) #(2) Survival functions for field maximum: heights = np.linspace(2.0, 4, 21) sf_full = np.array( [ (ymax_full>=h).mean() for h in heights] ) sf_broken = np.array( [ (ymax_broken>=h).mean() for h in heights] ) ### expected: sfE_full = rft1d.norm.sf(heights, nNodes, FWHM) #theoretical sfE_broken = rft1d.norm.sf(heights, nodes, FWHM) #theoretical
nNodes = 101 FWHM = [5.0, 10.0, 25.0] nResponses = 20 heights = np.linspace(1.0, 3.0, 11) ### generate mask: nodes = np.array([True] * nNodes) nodes[20:35] = False nodes[60:80] = False ### assemble the field's geometric characteristics: nSegments = rft1d.geom.bwlabel(nodes)[ 1] #number of unbroken field segments (here: 3) nNodesTotal = nodes.sum() #number of nodes in the unbroken field segments fieldSize = nNodesTotal - nSegments #(1) Generate broken random fields: y0 = rft1d.randn1d(nResponses, nodes, FWHM[0], pad=True) y1 = rft1d.randn1d(nResponses, nodes, FWHM[1], pad=True) y2 = rft1d.randn1d(nResponses, nodes, FWHM[2], pad=True) #(2) Plot results: pyplot.close('all') axx = np.linspace(0.04, 0.695, 3) AX = [pyplot.axes([xx, 0.18, 0.29, 0.8]) for xx in axx] ax0, ax1, ax2 = AX ### plot fields: colors = scalar2color(range(nResponses + 3), cmap=cm.RdPu) [ax0.plot(yy, color=color) for yy, color in zip(y0, colors)] [ax1.plot(yy, color=color) for yy, color in zip(y1, colors)] [ax2.plot(yy, color=color) for yy, color in zip(y2, colors)] ### adjust axes: pyplot.setp(AX, xlim=(0, 100), ylim=(-3.8, 3.8))
### expectations for unbroken fields: ec0u = here_ec_0d(u, 1) ec1u = here_ec_1d(u, fieldSize, W) EC0.append( ec0 + ec1 ) EC0u.append( ec0u + ec1u ) expectedEC.append(EC0) expectedECunbroken.append(EC0u) expectedEC = np.array(expectedEC) expectedECunbroken = np.array(expectedECunbroken) #(2) Simulate broken random fields and compute their EC: EC = [] for W in FWHM: y = rft1d.randn1d(nIterations, nodes, W, pad=True) ec = np.array([[here_ec_actual(yy, u) for u in heights] for yy in y]).mean(axis=0) EC.append(ec) EC = np.array(EC) #(3) Plot results: pyplot.close('all') ax = pyplot.axes([0.11,0.14,0.86,0.84]) colors = ['b', 'r', 'g'] for color,ec0,ec0u,ec in zip(colors, expectedEC, expectedECunbroken, EC): ax.plot(heights, ec0u, '-', lw=1, color=color) #expectation for an unbroken field ax.plot(heights, ec0, ':', lw=2, color=color) #expectation for broken field ax.plot(heights, ec, 'o', color=color, markersize=5) #simulated data
Note: When FWHM gets large (2FWHM>nNodes), the data should be padded using the *pad* keyword. ''' import numpy as np from matplotlib import pyplot import rft1d #(0) Set parameters: np.random.seed(12345) nResponses = 5 nNodes = 101 FWHM = 20.0 #(1) Generate Gaussian 1D fields: y = rft1d.randn1d(nResponses, nNodes, FWHM, pad=False) #(2) Plot: pyplot.close('all') pyplot.plot(y.T) pyplot.plot([0,100], [0,0], 'k:') pyplot.xlabel('Field position', size=16) pyplot.ylabel('z', size=20) pyplot.title('Random (Gaussian) fields', size=20) pyplot.show()
### actual EC: def here_ec(b): L, n = rft1d.geom.bwlabel(b) return n #(0) Simulation: np.random.seed(0) nNodes = 101 FWHM = [5, 10, 25] nIterations = 100 #increase this to 1000 to reproduce the results from the paper heights = np.linspace(0, 4, 21) ### simulate random fields and compute their EC: EC = [] for W in FWHM: y = rft1d.randn1d(nIterations, nNodes, W, pad=True) ec = np.array([[here_ec(yy > u) for u in heights] for yy in y]).mean(axis=0) EC.append(ec) EC = np.array(EC) #(1) Expected EC: E0, E1 = [], [] for W in FWHM: e0 = np.array([here_expected_ec_0d(u) for u in heights]) e1 = np.array([here_expected_ec_1d(nNodes, W, u) for u in heights]) E0.append(e0) E1.append(e1) #(2) Plot results: pyplot.close('all')
nNodes = 101 FWHM = [5.0, 10.0, 25.0] nResponses = 20 heights = np.linspace(1.0, 3.0, 11) ### generate mask: nodes = np.array([True]*nNodes) nodes[20:35] = False nodes[60:80] = False ### assemble the field's geometric characteristics: nSegments = rft1d.geom.bwlabel(nodes)[1] #number of unbroken field segments (here: 3) nNodesTotal = nodes.sum() #number of nodes in the unbroken field segments fieldSize = nNodesTotal - nSegments #(1) Generate broken random fields: y0 = rft1d.randn1d(nResponses, nodes, FWHM[0], pad=True) y1 = rft1d.randn1d(nResponses, nodes, FWHM[1], pad=True) y2 = rft1d.randn1d(nResponses, nodes, FWHM[2], pad=True) #(2) Plot results: pyplot.close('all') axx = np.linspace(0.04, 0.695, 3) AX = [pyplot.axes([xx,0.18,0.29,0.8]) for xx in axx] ax0,ax1,ax2 = AX ### plot fields: colors = scalar2color(list(range(nResponses+3)), cmap=cm.RdPu) [ax0.plot(yy, color=color) for yy,color in zip(y0,colors)]
from matplotlib import pyplot import rft1d #(0) Set parameters: np.random.seed(0) nResponses = 10000 nNodes = 101 FWHM = 13.1877 ### generate a field mask: nodes_full = np.array([True] * nNodes) #nothing masked out nodes = nodes_full.copy() nodes[20:45] = False #this region will be masked out nodes[60:80] = False #(1) Generate Gaussian 1D fields and extract maxima:: y_full = rft1d.randn1d(nResponses, nNodes, FWHM) np.random.seed(0) y_broken = rft1d.randn1d(nResponses, nodes, FWHM) ymax_full = y_full.max(axis=1) ymax_broken = np.nanmax(y_broken, axis=1) #(2) Survival functions for field maximum: heights = np.linspace(2.0, 4, 21) sf_full = np.array([(ymax_full >= h).mean() for h in heights]) sf_broken = np.array([(ymax_broken >= h).mean() for h in heights]) ### expected: sfE_full = rft1d.norm.sf(heights, nNodes, FWHM) #theoretical sfE_broken = rft1d.norm.sf(heights, nodes, FWHM) #theoretical #(3) Plot results: pyplot.close('all')