-
Notifications
You must be signed in to change notification settings - Fork 1
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
WIP: Refactor inference modules #143
base: main
Are you sure you want to change the base?
Conversation
…e the parameter sampling
I believe this is a great idea, this PR should greatly improve readability, maintainability & separation of concerns. Just to make sure I got this right: In the end, dense grids, sparse grids, and MCMC samplers would be implemented in the sampling submodule, right? Edit: Nevermind, I just saw you did two different submodules for that, which makes sense ofc! |
def combined_evaluation(param, model, data_transformation, kde, slice): | ||
param, sim_res, logdensity = evaluate_log_density( | ||
param, model, data_transformation, kde, slice | ||
) | ||
combined_result = np.concatenate([param, sim_res, np.array([logdensity])]) | ||
return logdensity, combined_result | ||
|
||
|
||
def get_LogDensityEvaluator( | ||
model: BaseModel, | ||
data_transformation: DataTransformation, | ||
kde: KDE, | ||
slice: np.ndarray, | ||
): | ||
logdensity_blob_function = partial( | ||
combined_evaluation, | ||
model=model, | ||
data_transformation=data_transformation, | ||
kde=kde, | ||
slice=slice, | ||
) | ||
|
||
param_dim = slice.shape[0] | ||
output_dim = (1, param_dim + model.data_dim + 1) | ||
return FunctionWithDimensions( | ||
logdensity_blob_function, param_dim, output_dim |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussion Result: Refactor as a class with name LogDensityEvaluator(FunctionWithDimensions)
def combined_evaluation(param, model, data_transformation, kde, slice): | ||
param, sim_res, density = evaluate_density( | ||
param, model, data_transformation, kde, slice | ||
) | ||
combined_result = np.concatenate([param, sim_res, np.array([density])]) | ||
return combined_result | ||
|
||
|
||
def get_DensityEvaluator( | ||
model: BaseModel, | ||
data_transformation: DataTransformation, | ||
kde: KDE, | ||
slice: np.ndarray, | ||
): | ||
combined_evaluation_function = partial( | ||
combined_evaluation, | ||
model=model, | ||
data_transformation=data_transformation, | ||
kde=kde, | ||
slice=slice, | ||
) | ||
|
||
param_dim = slice.shape[0] | ||
output_dim = param_dim + model.data_dim + 1 | ||
return FunctionWithDimensions( | ||
combined_evaluation_function, param_dim, output_dim | ||
) |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussion Result: Refactor as a class with name DensityEvaluator(FunctionWithDimensions)
eulerpi/inference.py
Outdated
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussion Result: Offer functional interface inference
, but use object oriented approach internally
def sampling_inference( | ||
model: BaseModel, | ||
data_transformation: DataTransformation, | ||
kde: KDE, | ||
slice: np.ndarray, | ||
result_manager: ResultManager, | ||
num_processes: int, | ||
sampler: Sampler = None, | ||
num_walkers: int = 10, | ||
num_steps: int = 2500, | ||
num_burn_in_samples: Optional[int] = None, | ||
thinning_factor: Optional[int] = None, | ||
get_walker_acceptance: bool = False, |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Discussion Result: Reintroduce subruns
…r, and PathManager
Fix a few minor bugs
Fix a few small issues/bugs
…tems-Biology/EPI into refactor/inference
Restore sub run functionality
Update imports
Move sampler subpackage to sampling_inference
Description
This pull request addresses architectural limitations highlighted in issues #134 and #42. While the current design is effective for a stable prototype and generating results, it lacks flexibility for future extensions. Building on the refactoring efforts in #141 and #142, this PR restructures the framework to allow more flexible and modular usage.
Key Changes
evaluation
: Handles kernel density estimation and the mapping of densities between data and parameter spaces.inferences
: Manages different approaches to selecting points for density evaluation:sampling
: Dynamically generates points based on the density of previous points.grids
: Uses a predefined set of points, enabling straightforward operations like chunking for multiprocessing.samplers
: Encapsulates dependencies on specific samplers for sampling-based inference.dense_grid
no longer depends on theInferenceTypes
orcalc_kernel_width
. Instead it relies on the abstractKDE
class, which serves as a callable interface.evaluate_density
function no longer requires direct access todata
,kernel_width
, or the hardcodedeval_gauss_kde
function. Instead, it operates via theKDE
interface.InferenceType
andDenseGridType
are no longer used globally but only locally, in theinference
anddense_grid
respectively.This pull request fixes #134 and allows to implement #42 quickly.
The dependency graph of eulerpi before the refactoring at commit 627ecab. Many of the dependencies are not visible, because this graph only contains modules, not classes.
Current dependency graph, the larger number of nodes is caused by splitting modules with multiple classes into separate files.
And a overview over the external dependencies
Question
num_runs
in the sampler The reason for thesub_runs
was to have some fault tolerance. I am not sure if our package should take care of that or whether the details should be handled by the sampler. The user can still loop over the sampling by usingcontinue_sampling
.Help wanted
TODOs:
inference
call.extensions/doctest.html
Type of change
How Has This Been Tested?
Please note/describe at least one tests that you ran to verify your changes.
Checklist For Contributor
[unreleased]
sectionChecklist For Maintainers