Skip to content

Commit

Permalink
refactor of t1dmodel + sensor lifetime
Browse files Browse the repository at this point in the history
  • Loading branch information
gcappon committed Oct 12, 2024
1 parent b6029f5 commit 5f6799b
Show file tree
Hide file tree
Showing 13 changed files with 2,568 additions and 1,460 deletions.
27 changes: 8 additions & 19 deletions py_replay_bg/identification/map.py
Original file line number Diff line number Diff line change
@@ -1,31 +1,19 @@
import os

import matplotlib.pyplot as plt
from matplotlib import pylab


import numpy as np

from multiprocessing import Pool

import warnings
import pickle

import copy
import numpy as np
import pandas as pd

from py_replay_bg.data import ReplayBGData

from datetime import datetime, timedelta

from multiprocessing import Pool
from tqdm import tqdm

from scipy.optimize import minimize

import warnings
from py_replay_bg.data import ReplayBGData

# Suppress all RuntimeWarnings
warnings.filterwarnings("ignore", category=RuntimeWarning)


class MAP:
"""
A class that orchestrates the identification process via MAP.
Expand All @@ -40,7 +28,7 @@ def __init__(self, model,
----------
model: Model
An object that represents the physiological model hyperparameters to be used by ReplayBG.
max_iter: int, optional, default : 1000
max_iter: int, optional, default : 100000
Maximum number of iterations.
Returns
Expand Down Expand Up @@ -122,7 +110,7 @@ def identify(self, data, rbg_data, rbg):
options['disp'] = False

# Select the function to minimize
neg_log_posterior_func = self.model.neg_log_posterior_single_meal if self.model.is_single_meal else self.model.neg_log_posterior_multi_meal
neg_log_posterior_func = self.model.neg_log_posterior

# Initialize results
results = []
Expand Down Expand Up @@ -175,6 +163,7 @@ def identify(self, data, rbg_data, rbg):

return draws


def run_map(start, func, rbg_data, options):
result = minimize(func, start, method='Powell', args=(rbg_data,), options=options)
ret = dict()
Expand Down
4 changes: 2 additions & 2 deletions py_replay_bg/identification/mcmc.py
Original file line number Diff line number Diff line change
Expand Up @@ -145,7 +145,7 @@ def identify(self, data, rbg_data, rbg):
if rbg.environment.parallelize:
pool = Pool(processes=rbg.environment.n_processes)

log_posterior_func = self.model.log_posterior_single_meal if self.model.is_single_meal else self.model.log_posterior_multi_meal
log_posterior_func = self.model.log_posterior
sampler = emcee.EnsembleSampler(self.n_walkers, self.n_dim, log_posterior_func, moves=[(emcee.moves.DEMove(sigma=1.0e-3), 0.2), (emcee.moves.DESnookerMove(gammas=0.1), 0.8)], pool=pool, args=[rbg_data])

# Run the burn-in chain
Expand Down Expand Up @@ -189,7 +189,7 @@ def identify(self, data, rbg_data, rbg):
for i in range(0, len(tbs)):

#...check if it is ok. If so...
if self.model.check_copula_extraction(samples[i]):
if self.model.check_extraction(samples[i]):

# ...flag it and fill the final vector
to_be_sampled[tbs[i]] = False
Expand Down
Loading

0 comments on commit 5f6799b

Please sign in to comment.