Skip to content

Commit

Permalink
Merge pull request #383 from oooo26/master
Browse files Browse the repository at this point in the history
Update pypi workflow & improve codecov
  • Loading branch information
oooo26 authored Mar 11, 2022
2 parents 31198ec + 516111d commit 2cd72d1
Show file tree
Hide file tree
Showing 7 changed files with 167 additions and 52 deletions.
5 changes: 3 additions & 2 deletions .github/workflows/pypi.yml
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,11 @@ jobs:
- name: Check metadata
run: |
pipx run twine check dist/*
realpath dist/*
- uses: actions/upload-artifact@v2
- uses: actions/upload-artifact@v3
with:
path: dist/*.tar.gz
path: python/dist/*

build_wheels:
name: Wheels on ${{ matrix.os }}
Expand Down
2 changes: 1 addition & 1 deletion python/abess/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
# @Site :
# @File : __init__.py

__version__ = "0.4.2"
__version__ = "0.4.3"
__author__ = ("Jin Zhu, Kangkang Jiang, "
"Junhao Huang, Yanhang Zhang, "
"Yanhang Zhang, Shiyun Lin, "
Expand Down
38 changes: 19 additions & 19 deletions python/abess/decomposition.py
Original file line number Diff line number Diff line change
Expand Up @@ -110,14 +110,14 @@ def ratio(self, X):
"""
X = new_data_check(self, X)
s = np.cov(X.T)
if len(self.coef_.shape) == 1:
explain = self.coef_.T.dot(s).dot(self.coef_)
else:
explain = np.sum(np.diag(self.coef_.T.dot(s).dot(self.coef_)))
if isinstance(s, (int, float)):
full = s
else:
full = np.sum(np.diag(s))
# if len(self.coef_.shape) == 1:
# explain = self.coef_.T.dot(s).dot(self.coef_)
# else:
explain = np.sum(np.diag(self.coef_.T.dot(s).dot(self.coef_)))
# if isinstance(s, (int, float)):
# full = s
# else:
full = np.sum(np.diag(s))
return explain / full

def fit(self, X=None, is_normal=False,
Expand Down Expand Up @@ -261,7 +261,7 @@ def fit(self, X=None, is_normal=False,
elif self.screening_size > p:
raise ValueError(
"screening size should be smaller than X.shape[1].")
elif self.screening_size < max(support_sizes):
elif self.screening_size < np.nonzero(support_sizes)[0].max() + 1:
raise ValueError(
"screening size should be more than max(support_size).")

Expand Down Expand Up @@ -290,11 +290,11 @@ def fit(self, X=None, is_normal=False,
"number should be an positive integer and"
" not bigger than X.shape[1].")

# Important_search
if (not isinstance(self.important_search, int)
or self.important_search < 0):
raise ValueError(
"important_search should be a non-negative number.")
# # Important_search
# if (not isinstance(self.important_search, int)
# or self.important_search < 0):
# raise ValueError(
# "important_search should be a non-negative number.")

# A_init
if A_init is None:
Expand Down Expand Up @@ -547,11 +547,11 @@ def fit(self, X, r, group=None, A_init=None):
if self.splicing_type not in (0, 1):
raise ValueError("splicing type should be 0 or 1.")

# Important_search
if (not isinstance(self.important_search, int)
or self.important_search < 0):
raise ValueError(
"important_search should be a non-negative number.")
# # Important_search
# if (not isinstance(self.important_search, int)
# or self.important_search < 0):
# raise ValueError(
# "important_search should be a non-negative number.")

# A_init
if A_init is None:
Expand Down
47 changes: 24 additions & 23 deletions python/abess/functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ def __call__(self, x):
return value[0]
return value

def __repr__(self):
return "StepFunction(x=%r, y=%r, a=%r, b=%r)" % (
self.x, self.y, self.a, self.b)
# def __repr__(self):
# return "StepFunction(x=%r, y=%r, a=%r, b=%r)" % (
# self.x, self.y, self.a, self.b)


class BreslowEstimator:
Expand Down Expand Up @@ -125,25 +125,25 @@ def fit(self, linear_predictor, event, time):
np.exp(- self.cum_baseline_hazard_.y))
return self

def get_cumulative_hazard_function(self, linear_predictor):
r"""Predict cumulative hazard function.
Parameters
----------
linear_predictor : array-like, shape = (n_samples,)
Linear predictor of risk: `X @ coef`.
Returns
-------
cum_hazard : ndarray, shape = (n_samples,)
Predicted cumulative hazard functions.
"""
risk_score = np.exp(linear_predictor)
n_samples = risk_score.shape[0]
funcs = np.empty(n_samples, dtype=object)
for i in range(n_samples):
funcs[i] = StepFunction(x=self.cum_baseline_hazard_.x,
y=self.cum_baseline_hazard_.y,
a=risk_score[i])
return funcs
# def get_cumulative_hazard_function(self, linear_predictor):
# r"""Predict cumulative hazard function.
# Parameters
# ----------
# linear_predictor : array-like, shape = (n_samples,)
# Linear predictor of risk: `X @ coef`.
# Returns
# -------
# cum_hazard : ndarray, shape = (n_samples,)
# Predicted cumulative hazard functions.
# """
# risk_score = np.exp(linear_predictor)
# n_samples = risk_score.shape[0]
# funcs = np.empty(n_samples, dtype=object)
# for i in range(n_samples):
# funcs[i] = StepFunction(x=self.cum_baseline_hazard_.x,
# y=self.cum_baseline_hazard_.y,
# a=risk_score[i])
# return funcs

def get_survival_function(self, linear_predictor):
r"""Predict survival function.
Expand All @@ -164,7 +164,8 @@ def get_survival_function(self, linear_predictor):
y=np.power(self.baseline_survival_.y, risk_score[i]))
return funcs

def _compute_counts(self ,event, time, order=None):
@staticmethod
def _compute_counts(event, time, order=None):
"""Count right censored and uncensored samples at each unique time point.
Parameters
Expand Down
10 changes: 5 additions & 5 deletions python/abess/utilities.py
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,9 @@ def categorical_to_dummy(x, classes=None):
for i, x_i in enumerate(x):
if x_i in classes:
dummy_x[i, index[x_i]] = 1
else:
print(
"Data {} (index {}) is not in classes.".format(
x_i,
i))
# else:
# print(
# "Data {} (index {}) is not in classes.".format(
# x_i,
# i))
return dummy_x
17 changes: 15 additions & 2 deletions python/pytest/test_alg.py
Original file line number Diff line number Diff line change
Expand Up @@ -176,6 +176,11 @@ def assert_reg(coef):
# assert_fit(model1.coef_, model2.coef_) # TODO
assert_reg(model2.coef_)

# survival function
surv = model1.predict_survival_function(data.x)
time_points = np.quantile(data.y[:, 0], np.linspace(0, 0.6, 100))
surv[0](time_points)

@staticmethod
def test_poisson():
np.random.seed(9)
Expand Down Expand Up @@ -379,8 +384,12 @@ def test_PCA():

# ic
for ic in ['aic', 'bic', 'ebic', 'gic']:
model4 = abess.SparsePCA(support_size=support_size, ic_type=ic)
model4.fit(X, is_normal=False)
model = abess.SparsePCA(support_size=support_size, ic_type=ic)
model.fit(X, is_normal=False)

# A_init
model = abess.SparsePCA(support_size=support_size)
model.fit(X, A_init=[0, 1, 2])

@staticmethod
def test_gamma():
Expand Down Expand Up @@ -440,6 +449,10 @@ def test_RPCA():
model4 = abess.RobustPCA(support_size=s, ic_type=ic)
model4.fit(X, r=r)

# always select
model5 = abess.RobustPCA(support_size=s, always_select=[1])
model5.fit(X, r=r)

@staticmethod
def test_ordinal():
np.random.seed(0)
Expand Down
100 changes: 100 additions & 0 deletions python/pytest/test_check.py
Original file line number Diff line number Diff line change
Expand Up @@ -272,15 +272,27 @@ def test_base():
else:
assert False

try:
data = abess.make_glm_data(n=100, p=10, k=3, family='gaussian')
model = abess.LinearRegression()
model.fit(data.x, data.y)
model.score(data.x[:, 1:], data.y)
except ValueError as e:
print(e)
else:
assert False

# lack of necessary parameter
try:
model = abess.LinearRegression()
model.fit(X=[[1]])
except ValueError as e:
print(e)
else:
assert False

try:
model = abess.LinearRegression()
model.fit(y=[1])
except ValueError as e:
print(e)
Expand Down Expand Up @@ -361,6 +373,14 @@ def test_pca():
else:
assert False

try:
model = abess.SparsePCA(screening_size=np.ones((100, 1)))
model.fit(data)
except ValueError as e:
print(e)
else:
assert False

# screening_size
model = abess.SparsePCA(screening_size=0)
model.fit(data)
Expand All @@ -373,6 +393,14 @@ def test_pca():
else:
assert False

try:
model = abess.SparsePCA(screening_size=1, support_size=2)
model.fit(data)
except ValueError as e:
print(e)
else:
assert False

# lack of necessary parameter
try:
model.fit()
Expand Down Expand Up @@ -444,6 +472,38 @@ def test_pca():
else:
assert False

try:
model1 = abess.SparsePCA(exchange_num=-1)
model1.fit([[1]])
except ValueError as e:
print(e)
else:
assert False

try:
model1 = abess.SparsePCA(thread=-1)
model1.fit([[1]])
except ValueError as e:
print(e)
else:
assert False

try:
model1 = abess.SparsePCA()
model.fit([[1]], A_init=[[0, 1, 2]])
except ValueError as e:
print(e)
else:
assert False

try:
model1 = abess.SparsePCA()
model.fit([[1]], A_init=[-1])
except ValueError as e:
print(e)
else:
assert False

@staticmethod
def test_rpca():
model = abess.RobustPCA()
Expand Down Expand Up @@ -507,3 +567,43 @@ def test_rpca():
print(e)
else:
assert False

try:
model1 = abess.RobustPCA(support_size=[100])
model1.fit([[1]], r=1)
except ValueError as e:
print(e)
else:
assert False

try:
model1 = abess.RobustPCA()
model1.fit([[1]], r=0.1)
except ValueError as e:
print(e)
else:
assert False

try:
model1 = abess.RobustPCA(exchange_num=-1)
model1.fit([[1]], r=1)
except ValueError as e:
print(e)
else:
assert False

try:
model1 = abess.RobustPCA(splicing_type=-1)
model1.fit([[1]], r=1)
except ValueError as e:
print(e)
else:
assert False

try:
model1 = abess.RobustPCA(thread=-1)
model1.fit([[1]], r=1)
except ValueError as e:
print(e)
else:
assert False

0 comments on commit 2cd72d1

Please sign in to comment.