def invert_dif(thetav, phiv, D, I, F, degrees, g0=None, steps=5): # reescalar intensidad pls k, m, n = degrees if g0 is None: g0 = numpy.zeros_like(k); g0[0] = -1000 #because why the heck not g = g0.copy() Ax, Ay, Az = condition_matrix_xyz(thetav, phiv, degrees) D_0, I_0, F_0 = D, I, F D_0 = D_0[~numpy.isnan(D_0)] I_0 = I_0[~numpy.isnan(I_0)] F_0 = F_0[~numpy.isnan(F_0)] #dif_0 = numpy.concatenate((D_0, I_0, F_0)) di_0 = numpy.concatenate((D_0, I_0)) for iteration in range(steps): Bx, By, Bz = xyzfield(k, m, n, g, thetav, phiv) D_model, I_model, F_model, H_model = xyz.xyz2difh(Bx, By, Bz) Ad, Ai, Af = condition_matrix_dif(Bx, By, Bz, F_model, H_model, Ax, Ay, Az) Af = Af / F_model[:, numpy.newaxis] Adif = numpy.concatenate((Ad[~numpy.isnan(D_0), :], Ai[~numpy.isnan(I_0), :], Af[~numpy.isnan(F_0), :]), axis=0) D_model = D_model[~numpy.isnan(D_0)] I_model = I_model[~numpy.isnan(I_0)] F_model = F_model[~numpy.isnan(F_0)] di = numpy.concatenate((D_model, I_model), axis=0) #corregir diferencias 360 -> 0 di_delta = numpy.arctan2(numpy.sin(di-di_0), numpy.cos(di-di_0)) f_delta = (F_model - F_0)/F_model delta = numpy.concatenate((di_delta, f_delta), axis=0) #hmmmmm g = g - (numpy.linalg.pinv(Adif.T @ Adif) @ Adif.T) @ delta #solution = numpy.linalg.lstsq(Adif, delta) #g = g - solution[0] sys.stdout.write("\r") sys.stdout.write(str(numpy.sqrt(sum(delta**2)))) if (sum(abs(delta**2)) > 10000): warnings.warn("inversion might not have converged (╯°□°)╯︵ ┻━┻") return g
def drift(lat, lon, gcomps, order=10): theta = numpy.deg2rad(90 - lat) phi = numpy.deg2rad(lon) declinations = numpy.empty_like(data.years) inclinations = numpy.empty_like(data.years) for i, y in enumerate(data.years): dec, inc, f, h = xyzfield.xyz2difh(*xyzfield.xyzfieldv2(gcomps[i, :], phi, theta, order)) declinations[i] = dec inclinations[i] = inc intensities[i] = f return declinations, inclinations, intensities
def invert_dif(thetav, phiv, data, order=13, g0=-30000, steps=5): g=numpy.zeros(order*(order+2)); g[0]=g0 Ax,Ay,Az = condition_array_xyz(thetav,phiv,order) dec0, inc0, int0 = data dec0_f = dec0[~numpy.isnan(dec0)] inc0_f = inc0[~numpy.isnan(inc0)] int0_f = int0[~numpy.isnan(int0)] dif0=numpy.concatenate((dec0_f,inc0_f,int0_f)) dif=dif0.copy() for iteration in range(steps): x,y,z=xyzfield.xyzfieldv2(g,phiv,thetav, rparam=1.0, order=order) #pyplot.show(xyzfield.xyzcontour(thetav,phiv,x,y,z,regular=False)) dec,inc,intensity,horizontal=xyzfield.xyz2difh(x,y,z) Ad, Ai, Af = condition_array_dif(x,y,z,intensity,horizontal, Ax, Ay, Az, order) #remove emptys Ad=Ad[:,~numpy.isnan(dec0)] Ai=Ai[:,~numpy.isnan(inc0)] Af=Af[:,~numpy.isnan(int0)] dec=dec[~numpy.isnan(dec0)] inc=inc[~numpy.isnan(inc0)] intensity=intensity[~numpy.isnan(int0)] Adif=numpy.concatenate((Ad,Ai,Af), axis=1) dif = numpy.concatenate((dec,inc,intensity)) #get delta delta=dif0-dif #g = numpy.linalg.inv(Adif @ Adif.transpose()) @ Adif @ dif0 g = g + numpy.linalg.inv(Adif @ Adif.transpose()) @ Adif @ delta #print(g[:5], sum(abs(delta))) if (sum(abs(delta)) > 1000): warnings.warn("inversion might not have converged (╯°□°)╯︵ ┻━┻") return g
def invert_dift(thetav, phiv, t, D, I, F, degrees, knots, g0=None, reg_coef_spatial = 0, reg_coef_time = 0, theta_0 = None, steps=5): k, m, n = degrees #n_knots, n_degrees = len(knots)-4, len(k) #knots = bspline.fix_knots(kn, 3) n_knots, n_degrees = len(knots), len(k) n_coefs = n_knots*n_degrees #base = bspline.deboor_base(knots, t, 3)[:, :-4] Ax, Ay, Az = condition_matrix_xyz(thetav, phiv, degrees) D_0, I_0, F_0 = D, I, F base = numpy.concatenate((bspline.condition_array(knots, t[~numpy.isnan(D)]), bspline.condition_array(knots, t[~numpy.isnan(I)]), bspline.condition_array(knots, t[~numpy.isnan(F)])), axis=0) D_0 = D_0[~numpy.isnan(D)] I_0 = I_0[~numpy.isnan(I)] F_0 = F_0[~numpy.isnan(F)] if g0 is None: g0 = numpy.zeros((n_knots, n_degrees)) can_get_z = ((~numpy.isnan(I)) & (~numpy.isnan(F))) avg_z = numpy.average(F[can_get_z] * sin(I[can_get_z])) g0[:, 0] = -avg_z g = g0.copy() di_0 = numpy.concatenate((D_0, I_0)) if (reg_coef_spatial != 0) or (reg_coef_time != 0): reg_matrix = full_reg(spatial_reg(k, m, n, theta_0), time_reg(knots), coef_L=reg_coef_spatial, coef_S=reg_coef_time) else: reg_matrix = 0.0 for iteration in range(steps): #como poner la dependencia del tiempo aquí? #con magia Bx, By, Bz = xyzfieldt(k, m, n, knots, g, thetav, phiv, t) D_model, I_model, F_model, H_model = xyz.xyz2difh(Bx, By, Bz) Ad, Ai, Af = condition_matrix_dif(Bx, By, Bz, F_model, H_model, Ax, Ay, Az) Af = Af / F_model[:, numpy.newaxis] Adif = numpy.concatenate((Ad[~numpy.isnan(D), :], Ai[~numpy.isnan(I), :], Af[~numpy.isnan(F), :]), axis=0) Adif = numpy.concatenate([Adif*spline[:, numpy.newaxis] for spline in base.T], axis=1) D_model = D_model[~numpy.isnan(D)] I_model = I_model[~numpy.isnan(I)] F_model = F_model[~numpy.isnan(F)] di = numpy.concatenate((D_model, I_model), axis=0) di_delta = numpy.arctan2(numpy.sin(di-di_0), numpy.cos(di-di_0)) f_delta = (F_model - F_0)/F_model delta = numpy.concatenate((di_delta, f_delta), axis=0) #solution = numpy.linalg.lstsq(Adif, delta) #g = g - solution[0].reshape((n_knots, n_degrees)) g = g - ((numpy.linalg.pinv(Adif.T @ Adif + reg_matrix) @ Adif.T) @ delta).reshape((n_knots, n_degrees)) sys.stdout.write("\r") sys.stdout.write("[{: <20}] ".format("#"*int((iteration/steps)*20+1))) sys.stdout.write("iteration {0} : rms = ".format(iteration+1)) sys.stdout.write(str(numpy.sqrt(sum(delta**2)/len(delta)))) #debuge #ax.scatter(t, di_delta[:len(di_delta)//2], color="green") #ax.scatter(t, di_delta[len(di_delta)//2:], color="red") #ax.scatter(t, f_delta, color="blue") #ax.scatter(t, D_model - D_0, color="green") #ax.scatter(t, I_model - I_0, color="red") #ax.scatter(t, F_model - F_0, color="blue") #pyplot.show(fig) if (sum(abs(delta**2)) > 10000): warnings.warn("a bad thing is happening ヽ( `д´*)ノ") return g