h_cut = min(sx,sy) hann[ rad < h_cut/2 ] = hann_function( rad[ rad < h_cut/2 ], h_cut ) # windowing # image_fft = np.abs( np.fft.fftshift(np.fft.fft2(z2d*hann)) ) **2.0 # no windowing image_fft = np.abs( np.fft.fftshift(np.fft.fft2(z2d)) ) **2.0 freq_points = side//2 freqs = np.arange( 0,freq_points ) powers = scipy_ndimage_mean(image_fft, np.round(rad), index=freqs) autoexp, autoc, autopcov = fract.autoseeded_weighted_power_law_fit(freqs[2:-cutoff], powers[2:-cutoff], sigmas=powers[2:-cutoff]) tailexp, tailc, tailpcov = fract.autoseeded_weighted_power_law_fit(freqs[-cutoff:], powers[-cutoff:], sigmas=powers[-cutoff:]) detect_H = fract.freq_exp_to_H(autoexp) print("expected H", gen_H) print(" detect_H",detect_H) det_H_s[i] = detect_H plt.figure() plt.scatter(freqs,powers, marker=".", color="deepskyblue") plt.plot(freqs, fract.power_law(freqs, autoexp, autoc), color="red") plt.plot(freqs, fract.power_law(freqs, tailexp, tailc), color="green") # plt.plot(freqs, (10**c)*freqs**expected_freq_exp, color="red") plt.xscale("log")
freqs = freqs[2:] powers = powers[2:] sigmas = powers popt, pcov = scipy.optimize.curve_fit(pow_law, freqs, powers, sigma=sigmas, p0=(1.0, -3.0)) # popt, pcov = scipy.optimize.curve_fit(pow_law, freqs, powers, p0=(1.0,3.0)) popt # plt.plot(freqs, pow_law(freqs, *popt), color="green") autoexp, autoc, autopcov = fract.autoseeded_weighted_power_law_fit( freqs, powers, sigmas=sigmas) plt.plot(freqs, pow_law(freqs, autoexp, autoc), color="gray") print("expected H", gen_H) plt.scatter(freqs, powers, marker=".", color="deepskyblue") plt.plot(freqs, (10**c) * freqs**freq_exp, color="purple") # plt.plot(freqs, (10**c)*freqs**expected_freq_exp, color="red") plt.xscale("log") plt.yscale("log") plt.title("H = " + str(detect_H)) plt.show() plt.scatter(freqs, powers, marker=".", color="deepskyblue") plt.plot(freqs, (10**c) * freqs**freq_exp, color="purple") # plt.plot(freqs, (10**c)*freqs**expected_freq_exp, color="red")
M, N = z2d.shape if s_max == "auto": s_max = min(N, M) // 4 else: pass # (keep the passed s_max) scales_flucts = np.zeros((s_max - s_min, 2)) # remove nan values (from linear regression on too few data points) scales_flucts = scales_flucts[~np.isnan(scales_flucts).any(axis=1)] # return scales_flucts[:,0], scales_flucts[:,1] scales, flucts = scales_flucts[:, 0], scales_flucts[:, 1] dfa_H, dfa_c, pcov = fract.autoseeded_weighted_power_law_fit(scales, flucts, sigmas=scales) plt.figure() plt.scatter(scales, flucts) plt.plot(scales, fract.power_law(scales, dfa_H, dfa_c), color="springgreen", label="dfa") plt.xscale("log") plt.yscale("log") plt.legend() plt.show()
for i, s in enumerate(np.arange(smin, smax)): n_boxes = 0 area = s**(s_dim) for i_x in range(xwid // s): for i_y in range(ywid // s): submat = z2d[i_x * s:(i_x + 1) * s + 1, i_y * s:(i_y + 1) * s + 1] * z_scale # if (submat[ submat != 0].size)/(submat.size) > 0.90: n_boxes += (1 + (np.max(submat) - np.min(submat)) // s) ######### else: zero boxes nbox_s[i] = n_boxes * area scales[i] = s num = 1 b_exp, b_c, b_k, pcov = fract.autoseeded_weighted_power_law_fit( scales[num:], nbox_s[num:]) # det_H = 2 - (-b_exp +1 ) det_H = 3 + b_exp - s_dim fract_dim = 3 - det_H print("fractal dimension:", fract_dim) print("detected H:", det_H) plt.scatter(scales, nbox_s) plt.plot(scales, fract.power_law(scales, b_exp, b_c, b_k), color="red") plt.xscale("log") plt.yscale("log") plt.title("Box counting: H = " + str(det_H)) plt.show() # def to_data(name, inner_func, *args, **kwargs):
fract.power_law(freqs, freq_exp, f_c), color="red", label="fourier") plt.xscale("log") plt.yscale("log") plt.legend() plt.show() #### dfa dfa_H, dfa_c, scales3, flucts3 = fract.dfa_H(npy_points, messages=False, min_nonzero=0.99) print("dfa_H", dfa_H) ### first branch dfa_H2, c2, pcov = fract.autoseeded_weighted_power_law_fit( scales3[:17], flucts3[:17], flucts3[:17]) print("dfa_H2", dfa_H2) plt.figure() plt.scatter(scales3, flucts3) plt.plot(scales3, fract.power_law(scales3, dfa_H, dfa_c), color="springgreen", label="dfa: H =" + str(dfa_H)) plt.plot(scales3, fract.power_law(scales3, dfa_H2, c2), color="red", label="dfa: H = " + str(dfa_H2)) plt.plot(scales3, fract.power_law(scales3, fourier_H, c2 / 0.8),
# windowing? image_fft = abs( np.fft.fftshift(np.fft.fft2(z2d*hann)) ) **2 # image_fft = abs( np.fft.fftshift(np.fft.fft2(z2d)) ) **2 corr2d = scipy.signal.fftconvolve(z2d, z2d[::-1,::-1], mode="full") # plt.figure() # plt.imshow(z2d, interpolation="None") # plt.show() plt.figure() plt.imshow(corr2d, interpolation="None") plt.show() r_points = np.arange(1,max_r) autocorrs = scipy.ndimage.mean(image_fft, np.round(rad), index=r_points) exp, c, pcov = fract.autoseeded_weighted_power_law_fit(r_points, autocorrs, sigmas=autocorrs) plt.scatter(r_points, autocorrs) plt.plot(r_points, fract.power_law(r_points, exp, c), color="orange") plt.xscale("log") plt.yscale("log") plt.show() det_H = -exp/2 -1 print("gen_H", H) print("det_H", det_H)
nshifts = max(k - 1, 1) for shift_x in np.arange(0, nshifts): for shift_y in np.arange(0, nshifts): submat = z2d[shift_x::k, shift_y::k] tr_area = higuchi_area(submat, lattice_l=5 * k) # edge correction n_quads = ((submat.shape[0] - 1) * (submat.shape[1] - 1)) full_nquads = (xwid - 1) * (ywid - 1) * (1 / k**2) areas[i] += (tr_area) * (full_nquads / n_quads) / (nshifts)**2 scales[i] = k**2 h_exp, h_c, pcov = fract.autoseeded_weighted_power_law_fit(scales[30:], areas[30:], sigmas=areas[30:]) det_H = 2 - (-h_exp + 1) # return det_H, h_exp, h_c, scales, areas # det_H, h_exp, h_c, scales, areas = higuchi_H(z2d) print("fractal dimension:", -h_exp + 1) plt.scatter(scales, areas) plt.plot(scales, fract.power_law(scales, h_exp, h_c), color="red") plt.xscale("log") plt.yscale("log") plt.title("Higuchi: H = " + str(det_H)) plt.show()
# large z2d += fract.fbm2D(0.1, N=9) ## fourier fourier_H, freq_exp, f_c, freqs, powers = fract.fourier_H(np.copy(z2d), fill_with_mean=True, images=True, corr=True) print("fourier_H", fourier_H) #### dfa dfa_H, c, scales3, flucts3 = fract.dfa_H(z2d, messages=False, min_nonzero=0.99) print("dfa_H", dfa_H) # fourier plot freq_exp_2, fc2, pcov = fract.autoseeded_weighted_power_law_fit( freqs[:20], powers[:20], powers[:20]) plt.figure() plt.scatter(freqs, powers) plt.plot(freqs, fract.power_law(freqs, freq_exp, f_c), color="red", label="fourier: H =" + str(fract.freq_exp_to_H(freq_exp))) plt.plot(freqs, fract.power_law(freqs, freq_exp_2, fc2), color="springgreen", label="fourier: H =" + str(fract.freq_exp_to_H(freq_exp_2))) plt.xscale("log") plt.yscale("log")
z_segment += x_segment * mx + y_segment * my from scipy.ndimage import rotate npy_points = rotate(npy_points, 30) # plt.figure() # plt.imshow(npy_points) # plt.show() ## fourier fourier_H, freq_exp, f_c, freqs, powers = fract.fourier_H(np.copy(npy_points), fill_with_mean=True, images=True, corr=True) print("fourier_H", fourier_H) freq_exp_2, fc2, pcov = fract.autoseeded_weighted_power_law_fit( freqs[2:20], powers[2:20], sigmas=powers[2:20]) fourier_H2 = fract.freq_exp_to_H(freq_exp_2) plt.figure() plt.scatter(freqs, powers) plt.plot(freqs, fract.power_law(freqs, freq_exp, f_c), color="red", label="fourier: H =" + str(fourier_H)) plt.plot(freqs, fract.power_law(freqs, freq_exp_2, fc2), color="springgreen", label="fourier: H =" + str(fourier_H2)) plt.xscale("log") plt.yscale("log") plt.legend()
# plt.show() scales_out_real, flucts_out_real = dma_1(z2d) # plt.figure() # plt.scatter(scales_out_real, flucts_out_real ) # plt.show() scales_out = scales_out_real flucts_out = np.sqrt(flucts_out_real) scales_out = scales_out[~np.isnan(flucts_out)] flucts_out = flucts_out[~np.isnan(flucts_out)] import matplotlib.pyplot as plt dma_1, dma_c, pcov = fract.autoseeded_weighted_power_law_fit( scales_out[:25], flucts_out[:25], sigmas=flucts_out[:25]) np.min(scales_out) print("dma_1", dma_1) # ### branches # scale_f1 = 17 scale_f2 = 25 # dfa_H2, c2, pcov = fract.autoseeded_weighted_power_law_fit(scales_out[:scale_f1], flucts_out[:scale_f1], flucts_out[:scale_f1]) dfa_H2, c2, pcov = fract.autoseeded_weighted_power_law_fit( scales_out[scale_f2:], flucts_out[scale_f2:], flucts_out[scale_f2:]) # print("dfa_H2", dfa_H2) plt.figure() plt.scatter(scales_out, flucts_out)