def cut_profile(image, contour_H, treshold=0.0, invert=1):
    n, m = image.shape
    side = max(n, m)
    N = np.ceil(np.log2(side - 1)).astype(int)
    contour_im = fract.fbm2D(contour_H, N=N)
    contour_im[invert * contour_im >= invert * treshold] = 0.0
    contour_im[invert * contour_im < invert * treshold] = 1.0
    return np.multiply(contour_im, image)
Exemple #2
0
    # A = np.vstack([np.ones(len(s_log)), s_log]).T
    H, c, r_value, p_value, std_err = scipy.stats.linregress(s_log, f_log)
    # print("H =  ",H)
    return H, c, scales_flucts[:, 0], scales_flucts[:, 1]


# plot


def pow_law(x, a, b):
    return a * x**b


if __name__ == '__main__':
    ex_z2d = fract.fbm2D(0.6, 10)

    H, c, scales, flucts = profile_var_range_from_z2d_corrected_corners(
        ex_z2d, messages=False)

    popt, pcov = scipy.optimize.curve_fit(pow_law, scales, flucts)
    # popt, pcov = scipy.optimize.curve_fit(pow_law, scales, flucts, sigma=flucts)

    popt

    for i in range(4, 20):
        plt.axvline((ex_z2d.shape[0]) // i)

    plt.plot(scales, pow_law(scales, *popt), color="springgreen")

    plt.scatter(scales, flucts, marker=".", color="deepskyblue")
Exemple #3
0
import fract

import pywt

df = pd.read_csv("/home/gaboloth/D/fisica/tesi/stats/stats1.csv", sep=";")

tdf = df[1:5]


def to_data(name, inner_func, *args, **kwargs):
    npy_points = np.load("/home/gaboloth/D/fisica/tesi/dati/npysquare/all/" +
                         name + "-2d.npy")
    return inner_func(npy_points, *args, **kwargs)


# name = "Narcanello"
# npy_points = np.load("/home/gaboloth/D/fisica/tesi/dati/npysquare/all/"+name+"-2d.npy")

npy_points = fract.fbm2D(0.3, N=8)
npy_points_plane, _ = np.mgrid[0:npy_points.shape[0], 0:npy_points.shape[1]]

# add mean plane
# npy_points = npy_points + npy_points_plane/15.0

plt.imshow(npy_points)
plt.show()

(cA, (cH, cV, cD)) = pywt.dwt2(npy_points, mode="zero", wavelet="db1")

plt.imshow(cV)
plt.imshow(np.log10(cA))

def log_sample(x, y, base=2):
    ind = np.array(range(x.size))
    xl = x[(np.mod(np.log(ind) / np.log(base), 1)) == 0]
    yl = y[(np.mod(np.log(ind) / np.log(base), 1)) == 0]

    return (xl, yl)


if __name__ == '__main__':
    print("generating")
    N = 8
    gen_H = np.float64(0.1)
    contour_H = np.float64(0.7)
    z2d = fract.fbm2D(gen_H, N)
    z2d = fract.cut_profile(z2d, contour_H)

    imm = np.copy(z2d)
    imm[imm == 0.0] = np.nan
    plt.imshow(imm, interpolation="None")
    plt.show()

    freqs, powers = fract.fourier_1(z2d, images=False, corr=False)

    freq_exp, c = fract.linear_log_fit(freqs[1:], powers[1:])
    detect_H = fract.freq_exp_to_H(freq_exp)

    freqs = freqs[2:]
    powers = powers[2:]
    sigmas = powers
Exemple #5
0
start = 0.1
stop = 0.9001
step = 0.1
nhs = len(np.arange(start, stop=stop, step=step))
res = np.zeros((nhs, 3))

for (i, H) in enumerate(np.arange(start, stop=stop, step=step)):
    # H = start + i*step
    stat_res = np.zeros(stat_n)
    print("H", H)
    for j in range(1, stat_n):

        # generate
        # Random.seed!(729 + i*34);
        data = fract.fbm2D(H, N=N)

        # dfa
        h_detected, c, scales, flucts = fract.profile_dfa_from_z2d(data)
        stat_res[j] = h_detected
        # print("     ", j)

    av_h_detected = np.mean(stat_res)
    std_h_detected = np.std(stat_res)
    print("     av. H detected =", av_h_detected)

    newrow = np.transpose([H, av_h_detected, std_h_detected])
    res[i, :] = newrow

# print(res)
plt.errorbar(res[:, 0], res[:, 1], yerr=res[:, 2])
stop = 0.9001
step = 0.1
nhs = len(np.arange(start, stop=stop, step=step))
res = np.zeros((nhs, 3))

for data_H in np.arange(start, stop=stop, step=step):

    print("data_H", data_H)
    for (i, contour_H) in enumerate(np.arange(start, stop=stop, step=step)):

        stat_res = np.zeros(stat_n)
        # print("contour_H", contour_H)
        for j in range(1, stat_n):

            # generate
            data = fract.fbm2D(data_H, N=N)
            data = cut_profile(data, contour_H)
            # plt.imshow(data)
            # plt.show()

            # dfa
            try:
                h_detected, c, scales, flucts = fract.dfa_H(data)
                stat_res[j] = h_detected
            except:
                stat_res[j] = np.nan

        av_h_detected = np.mean(stat_res[~np.isnan(stat_res)])
        std_h_detected = np.std(stat_res[~np.isnan(stat_res)])
        # print("     av. H detected =", av_h_detected)
Exemple #7
0
    #             miss_count += 1

    # segment_fs = np.trim_zeros(segment_fs, trim="b")
    # if segment_fs.size > 4:
    #     fluct_av_sq = np.sum(segment_fs)/(index)
    #     print("average for scale",s,":",np.sqrt(fluct_av_sq))
    #     print( "fluct_av_sq", (fluct_av_sq), "scale", s, ",", n_submat_m*n_submat_n, "submatrices olinear "scale", s, ",", n_submat_m*n_submat_n, "submatrices of which", miss_count, "empty. fit too poor, aborted")

    # (v_plot, w_plot) = (3,1)


# fig,ax = plt.subplots(1,1)
# ovshape = z2d.shape + (4,)linear
#### single npypoints test

npy_points = fract.fbm2D(1.5, N=9)
# npy_points_2 = fract.fbm2D(0.3, N=9)
# npy_points += npy_points_2
# npy_points_plane, _ = np.mgrid[0:npy_points.shape[0], 0:npy_points.shape[1]]

# # #### flat plane
# npy_points = np.full((512,512), 0.0)

# # #### add noise
# npy_points += np.random.normal(size=npy_points.size, scale=0.2).reshape(npy_points.shape)

#### add tilted plane
# npy_points = npy_points + npy_points_plane/25.0

plt.figure()
plt.imshow(npy_points)
Exemple #8
0
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt

import fract


def dma_1(z2d):
    pass


# name = "Adamello"
# z2d = np.load("/home/gaboloth/D/fisica/tesi/dati/npysquare/all/"+name+"-2d.npy")

z2d = fract.fbm2D(0.6, N=7)

s_min = 6
s_max = "auto"
min_nonzero = 0.99

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)]
Exemple #9
0
# df["Nome"]

# name = "Cima Wanda"
# npy_points = np.load("/home/gaboloth/D/fisica/tesi/dati/npysquare/all/"+name+"-2d.npy")

# res = np.zeros(9)
# stat_n = 50
# for i,h in enumerate(np.linspace(0.1, 0.9, 9)):
#     for j in range(stat_n):
#         z2d = fract.fbm2D(H=h, N=8)
#         res[i] += (area2(z2d))/stat_n
# plt.plot(res)

N = 10
# # z2d = np.full((2**N +1, 2**N +1), 35.0)
z2d = fract.fbm2D(H=0.6, N=N)
z2d = fract.cut_profile(z2d, contour_H=0.99)

# name = "Scerscen Inferiore"
# z2d = np.load("/home/gaboloth/D/fisica/tesi/dati/npysquare/all/"+name+"-2d.npy")

plt.figure()
plt.imshow(z2d)
plt.show()
plt.figure()


def higuchi_H(z2d):
    pass

Exemple #10
0
    # A = np.vstack([np.ones(len(s_log)), s_log]).T
    H, c, r_value, p_value, std_err = scipy.stats.linregress(s_log, f_log)
    # print("H =  ",H)
    return H, c, scales_flucts[:,0], scales_flucts[:,1]




def pow_law(x, exp, coeff):
    return coeff * x**exp

if __name__ == '__main__':
    # plot
    import fract

    ex_z2d = fract.fbm2D(0.3, 10)

    H, c, scales, flucts = fract.profile_dfa_from_z2d(ex_z2d)

    popt, pcov = scipy.optimize.curve_fit(pow_law, scales, flucts)
    
    popt

    for i in range(4,10):
        plt.axvline( (ex_z2d.shape[0])//i )

    plt.plot(scales, pow_law(scales, *popt), color="green")

    plt.scatter(scales,flucts, marker=".", color="deepskyblue")
    plt.plot(scales, (10**c)*scales**H, color="purple")
    plt.title("H = " + str(H))
Exemple #11
0
            vec[gstep:side - gstep:gstep,
                half:side:gstep] = (up4 + down4 + left4 + right4) / 4

    return vec[1:side - 1, 1:side - 1]


scale_t = 65
# small scale fractal
# z2d = fbm2D_range(0.6, N=9, max_scale=scale_t)

# large scale fractal
# z2d = fbm2D_range(0.6, N=9, min_scale=25)

# mixed scale
# small
z2d = fract.fbm2D(0.6, N=9)
# 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
Exemple #12
0
import numpy as np
import matplotlib.pyplot as plt

import fract
import c_dma

z2d = fract.fbm2D(H=0.6, N=8)

dmah, dmac, arr1, arr2 = fract.dma_H(z2d)

N = 8

stat_n = 50

start = 0.1
stop = 0.9001
step = 0.1
nhs = len(np.arange(start,stop=stop,step=step))
res = np.zeros( (nhs, 3))


for (i,H) in enumerate( np.arange(start,stop=stop,step=step) ):
    # H = start + i*step  
    stat_res = np.zeros(stat_n)
    print("H", H)
    for j in range(1, stat_n):

        # generate
        # Random.seed!(729 + i*34);
        data = fract.fbm2D(H,N=N)
Exemple #13
0
        dfa_H, dfa_c, scales3, flucts3 = fract.dfa_H(*args, **kwargs)
        return dfa_H
    except:
        return np.nan


# df["Fourier H"] = df["Nome"].apply(to_data, inner_func=fourier_H_discard)

# df["DFA H"] = df["Nome"].apply(to_data, inner_func=dfa_H_discard)

# name = "Ventina"
# npy_points = np.load("/home/gaboloth/D/fisica/tesi/dati/npysquare/all/"+name+"-2d.npy")

#### single npypoints test

npy_points = fract.fbm2D(0.7, N=9)

# #### flat plane
# npy_points = np.full((512,512), 0.0)

# # #### add noise
# npy_points += np.random.normal(size=npy_points.size, scale=0.2).reshape(npy_points.shape)

x2d, y2d = np.mgrid[0:npy_points.shape[0], 0:npy_points.shape[1]]

#### add tilted plane
# npy_points = npy_points + x2d*50

#### add plane tiles
n_plane_tiles = 5
s_plane = npy_points.shape[0] // n_plane_tiles