ararpy 0.1.199__py3-none-any.whl → 0.2.1__py3-none-any.whl
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- ararpy/Example - Check arr.py +52 -0
- ararpy/Example - Granite Cooling History.py +411 -0
- ararpy/Example - Plot temperature calibration.py +291 -0
- ararpy/Example - Show MDD results.py +561 -0
- ararpy/Example - Show all Kfs age spectra.py +344 -0
- ararpy/Example - Show random walk results.py +363 -0
- ararpy/Example - Tc calculation.py +437 -0
- ararpy/__init__.py +3 -4
- ararpy/calc/age.py +34 -36
- ararpy/calc/arr.py +0 -20
- ararpy/calc/basic.py +26 -3
- ararpy/calc/corr.py +131 -85
- ararpy/calc/plot.py +1 -2
- ararpy/calc/raw_funcs.py +41 -2
- ararpy/calc/regression.py +224 -132
- ararpy/files/arr_file.py +2 -1
- ararpy/files/basic.py +0 -22
- ararpy/files/calc_file.py +107 -84
- ararpy/files/raw_file.py +242 -229
- ararpy/smp/basic.py +133 -34
- ararpy/smp/calculation.py +6 -6
- ararpy/smp/corr.py +339 -153
- ararpy/smp/diffusion_funcs.py +345 -36
- ararpy/smp/export.py +247 -129
- ararpy/smp/info.py +2 -2
- ararpy/smp/initial.py +93 -45
- ararpy/smp/json.py +2 -2
- ararpy/smp/plots.py +144 -164
- ararpy/smp/raw.py +11 -15
- ararpy/smp/sample.py +222 -181
- ararpy/smp/style.py +26 -7
- ararpy/smp/table.py +42 -33
- ararpy/thermo/atomic_level_random_walk.py +56 -48
- ararpy/thermo/basic.py +2 -2
- {ararpy-0.1.199.dist-info → ararpy-0.2.1.dist-info}/METADATA +1 -1
- ararpy-0.2.1.dist-info/RECORD +73 -0
- {ararpy-0.1.199.dist-info → ararpy-0.2.1.dist-info}/WHEEL +1 -1
- ararpy-0.1.199.dist-info/RECORD +0 -66
- {ararpy-0.1.199.dist-info → ararpy-0.2.1.dist-info}/licenses/LICENSE +0 -0
- {ararpy-0.1.199.dist-info → ararpy-0.2.1.dist-info}/top_level.txt +0 -0
ararpy/calc/regression.py
CHANGED
|
@@ -16,7 +16,7 @@ import traceback
|
|
|
16
16
|
import numpy as np
|
|
17
17
|
import pandas as pd
|
|
18
18
|
from scipy.stats import distributions
|
|
19
|
-
from scipy.optimize import
|
|
19
|
+
from scipy.optimize import minimize_scalar
|
|
20
20
|
import warnings
|
|
21
21
|
from scipy.optimize import minimize
|
|
22
22
|
warnings.simplefilter(action="ignore", category=RuntimeWarning)
|
|
@@ -495,22 +495,25 @@ def linest(a0: list, a1: list, *args):
|
|
|
495
495
|
# calculate Y values base on the fitted formula
|
|
496
496
|
estimate_y = np.matmul(x, beta)
|
|
497
497
|
resid = (estimate_y - y) ** 2
|
|
498
|
-
reg = (estimate_y - np.mean(
|
|
499
|
-
ssresid = resid.sum()
|
|
500
|
-
ssreg = reg.sum()
|
|
501
|
-
sstotal =
|
|
498
|
+
reg = (estimate_y - np.mean(y)) ** 2
|
|
499
|
+
ssresid = resid.sum() # 残差平方和
|
|
500
|
+
ssreg = reg.sum() # 回归平方和
|
|
501
|
+
sstotal = ((y - np.mean(y)) ** 2).sum()
|
|
502
|
+
r2 = ssreg / sstotal if sstotal != 0 else np.inf
|
|
503
|
+
|
|
502
504
|
df = m - n
|
|
503
|
-
m_ssresid = ssresid / df
|
|
504
|
-
|
|
505
|
-
|
|
505
|
+
m_ssresid = ssresid / df # 均方残差,与加权平均中的MSWD对应
|
|
506
|
+
cov_beta = m_ssresid * inv_xtx
|
|
507
|
+
se_beta = np.diagonal(cov_beta) ** .5
|
|
508
|
+
|
|
509
|
+
beta = beta.flatten()
|
|
506
510
|
rse_beta = se_beta / beta
|
|
507
|
-
r2 = ssreg / sstotal if sstotal != 0 else np.inf
|
|
508
511
|
|
|
509
512
|
def get_adjusted_y(*args):
|
|
510
513
|
args = [[1] * len(args[0]), *args]
|
|
511
514
|
return [sum([beta[i] * args[i][j] for i in range(len(beta))]) for j in range(len(args[0]))]
|
|
512
515
|
|
|
513
|
-
return beta[0], se_beta[0], rse_beta[0] * 100, r2, m_ssresid, beta,
|
|
516
|
+
return beta[0], se_beta[0], abs(rse_beta[0]) * 100, r2, m_ssresid, beta, cov_beta, get_adjusted_y, m_ssresid
|
|
514
517
|
|
|
515
518
|
|
|
516
519
|
def average(a0: list, a1=None):
|
|
@@ -535,12 +538,12 @@ def average(a0: list, a1=None):
|
|
|
535
538
|
m_ssresid = ssresid / df
|
|
536
539
|
r2 = ssreg / sstotal if sstotal != 0 else 1 # r2 = ssreg / sstotal
|
|
537
540
|
|
|
538
|
-
|
|
539
|
-
|
|
541
|
+
k6 = [[sum([(i - k0) ** 2 for i in a0]) / df]]
|
|
542
|
+
k1 = pow(k6[0][0], 0.5) # standard deviation
|
|
543
|
+
k2 = k1 / abs(k0) * 100 if k0 != 0 else 0 # relative standard error
|
|
540
544
|
k3 = r2 # determination coefficient
|
|
541
|
-
k4 = 'MSWD'
|
|
545
|
+
k4 = m_ssresid # 'MSWD'
|
|
542
546
|
k5 = [k0]
|
|
543
|
-
k6 = [k1]
|
|
544
547
|
k8 = m_ssresid
|
|
545
548
|
|
|
546
549
|
def get_adjusted_y(x: list):
|
|
@@ -737,7 +740,7 @@ def quadratic(a0: list, a1: list):
|
|
|
737
740
|
"""
|
|
738
741
|
# y = b + m1 * x + m2 * x ^ 2
|
|
739
742
|
k = list(linest(a0, a1, [i ** 2 for i in a1]))
|
|
740
|
-
|
|
743
|
+
[b, m1, m2] = k[5]
|
|
741
744
|
|
|
742
745
|
def get_adjusted_y(x: list):
|
|
743
746
|
return [b + m1 * _x + m2 * _x ** 2 for _x in x]
|
|
@@ -756,7 +759,7 @@ def polynomial(a0: list, a1: list, degree: int = 5):
|
|
|
756
759
|
"""
|
|
757
760
|
# y = b + m1 * x + m2 * x ^ 2 + ... + m[n] * x ^ n
|
|
758
761
|
k = list(linest(a0, *[[j ** (i + 1) for j in a1] for i in range(degree)]))
|
|
759
|
-
|
|
762
|
+
beta = k[5]
|
|
760
763
|
|
|
761
764
|
def get_adjusted_y(x: list):
|
|
762
765
|
return [sum([beta[i] * _x ** i for i in range(degree + 1)]) for _x in x]
|
|
@@ -766,6 +769,7 @@ def polynomial(a0: list, a1: list, degree: int = 5):
|
|
|
766
769
|
return k
|
|
767
770
|
|
|
768
771
|
|
|
772
|
+
### Deprecated
|
|
769
773
|
def logest(a0: list, a1: list):
|
|
770
774
|
"""
|
|
771
775
|
:param a0: known_y's, y = b * m ^ x
|
|
@@ -775,15 +779,19 @@ def logest(a0: list, a1: list):
|
|
|
775
779
|
# y = b * m ^ x, Microsoft Excel LOGEST function, ln(y) = ln(b) + ln(m) * x
|
|
776
780
|
a0 = [np.log(i) for i in a0] # ln(y)
|
|
777
781
|
linest_res = linest(a0, a1)
|
|
778
|
-
|
|
779
|
-
|
|
782
|
+
lnb, selnb, rseb, r2, mswd, beta, cov_beta = linest_res[0:7]
|
|
783
|
+
lnb, lnm = beta
|
|
784
|
+
selnb, selnm = np.diagonal(cov_beta) ** .5
|
|
785
|
+
|
|
786
|
+
b = np.exp(lnb)
|
|
780
787
|
m = np.exp(lnm)
|
|
781
788
|
sem = np.exp(lnm) * selnm
|
|
782
|
-
seb =
|
|
783
|
-
rseb = seb / b * 100
|
|
789
|
+
seb = np.exp(lnb) * selnb # Excel.Logest function do not consider the error propagation
|
|
790
|
+
rseb = seb / abs(b) * 100
|
|
784
791
|
return b, seb, rseb, r2, mswd, m, sem
|
|
785
792
|
|
|
786
793
|
|
|
794
|
+
### Deprecated
|
|
787
795
|
def power(a0: list, a1: list):
|
|
788
796
|
"""
|
|
789
797
|
:param a0: known_y's, y = a * x ^ b + c
|
|
@@ -794,39 +802,31 @@ def power(a0: list, a1: list):
|
|
|
794
802
|
def _pow_func(x, a, b, c):
|
|
795
803
|
return a * x ** b + c
|
|
796
804
|
|
|
797
|
-
def
|
|
805
|
+
def _residuals(params):
|
|
798
806
|
a, b, c = params
|
|
799
|
-
|
|
800
|
-
|
|
801
|
-
|
|
802
|
-
|
|
803
|
-
y[1] = sum(a0) / len(a0)
|
|
804
|
-
x[2] = sum(a1[-3:]) / 3
|
|
805
|
-
y[2] = sum(a0[-3:]) / 3
|
|
806
|
-
return np.array([
|
|
807
|
-
_pow_func(x[0], a, b, c) - y[0],
|
|
808
|
-
_pow_func(x[1], a, b, c) - y[1],
|
|
809
|
-
_pow_func(x[2], a, b, c) - y[2],
|
|
810
|
-
])
|
|
811
|
-
|
|
812
|
-
def _get_sum(a, b, c):
|
|
813
|
-
y_predicted = [_pow_func(_x, a, b, c) for _x in a1]
|
|
814
|
-
return sum([(y_predicted[i] - a0[i]) ** 2 for i in range(len(a0))])
|
|
807
|
+
return [_pow_func(xi, a, b, c) - yi for xi, yi in zip(a1, a0)]
|
|
808
|
+
|
|
809
|
+
def _sum_squared_error(params):
|
|
810
|
+
return sum(r**2 for r in _residuals(params))
|
|
815
811
|
|
|
816
812
|
def _get_abc(b): # Return a, b, c given b based on linest regression
|
|
817
813
|
f = linest(a0, [_x ** b for _x in a1])
|
|
818
814
|
return f[5][1], b, f[0]
|
|
819
815
|
|
|
816
|
+
def _get_init():
|
|
817
|
+
f = linest(np.log(np.array(a0)), np.log(np.array(a1)))
|
|
818
|
+
return np.exp(f[0]), f[5][1], 0
|
|
819
|
+
|
|
820
820
|
try:
|
|
821
|
-
a, b, c =
|
|
821
|
+
a, b, c = _get_init() # initial estimate
|
|
822
822
|
count = 0
|
|
823
823
|
step = 0.01
|
|
824
824
|
while count < 100:
|
|
825
825
|
a, b, c = _get_abc(b)
|
|
826
|
-
s =
|
|
826
|
+
s = _sum_squared_error([a, b, c])
|
|
827
827
|
b_left, b_right = b - step * b, b + step * b
|
|
828
|
-
s_left =
|
|
829
|
-
s_right =
|
|
828
|
+
s_left = _sum_squared_error(_get_abc(b_left))
|
|
829
|
+
s_right = _sum_squared_error(_get_abc(b_right))
|
|
830
830
|
if s_left > s > s_right:
|
|
831
831
|
b = b_right
|
|
832
832
|
continue
|
|
@@ -849,7 +849,9 @@ def power(a0: list, a1: list):
|
|
|
849
849
|
raise IndexError
|
|
850
850
|
|
|
851
851
|
f = linest(a0, [_x ** b for _x in a1])
|
|
852
|
-
|
|
852
|
+
beta, cov_beta = f[5:7]
|
|
853
|
+
c, a = beta
|
|
854
|
+
sec, sea = np.diagonal(cov_beta) ** .5
|
|
853
855
|
|
|
854
856
|
calculated_y = [_pow_func(i, a, b, c) for i in a1]
|
|
855
857
|
resid = [(calculated_y[i] - a0[i]) ** 2 for i in range(len(a0))]
|
|
@@ -870,111 +872,193 @@ def power(a0: list, a1: list):
|
|
|
870
872
|
errfx = pow(sum([i ** 2 for i in a1]) / (dp * sum([i ** 2 for i in a1]) - sum(a1) ** 2), 0.5)
|
|
871
873
|
# seb = errfz * sey = errfz * ssresid / df -> se_intercept = sey * errfx = seb / errfz * errfx
|
|
872
874
|
se_intercept = sec / errfz * errfx
|
|
873
|
-
rse_intercept = se_intercept / intercept * 100
|
|
875
|
+
rse_intercept = se_intercept / abs(intercept) * 100
|
|
876
|
+
|
|
877
|
+
|
|
878
|
+
exp_beta = [a, b, c]
|
|
879
|
+
exp_cov_beta = [
|
|
880
|
+
[cov_beta[1][1], 0, cov_beta[1][0]],
|
|
881
|
+
[0, 0, 0],
|
|
882
|
+
[cov_beta[0][1], 0, cov_beta[0][0]],
|
|
883
|
+
]
|
|
874
884
|
|
|
875
885
|
return intercept, se_intercept, rse_intercept, r2, 'mswd', [a, b, c], 'se', \
|
|
876
886
|
lambda x: [_pow_func(i, a, b, c) for i in x], m_ssresid
|
|
877
887
|
|
|
878
888
|
|
|
879
889
|
def exponential(a0: list, a1: list):
|
|
880
|
-
|
|
881
|
-
|
|
882
|
-
|
|
883
|
-
|
|
884
|
-
|
|
890
|
+
try:
|
|
891
|
+
k = _exponential(a0, a1, newton_minimize_1d)
|
|
892
|
+
if k[2] > 100:
|
|
893
|
+
raise ValueError
|
|
894
|
+
if abs(k[0] - min(a0)) > 5 * (max(a0) - min(a0)):
|
|
895
|
+
raise ValueError
|
|
896
|
+
except Exception as e:
|
|
897
|
+
print(f"newton_minimize_1d failed, using grad minimize")
|
|
898
|
+
k = _exponential(a0, a1, grad_minimize)
|
|
899
|
+
return k
|
|
885
900
|
|
|
886
|
-
def _exp_func(x, a, b, c):
|
|
887
|
-
return a * b ** x + c
|
|
888
901
|
|
|
889
|
-
|
|
890
|
-
a, b, c = params
|
|
891
|
-
x, y = [0, 0, 0], [0, 0, 0]
|
|
892
|
-
x[0] = sum(a1[:3]) / 3
|
|
893
|
-
y[0] = sum(a0[:3]) / 3
|
|
894
|
-
x[1] = sum(a1) / len(a1)
|
|
895
|
-
y[1] = sum(a0) / len(a0)
|
|
896
|
-
x[2] = sum(a1[-3:]) / 3
|
|
897
|
-
y[2] = sum(a0[-3:]) / 3
|
|
898
|
-
return np.array([
|
|
899
|
-
_exp_func(x[0], a, b, c) - y[0],
|
|
900
|
-
_exp_func(x[1], a, b, c) - y[1],
|
|
901
|
-
_exp_func(x[2], a, b, c) - y[2],
|
|
902
|
-
])
|
|
903
|
-
|
|
904
|
-
def _get_sum(a, b, c):
|
|
905
|
-
y_predicted = [_exp_func(_x, a, b, c) for _x in a1]
|
|
906
|
-
return sum([(y_predicted[i] - a0[i]) ** 2 for i in range(len(a0))])
|
|
907
|
-
|
|
908
|
-
def _get_ac(b):
|
|
909
|
-
f = linest(a0, [b ** _x for _x in a1])
|
|
910
|
-
return f[5][1], b, f[0]
|
|
902
|
+
def _exponential(a0, a1, method):
|
|
911
903
|
|
|
912
|
-
|
|
913
|
-
|
|
914
|
-
|
|
915
|
-
step = 0.01
|
|
916
|
-
while count < 100:
|
|
917
|
-
a, b, c = _get_ac(b)
|
|
918
|
-
s = _get_sum(a, b, c)
|
|
919
|
-
b_left, b_right = b - step * b, b + step * b
|
|
920
|
-
s_left = _get_sum(*_get_ac(b_left))
|
|
921
|
-
s_right = _get_sum(*_get_ac(b_right))
|
|
922
|
-
if s_left > s > s_right:
|
|
923
|
-
b = b_right
|
|
924
|
-
continue
|
|
925
|
-
elif s_left < s < s_right:
|
|
926
|
-
b = b_left
|
|
927
|
-
continue
|
|
928
|
-
elif s_left < s_right:
|
|
929
|
-
b = (b + b_left) / 2
|
|
930
|
-
else:
|
|
931
|
-
b = (b + b_right) / 2
|
|
932
|
-
count += 1
|
|
933
|
-
step = step * 0.5
|
|
934
|
-
if step < 0.000001:
|
|
935
|
-
break
|
|
904
|
+
X = np.array(a1)
|
|
905
|
+
Y = np.array(a0)
|
|
906
|
+
y = np.array([a0]).transpose()
|
|
936
907
|
|
|
937
|
-
|
|
938
|
-
|
|
908
|
+
def _exp_func(xi, a, b, c):
|
|
909
|
+
return a * np.exp(b * xi) + c
|
|
910
|
+
|
|
911
|
+
@np.vectorize
|
|
912
|
+
def _get_s(b):
|
|
913
|
+
x = np.concatenate(([np.ones(len(a1))], [np.exp(b * np.array(a1))]), axis=0).transpose()
|
|
914
|
+
try:
|
|
915
|
+
inv_xtx = np.linalg.inv(np.matmul(x.transpose(), x))
|
|
916
|
+
except np.linalg.LinAlgError:
|
|
917
|
+
raise np.linalg.LinAlgError(f"The determinant of the given matrix must not be zero ")
|
|
918
|
+
beta = np.matmul(inv_xtx, np.matmul(x.transpose(), y))
|
|
919
|
+
c, a = beta.flatten()
|
|
920
|
+
reg_y = _exp_func(X, a, b, c)
|
|
921
|
+
resid = (reg_y - Y) ** 2
|
|
922
|
+
ssresid = sum(resid)
|
|
923
|
+
return ssresid
|
|
924
|
+
|
|
925
|
+
# 1阶
|
|
926
|
+
ini_f1 = linest(Y, X)
|
|
927
|
+
y_range = max(Y) - min(Y)
|
|
928
|
+
ini_b = ini_f1[5][1] / max(y_range, 1e-12)
|
|
929
|
+
# 2阶
|
|
930
|
+
ini_f2 = quadratic(Y, X)
|
|
931
|
+
ini_b *= np.sign(ini_f1[5][1]) * np.sign(ini_f2[5][2])
|
|
932
|
+
|
|
933
|
+
b, count = method(_get_s, ini_b)
|
|
934
|
+
|
|
935
|
+
x = np.concatenate(([np.ones(len(a1))], [np.exp(b * np.array(a1))]), axis=0).transpose()
|
|
936
|
+
|
|
937
|
+
m, n = x.shape # number of data, number of unknown x
|
|
938
|
+
try:
|
|
939
|
+
inv_xtx = np.linalg.inv(np.matmul(x.transpose(), x))
|
|
939
940
|
except np.linalg.LinAlgError:
|
|
940
|
-
raise np.linalg.LinAlgError
|
|
941
|
-
|
|
942
|
-
|
|
943
|
-
|
|
944
|
-
|
|
945
|
-
|
|
946
|
-
|
|
947
|
-
|
|
948
|
-
|
|
949
|
-
calculated_y = [_exp_func(i, a, b, c) for i in a1]
|
|
950
|
-
resid = [(calculated_y[i] - a0[i]) ** 2 for i in range(len(a0))]
|
|
951
|
-
reg = [(i - sum(calculated_y) / len(calculated_y)) ** 2 for i in calculated_y]
|
|
952
|
-
ssresid = sum(resid)
|
|
953
|
-
ssreg = sum(reg)
|
|
954
|
-
sstotal = ssreg + ssresid
|
|
955
|
-
dp = len(a1)
|
|
956
|
-
df = dp - 1
|
|
957
|
-
m_ssresid = ssresid / df
|
|
941
|
+
raise np.linalg.LinAlgError(f"The determinant of the given matrix must not be zero ")
|
|
942
|
+
beta = np.matmul(inv_xtx, np.matmul(x.transpose(), y))
|
|
943
|
+
estimate_y = np.matmul(x, beta)
|
|
944
|
+
resid = (estimate_y - y) ** 2
|
|
945
|
+
reg = (estimate_y - np.mean(y)) ** 2
|
|
946
|
+
ssresid = resid.sum() # 残差平方和
|
|
947
|
+
ssreg = reg.sum() # 回归平方和
|
|
948
|
+
sstotal = ((y - np.mean(y)) ** 2).sum()
|
|
958
949
|
r2 = ssreg / sstotal if sstotal != 0 else 1
|
|
959
950
|
|
|
960
|
-
|
|
951
|
+
df = m - n
|
|
952
|
+
m_ssresid = ssresid / df # 均方残差,与加权平均中的MSWD对应
|
|
953
|
+
cov_beta = m_ssresid * inv_xtx
|
|
954
|
+
|
|
955
|
+
sc, sa = np.diagonal(cov_beta) ** .5
|
|
956
|
+
c, a = beta.flatten()
|
|
961
957
|
intercept = a + c
|
|
962
|
-
|
|
963
|
-
# calculate error of intercept
|
|
964
|
-
errfz = pow(sum([i ** 2 for i in z]) / (dp * sum([i ** 2 for i in z]) - sum(z) ** 2), 0.5)
|
|
965
|
-
errfx = pow(sum([i ** 2 for i in a1]) / (dp * sum([i ** 2 for i in a1]) - sum(a1) ** 2), 0.5)
|
|
966
|
-
# seb = errfz * sey = errfz * ssresid / df -> se_intercept = sey * errfx = seb / errfz * errfx
|
|
967
|
-
se_intercept = sec / errfz * errfx
|
|
968
|
-
rse_intercept = se_intercept / intercept * 100
|
|
958
|
+
se_intercept = pow(sa ** 2 + sc ** 2 + 2 * cov_beta[0][1] , 0.5)
|
|
969
959
|
|
|
970
|
-
|
|
971
|
-
|
|
960
|
+
exp_beta = [a, b, c]
|
|
961
|
+
exp_cov_beta = [
|
|
962
|
+
[cov_beta[1][1], 0, cov_beta[1][0]],
|
|
963
|
+
[0, 0, 0],
|
|
964
|
+
[cov_beta[0][1], 0, cov_beta[0][0]],
|
|
965
|
+
]
|
|
966
|
+
|
|
967
|
+
# print(f"{b = }, {a = }, {c = }, {count = }, {ssresid = }, {r2 = }")
|
|
968
|
+
|
|
969
|
+
return intercept, se_intercept, se_intercept / abs(intercept) * 100, r2, m_ssresid, exp_beta, exp_cov_beta, \
|
|
970
|
+
lambda x: [_exp_func(xi, a, b, c) for xi in x], m_ssresid
|
|
971
|
+
|
|
972
|
+
|
|
973
|
+
def newton_minimize_1d(func, x0, h=1e-5, tol=1e-8, max_iter=30):
|
|
974
|
+
x = x0
|
|
975
|
+
for i in range(max_iter):
|
|
976
|
+
fx = func(x)
|
|
977
|
+
if not np.isfinite(fx):
|
|
978
|
+
raise ValueError("Newton Minimize: Function returned non-finite value at x={}".format(x))
|
|
979
|
+
|
|
980
|
+
# 一阶导数(中心差分)
|
|
981
|
+
f_plus = func(x + h)
|
|
982
|
+
f_minus = func(x - h)
|
|
983
|
+
if not (np.isfinite(f_plus) and np.isfinite(f_minus)):
|
|
984
|
+
raise ValueError("Newton Minimize: Non-finite values in derivative computation")
|
|
985
|
+
df = (f_plus - f_minus) / (2 * h)
|
|
986
|
+
d2f = (f_plus - 2 * fx + f_minus) / (h * h)
|
|
987
|
+
|
|
988
|
+
# 牛顿步长
|
|
989
|
+
if abs(d2f) < 1e-12: # 避免除零或平坦区域
|
|
990
|
+
raise ValueError("Newton Minimize: Second derivative too small, stopping.")
|
|
991
|
+
|
|
992
|
+
step = df / d2f
|
|
993
|
+
x_new = x - step
|
|
994
|
+
|
|
995
|
+
if abs(step) < tol:
|
|
996
|
+
x = x_new
|
|
997
|
+
break
|
|
998
|
+
|
|
999
|
+
x = x_new
|
|
1000
|
+
|
|
1001
|
+
if i + 1 == max_iter:
|
|
1002
|
+
raise ValueError("Newton Minimize: Over iteration max_iter={}".format(max_iter))
|
|
1003
|
+
|
|
1004
|
+
return x, i + 1
|
|
1005
|
+
|
|
1006
|
+
|
|
1007
|
+
def grad_minimize(func, x, lr=1e-5, tol=1e-8, max_iter=1000):
|
|
1008
|
+
|
|
1009
|
+
for i in range(max_iter):
|
|
1010
|
+
f_plus = func(x + lr)
|
|
1011
|
+
f_minus = func(x - lr)
|
|
1012
|
+
if not (np.isfinite(f_plus) and np.isfinite(f_minus)):
|
|
1013
|
+
raise ValueError("Newton Minimize: Non-finite values in derivative computation")
|
|
1014
|
+
g = (f_plus - f_minus) / (2 * lr)
|
|
1015
|
+
x_new = x - lr * g
|
|
1016
|
+
if abs(func(x_new) - func(x)) < tol:
|
|
1017
|
+
break
|
|
1018
|
+
x = x_new
|
|
1019
|
+
return x, i+1
|
|
972
1020
|
|
|
973
1021
|
|
|
974
1022
|
""" line functions """
|
|
975
1023
|
|
|
976
1024
|
|
|
977
|
-
def
|
|
1025
|
+
def linest_var(beta: list, cov: list, x: float):
|
|
1026
|
+
""" y = b0 * x^0 + b1 * x^1 + ... + bn * x^n
|
|
1027
|
+
Parameters
|
|
1028
|
+
----------
|
|
1029
|
+
|
|
1030
|
+
Returns
|
|
1031
|
+
-------
|
|
1032
|
+
|
|
1033
|
+
"""
|
|
1034
|
+
beta = np.array(beta, dtype=np.float64)
|
|
1035
|
+
cov_matrix = np.array(cov, dtype=np.float64)
|
|
1036
|
+
g = np.array([x ** k for k in range(len(beta))]).reshape(-1, 1)
|
|
1037
|
+
y = np.dot(g.T, beta.reshape(-1, 1))[0, 0]
|
|
1038
|
+
var = np.matmul(np.matmul(g.T, cov_matrix), g)[0, 0]
|
|
1039
|
+
return y, var
|
|
1040
|
+
|
|
1041
|
+
|
|
1042
|
+
def quadratic_var(beta: list, cov: list, x: float):
|
|
1043
|
+
""" y = b0 * x^0 + b1 * x^1 + b2 * x^2
|
|
1044
|
+
Parameters
|
|
1045
|
+
----------
|
|
1046
|
+
beta : coefficients
|
|
1047
|
+
x :
|
|
1048
|
+
|
|
1049
|
+
Returns
|
|
1050
|
+
-------
|
|
1051
|
+
|
|
1052
|
+
"""
|
|
1053
|
+
beta = np.array(beta, dtype=np.float64)
|
|
1054
|
+
cov_matrix = np.array(cov, dtype=np.float64)
|
|
1055
|
+
g = np.array([x ** k for k in range(len(beta))]).reshape(-1, 1)
|
|
1056
|
+
y = np.dot(g.T, beta.reshape(-1, 1))[0, 0]
|
|
1057
|
+
var = np.matmul(np.matmul(g.T, cov_matrix), g)[0, 0]
|
|
1058
|
+
return y, var
|
|
1059
|
+
|
|
1060
|
+
|
|
1061
|
+
def average_var(beta: list, cov: list, x: float):
|
|
978
1062
|
""" y = b0 * x^0 + b1 * x^1 + ... + bn * x^n
|
|
979
1063
|
Parameters
|
|
980
1064
|
----------
|
|
@@ -985,21 +1069,29 @@ def linear_eq(x: list, beta: list):
|
|
|
985
1069
|
-------
|
|
986
1070
|
|
|
987
1071
|
"""
|
|
988
|
-
|
|
1072
|
+
y, = beta
|
|
1073
|
+
var = np.array(cov, dtype=np.float64)[0, 0]
|
|
1074
|
+
return y, var
|
|
989
1075
|
|
|
990
1076
|
|
|
991
|
-
def
|
|
992
|
-
""" y = a *
|
|
1077
|
+
def exponential_var(beta: list, cov: list, x: float):
|
|
1078
|
+
""" y = a * exp(bx) + c
|
|
993
1079
|
Parameters
|
|
994
1080
|
----------
|
|
995
1081
|
beta : coefficients, [a, b, c]
|
|
996
|
-
|
|
1082
|
+
cov : covariance [[], [], []]
|
|
997
1083
|
|
|
998
1084
|
Returns
|
|
999
1085
|
-------
|
|
1000
1086
|
|
|
1001
1087
|
"""
|
|
1002
|
-
|
|
1088
|
+
cov_matrix = np.array(cov)
|
|
1089
|
+
a, b, c = beta
|
|
1090
|
+
k = np.exp(b*x)
|
|
1091
|
+
g = np.array([k, a*x*k, 1]).reshape(-1, 1)
|
|
1092
|
+
y = a * k + c
|
|
1093
|
+
var = np.matmul(np.matmul(g.T, cov_matrix), g)[0, 0]
|
|
1094
|
+
return y, var
|
|
1003
1095
|
|
|
1004
1096
|
|
|
1005
1097
|
def power_eq(x: list, beta: list):
|
ararpy/files/arr_file.py
CHANGED
|
@@ -26,7 +26,8 @@ def save(file_path, sample):
|
|
|
26
26
|
-------
|
|
27
27
|
str, file name
|
|
28
28
|
"""
|
|
29
|
-
|
|
29
|
+
if not file_path.endswith(".arr"):
|
|
30
|
+
file_path = os.path.join(file_path, f"{sample.Info.sample.name}.arr")
|
|
30
31
|
with open(file_path, 'wb') as f:
|
|
31
32
|
f.write(pickle.dumps(sample))
|
|
32
33
|
# with open(file_path, 'w') as f:
|
ararpy/files/basic.py
CHANGED
|
@@ -12,31 +12,9 @@
|
|
|
12
12
|
|
|
13
13
|
import os
|
|
14
14
|
import pickle
|
|
15
|
-
import traceback
|
|
16
15
|
import json
|
|
17
16
|
|
|
18
17
|
|
|
19
|
-
def upload(file, media_dir):
|
|
20
|
-
try:
|
|
21
|
-
name, suffix = os.path.splitext(file.name)
|
|
22
|
-
if suffix.lower() not in [
|
|
23
|
-
'.xls', '.age', '.xlsx', '.arr', '.jpg', '.png', '.txt',
|
|
24
|
-
'.log', '.seq', '.json', '.ahd', '.csv']:
|
|
25
|
-
raise TypeError(f"The imported file is not supported: {suffix}")
|
|
26
|
-
web_file_path = os.path.join(media_dir, file.name)
|
|
27
|
-
with open(web_file_path, 'wb') as f:
|
|
28
|
-
for chunk in file.chunks():
|
|
29
|
-
f.write(chunk)
|
|
30
|
-
print("File path on the server: %s" % web_file_path)
|
|
31
|
-
except PermissionError:
|
|
32
|
-
raise ValueError(f'Permission denied')
|
|
33
|
-
except (Exception, BaseException) as e:
|
|
34
|
-
print(traceback.format_exc())
|
|
35
|
-
raise ValueError(f'Error in opening file: {e}')
|
|
36
|
-
else:
|
|
37
|
-
return web_file_path, name, suffix
|
|
38
|
-
|
|
39
|
-
|
|
40
18
|
def read(file_path):
|
|
41
19
|
""" Read text files, default 'r', 'rb'
|
|
42
20
|
Parameters
|