Skip to content

Commit bfe9fb0

Browse files
committed
python2 compat
1 parent 2b43d2e commit bfe9fb0

8 files changed

+263
-155
lines changed

aux/demo_ll.png

6 Bytes
Loading

aux/demo_smooth.png

2 Bytes
Loading

pylds/util.pxd pylds/cyutil.pxd

+1-1
Original file line numberDiff line numberDiff line change
@@ -44,4 +44,4 @@ cdef inline void copy_upper_lower(int n, floating *x) nogil:
4444
for i in range(n):
4545
for j in range(i):
4646
x[n*i+j] = x[n*j+i]
47-
47+

pylds/lds_info_messages.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ from numpy.math cimport INFINITY, PI
1313
from scipy.linalg.cython_blas cimport dsymm, dcopy, dgemm, dgemv, daxpy, dsyrk, \
1414
dtrmv, dger, dnrm2, ddot
1515
from scipy.linalg.cython_lapack cimport dpotrf, dpotrs, dpotri, dtrtrs
16-
from util cimport copy_transpose, copy_upper_lower
16+
from cyutil cimport copy_transpose, copy_upper_lower
1717

1818

1919
# TODO instead of specializing last step in info filtering and rts, we could

pylds/lds_messages.pyx

+1-1
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ from numpy.math cimport INFINITY, PI
1313
from scipy.linalg.cython_blas cimport dsymm, dcopy, dgemm, dgemv, daxpy, dsyrk, \
1414
dtrmv, dger, dnrm2, ddot
1515
from scipy.linalg.cython_lapack cimport dpotrf, dpotrs, dpotri, dtrtrs
16-
from util cimport copy_transpose, copy_upper_lower
16+
from cyutil cimport copy_transpose, copy_upper_lower
1717

1818
# NOTE: because the matrix operations are done in Fortran order but the code
1919
# expects C ordered arrays as input, the BLAS and LAPACK function calls mark

pylds/models.py

+6-36
Original file line numberDiff line numberDiff line change
@@ -5,21 +5,15 @@
55
from pybasicbayes.abstractions import Model, ModelGibbsSampling, \
66
ModelEM, ModelMeanField, ModelMeanFieldSVI
77

8-
from pybasicbayes.distributions import DiagonalRegression
9-
8+
from pybasicbayes.distributions import DiagonalRegression, Gaussian, Regression
9+
from pylds.distributions import PoissonRegression
1010
from pylds.states import LDSStates, LDSStatesZeroInflatedCountData, LaplaceApproxPoissonLDSStates
11+
from pylds.util import random_rotation
1112

12-
# TODO make separate versions for stationary, nonstationary,
13-
# nonstationary-and-distinct-for-each-sequence
14-
15-
# NOTE: dynamics_distn should probably be an instance of AutoRegression,
16-
# emission_distn should probably be an instance of Regression, and
13+
# NOTE: dynamics_distn should be an instance of Regression,
14+
# emission_distn should be an instance of Regression, and
1715
# init_dynamics_distn should probably be an instance of Gaussian
1816

19-
######################
20-
# algorithm mixins #
21-
######################
22-
2317
class _LDSBase(Model):
2418

2519
_states_class = LDSStates
@@ -381,7 +375,7 @@ def sigma_init(self, sigma_init):
381375
class ZeroInflatedCountLDS(_LDSGibbsSampling, _LDSBase):
382376
_states_class = LDSStatesZeroInflatedCountData
383377

384-
def __init__(self, *args, rho=1.0, **kwargs):
378+
def __init__(self, rho, *args, **kwargs):
385379
"""
386380
:param rho: Probability of count drawn from model
387381
With pr 1-rho, the emission is deterministically zero
@@ -401,7 +395,6 @@ def _generate_obs(self,s, inputs):
401395
x=np.hstack((s.gaussian_states, inputs)), return_xy=False)
402396

403397
# Zero out data
404-
from scipy.sparse import csr_matrix
405398
zeros = np.random.rand(s.T, self.D_obs) > self.rho
406399
data[zeros] = 0
407400
s.data = csr_matrix(data)
@@ -509,25 +502,6 @@ def expected_log_likelihood(self):
509502
##############################
510503

511504
# TODO make data-dependent default constructors
512-
513-
from pybasicbayes.distributions import Regression
514-
515-
516-
def random_rotation(n, theta=None):
517-
if theta is None:
518-
# Sample a random, slow rotation
519-
theta = 0.5 * np.pi * np.random.rand()
520-
521-
if n == 1:
522-
return np.random.rand() * np.eye(1)
523-
524-
rot = np.array([[np.cos(theta), -np.sin(theta)],
525-
[np.sin(theta), np.cos(theta)]])
526-
out = np.zeros((n, n))
527-
out[:2, :2] = rot
528-
q = np.linalg.qr(np.random.randn(n, n))[0]
529-
return q.dot(out).dot(q.T)
530-
531505
def DefaultLDS(D_obs, D_latent, D_input=0,
532506
mu_init=None, sigma_init=None,
533507
A=None, B=None, sigma_states=None,
@@ -561,10 +535,6 @@ def DefaultLDS(D_obs, D_latent, D_input=0,
561535

562536
return model
563537

564-
565-
from pybasicbayes.distributions import Gaussian
566-
from pylds.distributions import PoissonRegression
567-
568538
def DefaultPoissonLDS(D_obs, D_latent, D_input=0,
569539
mu_init=None, sigma_init=None,
570540
A=None, B=None, sigma_states=None,

0 commit comments

Comments
 (0)