Skip to content

Implement optimization of CPD parameters based on landmarks or signal correlation #42

@Buglakova

Description

@Buglakova

Here is the script I used with optuna:

import optuna
from platy_reg.match_pointclouds import run_matching
from scipy.stats import spearmanr
from pathlib import Path

def objective(trial):
    w = trial.suggest_categorical('w', [1e-3, 1e-4, 1e-5, 1e-6])
    beta = trial.suggest_categorical('beta', [10, 100, 10000, 10000])
    lmd = trial.suggest_categorical('lmd', [0.1, 0.001, 0.0001, 0.00001])
    maxiter = 100

    print(f"Experiment w_{w}_beta_{beta}_lmd_{lmd}")
    fixed_pcd_path = "data/em_registration/init_alignment/point_cloud_s2_init_alignment.pcd"
    moving_pcd_1_path = "data/pipeline_output/em_registration/St-Mhc--20250113Hyb--ALMF_Stellaris-40X-Sfg_Pl1/rigid_registration/point_cloud_rigid.pcd"
    
    moving_pcd_2_path = "data/pipeline_output/em_registration/St-Mhc--20250113Hyb--ALMF_Stellaris-40X-Sfg_Pl4/rigid_registration/point_cloud_rigid.pcd"
    
    experiment_dir = Path("data/optuna_benchmark_point_matching") / f"w_{w}_beta_{beta}_lmd_{lmd}"
    experiment_dir.mkdir(exist_ok=True)
    log_dir_1 = experiment_dir / "platy1"
    log_dir_1.mkdir(exist_ok=True)
    log_dir_2 = experiment_dir / "platy4"
    log_dir_2.mkdir(exist_ok=True)
    
    mapped_features_1 = run_matching(fixed_pcd_path, moving_pcd_1_path, log_dir_1, w, beta, lmd, maxiter)
    mapped_features_2 = run_matching(fixed_pcd_path, moving_pcd_2_path, log_dir_2, w, beta, lmd, maxiter)

    sum_corr = spearmanr(mapped_features_1["gene1_sum"], mapped_features_2["gene1_sum"])
    mean_corr = spearmanr(mapped_features_1["gene1_mean"], mapped_features_2["gene1_mean"])
    spots_corr = spearmanr(mapped_features_1["gene1_spots"], mapped_features_2["gene1_spots"])
    return sum_corr.statistic, mean_corr.statistic, spots_corr.statistic

search_space = {
    'w': [1e-3, 1e-4, 1e-5, 1e-6],
    'beta': [10, 100, 10000, 10000],
    'lmd': [0.1, 0.001, 0.0001, 0.00001]}

study_name = "cpd_parameters"  # Unique identifier of the study.
storage_name = "sqlite:///{}.db".format(study_name)
    
    
study = optuna.create_study(study_name=study_name, storage=storage_name, sampler=optuna.samplers.GridSampler(search_space), load_if_exists=True, directions=["maximize", "maximize", "maximize"])
study.optimize(objective, n_trials=4 ** 3)

Then I think using the study class it's possible to choose the best or open the dashboard and inspect results . It's really nice compared to implementing logging experiment results ourselfs.

Metadata

Metadata

Assignees

Labels

No labels
No labels

Type

No type
No fields configured for issues without a type.

Projects

No projects

Milestone

No milestone

Relationships

None yet

Development

No branches or pull requests

Issue actions