Skip to content

Commit

Permalink
Fix #16, #17
Browse files Browse the repository at this point in the history
And apply updated black formatting
  • Loading branch information
gtca committed Oct 12, 2023
1 parent 09062fb commit 031e2c6
Show file tree
Hide file tree
Showing 24 changed files with 35 additions and 109 deletions.
1 change: 0 additions & 1 deletion mofapy2/build_model/build_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,6 @@ def build_ThetaZ(self):
# initTheta_qE = 1.

self.init_model.initThetaZ(
self.data_opts["samples_groups"],
qa=initTheta_a,
qb=initTheta_b,
qE=initTheta_qE,
Expand Down
12 changes: 0 additions & 12 deletions mofapy2/build_model/init_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -98,7 +98,6 @@ def initZ(
# mean
if qmean is not None:
if isinstance(qmean, str):

# Random initialisation
if qmean == "random":
qmean = stats.norm.rvs(loc=0, scale=1, size=(self.N, self.K))
Expand Down Expand Up @@ -199,7 +198,6 @@ def initZ_smooth(
# mean
if qmean is not None:
if isinstance(qmean, str):

# Random initialisation
if qmean == "random":
qmean = stats.norm.rvs(loc=0, scale=1, size=(self.N, self.K))
Expand Down Expand Up @@ -350,7 +348,6 @@ def initZgU(
# mean
if qmean is not None:
if isinstance(qmean, str):

# Random initialisation
if qmean == "random":
qmean = stats.norm.rvs(loc=0, scale=1, size=(self.N, self.K))
Expand Down Expand Up @@ -415,7 +412,6 @@ def initSigma(
model_groups=False,
use_gpytorch=False,
):

self.Sigma = Sigma_Node(
dim=(self.K,),
sample_cov=sample_cov,
Expand All @@ -437,7 +433,6 @@ def initSigma_sparse(
model_groups=False,
use_gpytorch=False,
):

self.Sigma = Sigma_Node_sparse(
dim=(self.K,),
sample_cov=sample_cov,
Expand All @@ -464,7 +459,6 @@ def initSigma_warping(
model_groups=False,
use_gpytorch=False,
):

self.Sigma = Sigma_Node_warping(
dim=(self.K,),
sample_cov=sample_cov,
Expand Down Expand Up @@ -514,7 +508,6 @@ def initSZ(

## Initialise variational distribution (Q)
if isinstance(qmean_T1, str):

if qmean_T1 == "random":
qmean_T1 = stats.norm.rvs(loc=0, scale=1, size=(self.N, self.K))
elif qmean_T1 == "pca":
Expand Down Expand Up @@ -579,7 +572,6 @@ def initW(
W_list = [None] * self.M

for m in range(self.M):

## Initialise prior distribution (P) ##

# mean
Expand All @@ -593,7 +585,6 @@ def initW(
# mean
if qmean is not None:
if isinstance(qmean, str):

# Random initialisation
if qmean == "random":
qmean_m = stats.norm.rvs(
Expand Down Expand Up @@ -665,12 +656,10 @@ def initSW(
W_list = [None] * self.M

for m in range(self.M):

## Initialise prior distribution (P)

## Initialise variational distribution (Q)
if isinstance(qmean_S1, str):

if qmean_S1 == "random":
qmean_S1_tmp = stats.norm.rvs(
loc=0, scale=1.0, size=(self.D[m], self.K)
Expand Down Expand Up @@ -800,7 +789,6 @@ def initTau(self, pa=1e-3, pb=1e-3, qa=1.0, qb=1.0, qE=None):
tau_list = [None] * self.M

for m in range(self.M):

# Poisson noise model for count data
if self.lik[m] == "poisson":
tmp = 0.25 + 0.17 * np.nanmax(self.data[m], axis=0)
Expand Down
13 changes: 0 additions & 13 deletions mofapy2/build_model/save_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,6 @@ def __init__(
sort_factors=True,
compression_level=9,
):

# Check that the model is trained
# NOTE: it might be not trained if saving when training is interrupted
if not model.trained:
Expand Down Expand Up @@ -265,7 +264,6 @@ def saveData(self):
data_subgrp = data_grp.create_group(self.views_names[m])
intercept_subgrp = intercept_grp.create_group(self.views_names[m])
for g in range(len(self.groups_names)):

# Subset group
samples_idx = np.where(
np.array(self.samples_groups) == self.groups_names[g]
Expand Down Expand Up @@ -332,7 +330,6 @@ def saveImputedData(self, mean, variance):
for m in range(len(mean)):
view_subgrp = data_grp.create_group(self.views_names[m])
for g in range(len(self.groups_names)):

# Subset group
samples_idx = np.where(
np.array(self.samples_groups) == self.groups_names[g]
Expand Down Expand Up @@ -391,7 +388,6 @@ def saveZpredictions(self, mean, variance, values, groups):
)

def saveExpectations(self, nodes="all"):

# Get nodes from the model
nodes_dic = self.model.getNodes()
if type(nodes) is str:
Expand Down Expand Up @@ -435,15 +431,12 @@ def saveExpectations(self, nodes="all"):
# Multi-view nodes
if isinstance(nodes_dic[n], Multiview_Node):
for m in range(nodes_dic[n].M):

# Multi-groups nodes (Tau, Y, and Z)
if n in multigroup_nodes:

# Create subgroup for the view
view_subgrp = node_subgrp.create_group(self.views_names[m])

for g in self.groups_names:

# Add missing values to Tau and Y nodes
exp[m][self.mask[m]] = np.nan

Expand Down Expand Up @@ -471,7 +464,6 @@ def saveExpectations(self, nodes="all"):

# Single-view nodes
else:

# Multi-group nodes (Z)
if n in multigroup_nodes:
for g in self.groups_names:
Expand Down Expand Up @@ -548,13 +540,11 @@ def saveParameters(self, nodes="all"):
# Multi-view nodes
if isinstance(nodes_dic[n], Multiview_Node):
for m in range(nodes_dic[n].M):

# Create subgroup for the view
view_subgrp = node_subgrp.create_group(self.views_names[m])

# Multi-groups nodes
if n in multigroup_nodes:

for g in self.groups_names:
grp_subgrp = view_subgrp.create_group(g)

Expand Down Expand Up @@ -586,7 +576,6 @@ def saveParameters(self, nodes="all"):

# Single-view nodes
else:

# Multi-group nodes
if n in multigroup_nodes:
for g in self.groups_names:
Expand Down Expand Up @@ -615,7 +604,6 @@ def saveParameters(self, nodes="all"):
pass

def saveModelOptions(self):

# Subset model options
options_to_save = [
"likelihoods",
Expand Down Expand Up @@ -717,7 +705,6 @@ def saveSmoothOptions(self, smooth_opts):
grp[k].attrs["names"] = np.asarray(list(opts.keys())).astype("S")

def saveVarianceExplained(self):

# Sort values by alphabetical order of views
# order = np.argsort(self.views_names)
# # order = [ i[0] for i in sorted(enumerate(self.views_names), key=lambda x:x[1]) ]
Expand Down
1 change: 0 additions & 1 deletion mofapy2/build_model/train_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@


def train_model(model):

# Sanity check on the Bayesian Network
assert isinstance(model, BayesNet), "'model' has to be a BayesNet class"

Expand Down
3 changes: 0 additions & 3 deletions mofapy2/build_model/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -53,9 +53,7 @@ def gaussianise(Y_m, axis=0):


def process_data(data, likelihoods, data_opts, samples_groups):

for m in range(len(data)):

# For some wierd reason, when using reticulate from R, missing values are stored as -2147483648
data[m][data[m] == -2147483648] = np.nan

Expand All @@ -79,7 +77,6 @@ def process_data(data, likelihoods, data_opts, samples_groups):

# Centering and scaling is only appropriate for gaussian data
if likelihoods[m] in ["gaussian"]:

# Center features per group
if data_opts["center_groups"]:
for g in data_opts["groups_names"]:
Expand Down
3 changes: 1 addition & 2 deletions mofapy2/core/BayesNet.py
Original file line number Diff line number Diff line change
Expand Up @@ -133,7 +133,6 @@ def getNodes(self):
return self.nodes

def calculate_variance_explained(self, total=False):

# Collect relevant expectations
Z = self.nodes["Z"].getExpectation()
W = self.nodes["W"].getExpectation()
Expand Down Expand Up @@ -384,7 +383,7 @@ def iterate(self):
"time": iter_time,
"number_factors": number_factors,
"elbo": elbo["total"].values,
"elbo_terms": elbo.drop("total", 1),
"elbo_terms": elbo.drop("total", axis=1),
}
if "Sigma" in self.nodes.keys():
tmp = self.nodes["Sigma"].getParameters() # save only last iteration
Expand Down
1 change: 1 addition & 0 deletions mofapy2/core/distributions/basic_distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# from mofapy2.config import settings
# from mofapy2.core.utils import * # TODO prob not necessary ?


# General class for probability distributions
class Distribution(object):
"""General class for a statistical distribution"""
Expand Down
1 change: 0 additions & 1 deletion mofapy2/core/distributions/bernoulli_gaussian.py
Original file line number Diff line number Diff line change
Expand Up @@ -107,7 +107,6 @@ def updateExpectations(self):
# self.expectations = {'E':E, 'EB':EB, 'EN':EN, 'E2':E2, 'ENN':ENN, 'EXXT':EXXT }

def removeDimensions(self, axis, idx):

# Method to remove undesired dimensions
# - axis (int): axis from where to remove the elements
# - idx (numpy array): indices of the elements to remove
Expand Down
1 change: 1 addition & 0 deletions mofapy2/core/gpu_utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
# --------------------------------------------------------
gpu_mode = False


# --------------------------------------------------------
# operations on single matrices
# --------------------------------------------------------
Expand Down
2 changes: 0 additions & 2 deletions mofapy2/core/nodes/Kc_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ def compute_kernel(self, spectral_decomp=True):
self.compute_kernel_at_gridpoint(i, spectral_decomp=spectral_decomp)

def compute_kernel_at_gridpoint(self, i, spectral_decomp=True):

# build kernel matrix based on given covariance function
if not spectral_decomp:
self.Kmat[i, :, :] = SE(self.covariates, self.l_grid[i], zeta=0)
Expand Down Expand Up @@ -118,7 +117,6 @@ def set_gridix(self, lidx, k):
# no recomputation required as stored on grid

def eval_at_newpoints_k(self, new_cov, k):

Kc_new = SE(new_cov, self.l_grid[self.gridix[k]], zeta=0)

return Kc_new
1 change: 1 addition & 0 deletions mofapy2/core/nodes/Kg_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from mofapy2.core.nodes.variational_nodes import *
from mofapy2.core.gp_utils import *


# TODO: for large number of groups avoid constructing Kmat if spectral decomp and only save x at the end (getParameters in SigmaNode)
class Kg_Node(Node):
"""
Expand Down
5 changes: 0 additions & 5 deletions mofapy2/core/nodes/Sigma_node.py
Original file line number Diff line number Diff line change
Expand Up @@ -345,7 +345,6 @@ def removeFactors(self, idx, axis=1):
self.Sigma_inv_logdet = np.delete(self.Sigma_inv_logdet, axis=0, obj=idx)

def calc_neg_elbo_k(self, par, lidx, k, var):

self.zeta[k] = par[0]
self.Kc.set_gridix(lidx, k)

Expand Down Expand Up @@ -624,7 +623,6 @@ def optimise(self, var):

# optimise hyperparamters of GP
for k in range(K):

best_zeta = -1
best_elbo = -np.Inf

Expand Down Expand Up @@ -751,7 +749,6 @@ def __init__(
rankx=None,
model_groups=False,
):

super().__init__(
dim, sample_cov, groups, start_opt, opt_freq, n_grid, rankx, model_groups
)
Expand Down Expand Up @@ -812,7 +809,6 @@ def __init__(
model_groups=False,
idx_inducing=None,
):

super().__init__(
dim, sample_cov, groups, start_opt, opt_freq, n_grid, rankx, model_groups
)
Expand Down Expand Up @@ -946,7 +942,6 @@ def __init__(
warping_open_end=True,
warping_groups=None,
):

super().__init__(
dim, sample_cov, groups, start_opt, opt_freq, n_grid, rankx, model_groups
)
Expand Down
4 changes: 0 additions & 4 deletions mofapy2/core/nodes/Theta_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,6 @@ def _updateParameters(self):
self.Q.setParameters(a=Qa, b=Qb)

def calculateELBO(self):

# Collect parameters and expectations
Qpar, Qexp = self.Q.getParameters(), self.Q.getExpectations()
Pa, Pb, Qa, Qb = self.Ppar["a"], self.Ppar["b"], Qpar["a"], Qpar["b"]
Expand All @@ -91,7 +90,6 @@ class ThetaZ_Node(Beta_Unobserved_Variational_Node):
"""

def __init__(self, dim, pa, pb, qa, qb, groups, qE=None):

self.groups = groups
self.factors_axis = 1
self.N = len(self.groups)
Expand Down Expand Up @@ -159,7 +157,6 @@ def updateParameters(self, ix=None, ro=1.0):
self.Q.setParameters(a=Qa, b=Qb)

def _updateParameters(self, S, groups, ro):

Q = self.Q.getParameters()
Qa, Qb = Q["a"], Q["b"]
Qa *= 1 - ro
Expand All @@ -186,7 +183,6 @@ def _updateParameters(self, S, groups, ro):
return Qa, Qb

def calculateELBO(self):

# Collect parameters and expectations
Qpar, Qexp = self.Q.getParameters(), self.Q.getExpectations()
Pa, Pb, Qa, Qb = self.Ppar["a"], self.Ppar["b"], Qpar["a"], Qpar["b"]
Expand Down
2 changes: 1 addition & 1 deletion mofapy2/core/nodes/U_nodes.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
# TODO:
# - integrate into Z node using ix


# U_GP_Node_mv
class U_GP_Node_mv(MultivariateGaussian_Unobserved_Variational_Node):
"""
Expand Down Expand Up @@ -64,7 +65,6 @@ def get_mini_batch(self):
return self.mini_batch

def updateParameters(self, ix=None, ro=1.0):

# Get expectations from other nodes
W = self.markov_blanket["W"].getExpectations()
Y = self.markov_blanket["Y"].get_mini_batch()
Expand Down
Loading

0 comments on commit 031e2c6

Please sign in to comment.