sciml 0.0.9__py3-none-any.whl → 0.0.10__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.
sciml/regress2.py ADDED
@@ -0,0 +1,217 @@
1
+ # Model type I and II regression, including RMA (reduced major axis regression)
2
+
3
+ """
4
+ Credit: UMaine MISC Lab; emmanuel.boss@maine.edu
5
+ http://misclab.umeoce.maine.edu/
6
+ https://github.com/OceanOptics
7
+ ------------------------------------------------------------------------------
8
+ MIT License
9
+
10
+ Copyright (c) [year] [fullname]
11
+
12
+ Permission is hereby granted, free of charge, to any person obtaining a copy
13
+ of this software and associated documentation files (the "Software"), to deal
14
+ in the Software without restriction, including without limitation the rights
15
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
16
+ copies of the Software, and to permit persons to whom the Software is
17
+ furnished to do so, subject to the following conditions:
18
+
19
+ The above copyright notice and this permission notice shall be included in all
20
+ copies or substantial portions of the Software.
21
+
22
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
23
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
24
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
25
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
26
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
27
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
28
+ SOFTWARE.
29
+ """
30
+
31
+ import statsmodels.api as sm
32
+ import numpy as np
33
+
34
+
35
+ def regress2(_x, _y, _method_type_1 = "ordinary least square",
36
+ _method_type_2 = "reduced major axis",
37
+ _weight_x = [], _weight_y = [], _need_intercept = True):
38
+ # Regression Type II based on statsmodels
39
+ # Type II regressions are recommended if there is variability on both x and y
40
+ # It's computing the linear regression type I for (x,y) and (y,x)
41
+ # and then average relationship with one of the type II methods
42
+ #
43
+ # INPUT:
44
+ # _x <np.array>
45
+ # _y <np.array>
46
+ # _method_type_1 <str> method to use for regression type I:
47
+ # ordinary least square or OLS <default>
48
+ # weighted least square or WLS
49
+ # robust linear model or RLM
50
+ # _method_type_2 <str> method to use for regression type II:
51
+ # major axis
52
+ # reduced major axis <default> (also known as geometric mean)
53
+ # arithmetic mean
54
+ # _need_intercept <bool>
55
+ # True <default> add a constant to relation (y = a x + b)
56
+ # False force relation by 0 (y = a x)
57
+ # _weight_x <np.array> containing the weigth of x
58
+ # _weigth_y <np.array> containing the weigth of y
59
+ #
60
+ # OUTPUT:
61
+ # slope
62
+ # intercept
63
+ # r
64
+ # std_slope
65
+ # std_intercept
66
+ # predict
67
+ #
68
+ # REQUIRE:
69
+ # numpy
70
+ # statsmodels
71
+ #
72
+ # The code is based on the matlab function of MBARI.
73
+ # AUTHOR: Nils Haentjens
74
+ # REFERENCE: https://www.mbari.org/products/research-software/matlab-scripts-linear-regressions/
75
+
76
+ # Check input
77
+ if _method_type_2 != "reduced major axis" and _method_type_1 != "ordinary least square":
78
+ raise ValueError("'" + _method_type_2 + "' only supports '" + _method_type_1 + "' method as type 1.")
79
+
80
+ # Set x, y depending on intercept requirement
81
+ if _need_intercept:
82
+ x_intercept = sm.add_constant(_x)
83
+ y_intercept = sm.add_constant(_y)
84
+
85
+ # Compute Regression Type I (if type II requires it)
86
+ if (_method_type_2 == "reduced major axis" or
87
+ _method_type_2 == "geometric mean"):
88
+ if _method_type_1 == "OLS" or _method_type_1 == "ordinary least square":
89
+ if _need_intercept:
90
+ [intercept_a, slope_a] = sm.OLS(_y, x_intercept).fit().params
91
+ [intercept_b, slope_b] = sm.OLS(_x, y_intercept).fit().params
92
+ else:
93
+ slope_a = sm.OLS(_y, _x).fit().params
94
+ slope_b = sm.OLS(_x, _y).fit().params
95
+ elif _method_type_1 == "WLS" or _method_type_1 == "weighted least square":
96
+ if _need_intercept:
97
+ [intercept_a, slope_a] = sm.WLS(
98
+ _y, x_intercept, weights=1. / _weight_y).fit().params
99
+ [intercept_b, slope_b] = sm.WLS(
100
+ _x, y_intercept, weights=1. / _weight_x).fit().params
101
+ else:
102
+ slope_a = sm.WLS(_y, _x, weights=1. / _weight_y).fit().params
103
+ slope_b = sm.WLS(_x, _y, weights=1. / _weight_x).fit().params
104
+ elif _method_type_1 == "RLM" or _method_type_1 == "robust linear model":
105
+ if _need_intercept:
106
+ [intercept_a, slope_a] = sm.RLM(_y, x_intercept).fit().params
107
+ [intercept_b, slope_b] = sm.RLM(_x, y_intercept).fit().params
108
+ else:
109
+ slope_a = sm.RLM(_y, _x).fit().params
110
+ slope_b = sm.RLM(_x, _y).fit().params
111
+ else:
112
+ raise ValueError("Invalid literal for _method_type_1: " + _method_type_1)
113
+
114
+ # Compute Regression Type II
115
+ if (_method_type_2 == "reduced major axis" or
116
+ _method_type_2 == "geometric mean"):
117
+ # Transpose coefficients
118
+ if _need_intercept:
119
+ intercept_b = -intercept_b / slope_b
120
+ slope_b = 1 / slope_b
121
+ # Check if correlated in same direction
122
+ if np.sign(slope_a) != np.sign(slope_b):
123
+ raise RuntimeError('Type I regressions of opposite sign.')
124
+ # Compute Reduced Major Axis Slope
125
+ slope = np.sign(slope_a) * np.sqrt(slope_a * slope_b)
126
+ if _need_intercept:
127
+ # Compute Intercept (use mean for least square)
128
+ if _method_type_1 == "OLS" or _method_type_1 == "ordinary least square":
129
+ intercept = np.mean(_y) - slope * np.mean(_x)
130
+ else:
131
+ intercept = np.median(_y) - slope * np.median(_x)
132
+ else:
133
+ intercept = 0
134
+ # Compute r
135
+ r = np.sign(slope_a) * np.sqrt(slope_a / slope_b)
136
+ # Compute predicted values
137
+ predict = slope * _x + intercept
138
+ # Compute standard deviation of the slope and the intercept
139
+ n = len(_x)
140
+ diff = _y - predict
141
+ Sx2 = np.sum(np.multiply(_x, _x))
142
+ den = n * Sx2 - np.sum(_x) ** 2
143
+ s2 = np.sum(np.multiply(diff, diff)) / (n - 2)
144
+ std_slope = np.sqrt(n * s2 / den)
145
+ if _need_intercept:
146
+ std_intercept = np.sqrt(Sx2 * s2 / den)
147
+ else:
148
+ std_intercept = 0
149
+ elif (_method_type_2 == "Pearson's major axis" or
150
+ _method_type_2 == "major axis"):
151
+ if not _need_intercept:
152
+ raise ValueError("Invalid value for _need_intercept: " + str(_need_intercept))
153
+ xm = np.mean(_x)
154
+ ym = np.mean(_y)
155
+ xp = _x - xm
156
+ yp = _y - ym
157
+ sumx2 = np.sum(np.multiply(xp, xp))
158
+ sumy2 = np.sum(np.multiply(yp, yp))
159
+ sumxy = np.sum(np.multiply(xp, yp))
160
+ slope = ((sumy2 - sumx2 + np.sqrt((sumy2 - sumx2)**2 + 4 * sumxy**2)) /
161
+ (2 * sumxy))
162
+ intercept = ym - slope * xm
163
+ # Compute r
164
+ r = sumxy / np.sqrt(sumx2 * sumy2)
165
+ # Compute standard deviation of the slope and the intercept
166
+ n = len(_x)
167
+ std_slope = (slope / r) * np.sqrt((1 - r ** 2) / n)
168
+ sigx = np.sqrt(sumx2 / (n - 1))
169
+ sigy = np.sqrt(sumy2 / (n - 1))
170
+ std_i1 = (sigy - sigx * slope) ** 2
171
+ std_i2 = (2 * sigx * sigy) + ((xm ** 2 * slope * (1 + r)) / r ** 2)
172
+ std_intercept = np.sqrt((std_i1 + ((1 - r) * slope * std_i2)) / n)
173
+ # Compute predicted values
174
+ predict = slope * _x + intercept
175
+ elif _method_type_2 == "arithmetic mean":
176
+ if not _need_intercept:
177
+ raise ValueError("Invalid value for _need_intercept: " + str(_need_intercept))
178
+ n = len(_x)
179
+ sg = np.floor(n / 2)
180
+ # Sort x and y in order of x
181
+ sorted_index = sorted(range(len(_x)), key=lambda i: _x[i])
182
+ x_w = np.array([_x[i] for i in sorted_index])
183
+ y_w = np.array([_y[i] for i in sorted_index])
184
+ x1 = x_w[1:sg + 1]
185
+ x2 = x_w[sg:n]
186
+ y1 = y_w[1:sg + 1]
187
+ y2 = y_w[sg:n]
188
+ x1m = np.mean(x1)
189
+ x2m = np.mean(x2)
190
+ y1m = np.mean(y1)
191
+ y2m = np.mean(y2)
192
+ xm = (x1m + x2m) / 2
193
+ ym = (y1m + y2m) / 2
194
+ slope = (x2m - x1m) / (y2m - y1m)
195
+ intercept = ym - xm * slope
196
+ # r (to verify)
197
+ r = []
198
+ # Compute predicted values
199
+ predict = slope * _x + intercept
200
+ # Compute standard deviation of the slope and the intercept
201
+ std_slope = []
202
+ std_intercept = []
203
+
204
+ # Return all that
205
+ return {"slope": float(slope), "intercept": intercept, "r": r,
206
+ "std_slope": std_slope, "std_intercept": std_intercept,
207
+ "predict": predict}
208
+
209
+
210
+ # if __name__ == '__main__':
211
+ # x = np.linspace(0, 10, 100)
212
+ # # Add random error on y
213
+ # e = np.random.normal(size=len(x))
214
+ # y = x + e
215
+ # results = regress2(x, y, _method_type_2="reduced major axis",
216
+ # _need_intercept=False)
217
+ # # print(results)
@@ -1,21 +1,21 @@
1
- MIT License
2
-
3
- Copyright (c) 2021 Zhu
4
-
5
- Permission is hereby granted, free of charge, to any person obtaining a copy
6
- of this software and associated documentation files (the "Software"), to deal
7
- in the Software without restriction, including without limitation the rights
8
- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
- copies of the Software, and to permit persons to whom the Software is
10
- furnished to do so, subject to the following conditions:
11
-
12
- The above copyright notice and this permission notice shall be included in all
13
- copies or substantial portions of the Software.
14
-
15
- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
- SOFTWARE.
1
+ MIT License
2
+
3
+ Copyright (c) 2021 Zhu
4
+
5
+ Permission is hereby granted, free of charge, to any person obtaining a copy
6
+ of this software and associated documentation files (the "Software"), to deal
7
+ in the Software without restriction, including without limitation the rights
8
+ to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9
+ copies of the Software, and to permit persons to whom the Software is
10
+ furnished to do so, subject to the following conditions:
11
+
12
+ The above copyright notice and this permission notice shall be included in all
13
+ copies or substantial portions of the Software.
14
+
15
+ THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16
+ IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17
+ FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18
+ AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19
+ LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20
+ OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21
+ SOFTWARE.
@@ -1,13 +1,13 @@
1
- Metadata-Version: 2.1
2
- Name: sciml
3
- Version: 0.0.9
4
- Summary: draw and basic calculations/conversions
5
- Home-page: https://github.com/soonyenju/sciml
6
- Author: Songyan Zhu
7
- Author-email: zhusy93@gmail.com
8
- License: MIT Licence
9
- Keywords: Scientific machine learning wrappers
10
- Platform: any
11
- License-File: LICENSE
12
-
13
- coming soon
1
+ Metadata-Version: 2.1
2
+ Name: sciml
3
+ Version: 0.0.10
4
+ Summary: draw and basic calculations/conversions
5
+ Home-page: https://github.com/soonyenju/sciml
6
+ Author: Songyan Zhu
7
+ Author-email: zhusy93@gmail.com
8
+ License: MIT Licence
9
+ Keywords: Scientific machine learning wrappers
10
+ Platform: any
11
+ License-File: LICENSE
12
+
13
+ coming soon
@@ -0,0 +1,11 @@
1
+ sciml/__init__.py,sha256=BqRVu5DbfbnxksBXhe4gH_uulPdqTjSaSO1LvGkc37Q,79
2
+ sciml/ccc.py,sha256=AE1l46hvh18_Q9_BQufMjsGF9-JfsTw2hrT1CbgBHE8,1210
3
+ sciml/metrics.py,sha256=ICEeH6jwmpdx9jxwYSzB_YTvbyBq9AEUYqkZiVS1ZGs,3577
4
+ sciml/models.py,sha256=qc2LgdpSkq9kGMnLKZTnyuwzytCu6R8hyU5i6PaI7Qw,10345
5
+ sciml/pipelines.py,sha256=NGBwl5vA0Uq5GO-VtIow_k42K7HoVwxPQrkW-jINflY,8381
6
+ sciml/regress2.py,sha256=GSZ4IqmyF9u3PGOhHIKV0Rb_C2pI8eJ3jGJBa1IrEXM,8978
7
+ sciml-0.0.10.dist-info/LICENSE,sha256=dX4jBmkgQPWc_TfYkXtKQzVIgZQWFuHZ8vQjV4sEeV4,1060
8
+ sciml-0.0.10.dist-info/METADATA,sha256=iMcI6kpM6IX2oBhx9JwmI77JiX2bZPWI93dHta_jkCM,314
9
+ sciml-0.0.10.dist-info/WHEEL,sha256=2wepM1nk4DS4eFpYrW1TTqPcoGNfHhhO_i5m4cOimbo,92
10
+ sciml-0.0.10.dist-info/top_level.txt,sha256=dS_7aBCZFKQE3myPy5sh4USjQZCZyGg382-YxUUYcdw,6
11
+ sciml-0.0.10.dist-info/RECORD,,
@@ -1,5 +1,5 @@
1
1
  Wheel-Version: 1.0
2
- Generator: bdist_wheel (0.43.0)
2
+ Generator: bdist_wheel (0.38.4)
3
3
  Root-Is-Purelib: true
4
4
  Tag: py3-none-any
5
5
 
sciml/utils.py DELETED
@@ -1,46 +0,0 @@
1
- import numpy as np
2
- import pandas as pd
3
- from sklearn.model_selection import ShuffleSplit
4
- from sklearn.model_selection import train_test_split
5
-
6
- # randomly select sites
7
- def random_select(ds, count, num, random_state = 0):
8
- np.random.seed(random_state)
9
- idxs = np.random.choice(np.delete(np.arange(len(ds)), count), num, replace = False)
10
- return np.sort(idxs)
11
-
12
- def split(Xs, ys, return_index = False, test_size = 0.33, random_state = 42):
13
- if return_index:
14
- sss = ShuffleSplit(n_splits=1, test_size = test_size, random_state = random_state)
15
- sss.get_n_splits(Xs, ys)
16
- train_index, test_index = next(sss.split(Xs, ys))
17
- return (train_index, test_index)
18
- else:
19
- X_train, X_test, y_train, y_test = train_test_split(
20
- Xs, ys,
21
- test_size = test_size,
22
- random_state = random_state
23
- )
24
- return (X_train, X_test, y_train, y_test)
25
-
26
- def split_cut(Xs, ys, test_ratio = 0.33):
27
- assert ys.ndim == 2, 'ys must be 2D!'
28
- assert len(Xs) == len(ys), 'Xs and ys should be equally long!'
29
- assert type(Xs) == type(ys), 'Xs and ys should be the same data type!'
30
- if not type(Xs) in [pd.core.frame.DataFrame, np.ndarray]: raise Exception('Only accept numpy ndarray or pandas dataframe')
31
- anchor = int(np.floor(len(ys) * (1 - test_ratio)))
32
-
33
- if type(Xs) == pd.core.frame.DataFrame:
34
- X_train = Xs.iloc[0: anchor, :]
35
- X_test = Xs.iloc[anchor::, :]
36
- y_train = ys.iloc[0: anchor, :]
37
- y_test = ys.iloc[anchor::, :]
38
- else:
39
- X_train = Xs[0: anchor, :]
40
- X_test = Xs[anchor::, :]
41
- y_train = ys[0: anchor, :]
42
- y_test = ys[anchor::, :]
43
-
44
- assert len(X_train) + len(X_test) == len(Xs), 'The sum of train and test lengths must equal to Xs/ys!'
45
-
46
- return (X_train, X_test, y_train, y_test)
@@ -1,9 +0,0 @@
1
- sciml/__init__.py,sha256=wtdlXERN2ik7NT_TQxFdd2gdodBY9vSU1ClSdeJnLm4,59
2
- sciml/models.py,sha256=BjbliW-KNfzbNdGNgM7nBdJ2SF2z21qCoAvug_v0FEg,10574
3
- sciml/pipelines.py,sha256=ReNEkQbdFn04D5G2tbxcA7jdSwACy8SnmZ8bFZI_oqE,15702
4
- sciml/utils.py,sha256=qCdABaTUu3K0R269jI7D_8SO6AqEjphg03CzdxCJR2k,1876
5
- sciml-0.0.9.dist-info/LICENSE,sha256=hcunSTJmVgRcUNOa1rKl8axtY3Jsy2B4wXDYtQsrAt0,1081
6
- sciml-0.0.9.dist-info/METADATA,sha256=S5hG3pP3x4yDPe8AJOKn4R-fIuvL-DL1GSKqGqiImSw,326
7
- sciml-0.0.9.dist-info/WHEEL,sha256=GJ7t_kWBFywbagK5eo9IoUwLW6oyOeTKmQ-9iHFVNxQ,92
8
- sciml-0.0.9.dist-info/top_level.txt,sha256=dS_7aBCZFKQE3myPy5sh4USjQZCZyGg382-YxUUYcdw,6
9
- sciml-0.0.9.dist-info/RECORD,,