Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
112 changes: 112 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,112 @@
glmnet_python/GLMnet.so

# Byte-compiled / optimized / DLL files
__pycache__/
*.py[cod]
*$py.class

# C extensions
*.so

# Distribution / packaging
.Python
build/
develop-eggs/
dist/
downloads/
eggs/
.eggs/
lib/
lib64/
parts/
sdist/
var/
wheels/
*.egg-info/
.installed.cfg
*.egg
MANIFEST

# PyInstaller
# Usually these files are written by a python script from a template
# before PyInstaller builds the exe, so as to inject date/other infos into it.
*.manifest
*.spec

# Installer logs
pip-log.txt
pip-delete-this-directory.txt

# Unit test / coverage reports
htmlcov/
.tox/
.coverage
.coverage.*
.cache
nosetests.xml
coverage.xml
*.cover
.hypothesis/
.pytest_cache/

# Translations
*.mo
*.pot

# Django stuff:
*.log
local_settings.py
db.sqlite3

# Flask stuff:
instance/
.webassets-cache

# Scrapy stuff:
.scrapy

# Sphinx documentation
docs/_build/

# PyBuilder
target/

# Jupyter Notebook
.ipynb_checkpoints

# IPython
profile_default/
ipython_config.py

# pyenv
.python-version

# celery beat schedule file
celerybeat-schedule

# SageMath parsed files
*.sage.py

# Environments
.env
.venv
env/
venv/
ENV/
env.bak/
venv.bak/

# Spyder project settings
.spyderproject
.spyproject

# Rope project settings
.ropeproject

# mkdocs documentation
/site

# mypy
.mypy_cache/
.dmypy.json
dmypy.json
Binary file removed glmnet_python/GLMnet.so
Binary file not shown.
64 changes: 32 additions & 32 deletions glmnet_python/coxnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
status -- column 1
"""
# import packages/methods
import scipy
import numpy
import ctypes
from loadGlmLib import loadGlmLib

Expand All @@ -20,7 +20,7 @@ def coxnet(x, is_sparse, irs, pcs, y, weights, offset, parm,
# pre-process data
ty = y[:, 0]
tevent = y[:, 1]
if scipy.any(ty <= 0):
if numpy.any(ty <= 0):
raise ValueError('negative event time not permitted for cox family')
if len(offset) == 0:
offset = ty*0
Expand All @@ -35,17 +35,17 @@ def coxnet(x, is_sparse, irs, pcs, y, weights, offset, parm,
######################################
# force inputs into fortran order and scipy float64
copyFlag = False
x = x.astype(dtype = scipy.float64, order = 'F', copy = copyFlag)
irs = irs.astype(dtype = scipy.int32, order = 'F', copy = copyFlag)
pcs = pcs.astype(dtype = scipy.int32, order = 'F', copy = copyFlag)
ty = ty.astype(dtype = scipy.float64, order = 'F', copy = copyFlag)
tevent = tevent.astype(dtype = scipy.float64, order = 'F', copy = copyFlag)
offset = offset.astype(dtype = scipy.float64, order = 'F', copy = copyFlag)
weights = weights.astype(dtype = scipy.float64, order = 'F', copy = copyFlag)
jd = jd.astype(dtype = scipy.int32, order = 'F', copy = copyFlag)
vp = vp.astype(dtype = scipy.float64, order = 'F', copy = copyFlag)
cl = cl.astype(dtype = scipy.float64, order = 'F', copy = copyFlag)
ulam = ulam.astype(dtype = scipy.float64, order = 'F', copy = copyFlag)
x = x.astype(dtype = numpy.float64, order = 'F', copy = copyFlag)
irs = irs.astype(dtype = numpy.int32, order = 'F', copy = copyFlag)
pcs = pcs.astype(dtype = numpy.int32, order = 'F', copy = copyFlag)
ty = ty.astype(dtype = numpy.float64, order = 'F', copy = copyFlag)
tevent = tevent.astype(dtype = numpy.float64, order = 'F', copy = copyFlag)
offset = offset.astype(dtype = numpy.float64, order = 'F', copy = copyFlag)
weights = weights.astype(dtype = numpy.float64, order = 'F', copy = copyFlag)
jd = jd.astype(dtype = numpy.int32, order = 'F', copy = copyFlag)
vp = vp.astype(dtype = numpy.float64, order = 'F', copy = copyFlag)
cl = cl.astype(dtype = numpy.float64, order = 'F', copy = copyFlag)
ulam = ulam.astype(dtype = numpy.float64, order = 'F', copy = copyFlag)

######################################
# --------- ALLOCATE OUTPUTS ---------
Expand All @@ -54,24 +54,24 @@ def coxnet(x, is_sparse, irs, pcs, y, weights, offset, parm,
lmu = -1
lmu_r = ctypes.c_int(lmu)
# ca
ca = scipy.zeros([nx, nlam], dtype = scipy.float64)
ca = ca.astype(dtype = scipy.float64, order = 'F', copy = False)
ca = numpy.zeros([nx, nlam], dtype = numpy.float64)
ca = ca.astype(dtype = numpy.float64, order = 'F', copy = False)
ca_r = ca.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
# ia
ia = -1*scipy.ones([nx], dtype = scipy.int32)
ia = ia.astype(dtype = scipy.int32, order = 'F', copy = False)
ia = -1*numpy.ones([nx], dtype = numpy.int32)
ia = ia.astype(dtype = numpy.int32, order = 'F', copy = False)
ia_r = ia.ctypes.data_as(ctypes.POINTER(ctypes.c_int))
# nin
nin = -1*scipy.ones([nlam], dtype = scipy.int32)
nin = nin.astype(dtype = scipy.int32, order = 'F', copy = False)
nin = -1*numpy.ones([nlam], dtype = numpy.int32)
nin = nin.astype(dtype = numpy.int32, order = 'F', copy = False)
nin_r = nin.ctypes.data_as(ctypes.POINTER(ctypes.c_int))
# dev
dev = -1*scipy.ones([nlam], dtype = scipy.float64)
dev = dev.astype(dtype = scipy.float64, order = 'F', copy = False)
dev = -1*numpy.ones([nlam], dtype = numpy.float64)
dev = dev.astype(dtype = numpy.float64, order = 'F', copy = False)
dev_r = dev.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
# alm
alm = -1*scipy.ones([nlam], dtype = scipy.float64)
alm = alm.astype(dtype = scipy.float64, order = 'F', copy = False)
alm = -1*numpy.ones([nlam], dtype = numpy.float64)
alm = alm.astype(dtype = numpy.float64, order = 'F', copy = False)
alm_r = alm.ctypes.data_as(ctypes.POINTER(ctypes.c_double))
# nlp
nlp = -1
Expand Down Expand Up @@ -146,21 +146,21 @@ def coxnet(x, is_sparse, irs, pcs, y, weights, offset, parm,
ninmax = max(nin)
# fix first value of alm (from inf to correct value)
if ulam[0] == 0.0:
t1 = scipy.log(alm[1])
t2 = scipy.log(alm[2])
alm[0] = scipy.exp(2*t1 - t2)
t1 = numpy.log(alm[1])
t2 = numpy.log(alm[2])
alm[0] = numpy.exp(2*t1 - t2)
# create return fit dictionary
if ninmax > 0:
ca = ca[0:ninmax, :]
df = scipy.sum(scipy.absolute(ca) > 0, axis=0)
df = numpy.sum(numpy.absolute(ca) > 0, axis=0)
ja = ia[0:ninmax] - 1 # ia is 1-indexed in fortran
oja = scipy.argsort(ja)
oja = numpy.argsort(ja)
ja1 = ja[oja]
beta = scipy.zeros([nvars, lmu], dtype = scipy.float64)
beta = numpy.zeros([nvars, lmu], dtype = numpy.float64)
beta[ja1, :] = ca[oja, :]
else:
beta = scipy.zeros([nvars, lmu], dtype = scipy.float64)
df = scipy.zeros([1, lmu], dtype = scipy.float64)
beta = numpy.zeros([nvars, lmu], dtype = numpy.float64)
df = numpy.zeros([1, lmu], dtype = numpy.float64)

fit = dict()
fit['beta'] = beta
Expand All @@ -170,7 +170,7 @@ def coxnet(x, is_sparse, irs, pcs, y, weights, offset, parm,
fit['lambdau'] = alm
fit['npasses'] = nlp_r.value
fit['jerr'] = jerr_r.value
fit['dim'] = scipy.array([nvars, lmu], dtype = scipy.integer)
fit['dim'] = numpy.array([nvars, lmu], dtype = numpy.integer)
fit['offset'] = is_offset
fit['class'] = 'coxnet'

Expand Down
16 changes: 8 additions & 8 deletions glmnet_python/cvcompute.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,24 +4,24 @@

Compute the weighted mean and SD within folds, and hence the SE of the mean
"""
import scipy
import numpy
from wtmean import wtmean

def cvcompute(mat, weights, foldid, nlams):
if len(weights.shape) > 1:
weights = scipy.reshape(weights, [weights.shape[0], ])
wisum = scipy.bincount(foldid, weights = weights)
nfolds = scipy.amax(foldid) + 1
outmat = scipy.ones([nfolds, mat.shape[1]])*scipy.NaN
good = scipy.zeros([nfolds, mat.shape[1]])
mat[scipy.isinf(mat)] = scipy.NaN
weights = numpy.reshape(weights, [weights.shape[0], ])
wisum = numpy.bincount(foldid, weights = weights)
nfolds = numpy.amax(foldid) + 1
outmat = numpy.ones([nfolds, mat.shape[1]])*numpy.NaN
good = numpy.zeros([nfolds, mat.shape[1]])
mat[numpy.isinf(mat)] = numpy.NaN
for i in range(nfolds):
tf = foldid == i
mati = mat[tf, ]
wi = weights[tf, ]
outmat[i, :] = wtmean(mati, wi)
good[i, 0:nlams[i]] = 1
N = scipy.sum(good, axis = 0)
N = numpy.sum(good, axis = 0)
cvcpt = dict()
cvcpt['cvraw'] = outmat
cvcpt['weights'] = wisum
Expand Down
18 changes: 9 additions & 9 deletions glmnet_python/cvelnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Internal cvglmnet function. See also cvglmnet.

"""
import scipy
import numpy
from glmnetPredict import glmnetPredict
from wtmean import wtmean
from cvcompute import cvcompute
Expand Down Expand Up @@ -31,29 +31,29 @@ def cvelnet(fit, \
if len(offset) > 0:
y = y - offset

predmat = scipy.ones([y.size, lambdau.size])*scipy.NAN
nfolds = scipy.amax(foldid) + 1
predmat = numpy.ones([y.size, lambdau.size])*numpy.NAN
nfolds = numpy.amax(foldid) + 1
nlams = []
for i in range(nfolds):
which = foldid == i
fitobj = fit[i].copy()
fitobj['offset'] = False
preds = glmnetPredict(fitobj, x[which, ])
nlami = scipy.size(fit[i]['lambdau'])
nlami = numpy.size(fit[i]['lambdau'])
predmat[which, 0:nlami] = preds
nlams.append(nlami)
# convert nlams to scipy array
nlams = scipy.array(nlams, dtype = scipy.integer)
nlams = numpy.array(nlams, dtype = numpy.integer)

N = y.shape[0] - scipy.sum(scipy.isnan(predmat), axis = 0)
yy = scipy.tile(y, [1, lambdau.size])
N = y.shape[0] - numpy.sum(numpy.isnan(predmat), axis = 0)
yy = numpy.tile(y, [1, lambdau.size])

if ptype == 'mse':
cvraw = (yy - predmat)**2
elif ptype == 'deviance':
cvraw = (yy - predmat)**2
elif ptype == 'mae':
cvraw = scipy.absolute(yy - predmat)
cvraw = numpy.absolute(yy - predmat)

if y.size/nfolds < 3 and grouped == True:
print('Option grouped=false enforced in cv.glmnet, since < 3 observations per fold')
Expand All @@ -67,7 +67,7 @@ def cvelnet(fit, \

cvm = wtmean(cvraw, weights)
sqccv = (cvraw - cvm)**2
cvsd = scipy.sqrt(wtmean(sqccv, weights)/(N-1))
cvsd = numpy.sqrt(wtmean(sqccv, weights)/(N-1))

result = dict()
result['cvm'] = cvm
Expand Down
24 changes: 12 additions & 12 deletions glmnet_python/cvfishnet.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
Internal cvglmnet function. See also cvglmnet.

"""
import scipy
import numpy
from glmnetPredict import glmnetPredict
from wtmean import wtmean
from cvcompute import cvcompute
Expand Down Expand Up @@ -34,32 +34,32 @@ def cvfishnet(fit, \
else:
is_offset = False

predmat = scipy.ones([y.size, lambdau.size])*scipy.NAN
nfolds = scipy.amax(foldid) + 1
predmat = numpy.ones([y.size, lambdau.size])*numpy.NAN
nfolds = numpy.amax(foldid) + 1
nlams = []
for i in range(nfolds):
which = foldid == i
fitobj = fit[i].copy()
if is_offset:
off_sub = offset[which]
else:
off_sub = scipy.empty([0])
off_sub = numpy.empty([0])
preds = glmnetPredict(fitobj, x[which, ], offset = off_sub)
nlami = scipy.size(fit[i]['lambdau'])
nlami = numpy.size(fit[i]['lambdau'])
predmat[which, 0:nlami] = preds
nlams.append(nlami)
# convert nlams to scipy array
nlams = scipy.array(nlams, dtype = scipy.integer)
nlams = numpy.array(nlams, dtype = numpy.integer)

N = y.shape[0] - scipy.sum(scipy.isnan(predmat), axis = 0)
yy = scipy.tile(y, [1, lambdau.size])
N = y.shape[0] - numpy.sum(numpy.isnan(predmat), axis = 0)
yy = numpy.tile(y, [1, lambdau.size])

if ptype == 'mse':
cvraw = (yy - predmat)**2
elif ptype == 'deviance':
cvraw = devi(yy, predmat)
elif ptype == 'mae':
cvraw = scipy.absolute(yy - predmat)
cvraw = numpy.absolute(yy - predmat)

if y.size/nfolds < 3 and grouped == True:
print('Option grouped=false enforced in cvglmnet, since < 3 observations per fold')
Expand All @@ -73,7 +73,7 @@ def cvfishnet(fit, \

cvm = wtmean(cvraw, weights)
sqccv = (cvraw - cvm)**2
cvsd = scipy.sqrt(wtmean(sqccv, weights)/(N-1))
cvsd = numpy.sqrt(wtmean(sqccv, weights)/(N-1))

result = dict()
result['cvm'] = cvm
Expand All @@ -88,8 +88,8 @@ def cvfishnet(fit, \
# end of cvfishnet
#=========================
def devi(yy, eta):
deveta = yy*eta - scipy.exp(eta)
devy = yy*scipy.log(yy) - yy
deveta = yy*eta - numpy.exp(eta)
devy = yy*numpy.log(yy) - yy
devy[yy == 0] = 0
result = 2*(devy - deveta)
return(result)
Expand Down
Loading