[Python] Keep getting pickling error when using neurolib (model-fitting)

Discussão em 'Python' iniciado por Stack, Abril 10, 2025.

  1. Stack

    Stack Membro Participativo

    Main error:

    PicklingError:
    Can't pickle <class 'neurolib.utils.parameterSpace.ParameterSpace'>:
    it's not the same object as neurolib.utils.parameterSpace.ParameterSpace


    Here's my code:

    # wcisp_gridsearch_final.py

    # --- Core Imports ---
    import sys
    import os
    import dill

    os.environ["NO_PROXY"] = "localhost,127.0.0.1" # Prevent network-related hangs
    os.environ["OBJC_DISABLE_INITIALIZE_FORK_SAFETY"] = "YES" # macOS specific
    import numpy as np
    from multiprocessing import set_start_method
    import gc
    import importlib


    # --- Configure multiprocessing first ---
    set_start_method("spawn", force=True)

    # --- Add local module path ---
    sys.path.append('/Users/pbj/Documents/Github/srg-wb-tms-modeling/wbm')

    # --- Import your custom model ---
    from wcisp import WCISP

    # --- Neurolib Setup ---
    from importlib import reload
    from neurolib.optimize.exploration.exploration import BoxSearch
    import neurolib.utils.parameterSpace as ps # Import the MODULE, not just the class
    from neurolib.utils.parameterSpace import ParameterSpace
    importlib.reload(ps)
    ParameterSpace = ps.ParameterSpace


    # --- Environment Configuration ---
    os.environ["OMP_NUM_THREADS"] = "1" # Disable OpenMP threading
    os.environ["HDF5_USE_FILE_LOCKING"] = "FALSE" # Prevent HDF5 locking issues

    # --- Debug Information ---
    print("\n=== Environment Verification ===")
    print(f"Python executable: {sys.executable}")
    print(f"NumPy version: {np.__version__}")
    import pickle
    pickle.Pickler = dill.Pickler
    pickle.Unpickler = dill.Unpickler

    try:
    import tables
    print(f"PyTables version: {tables.__version__}")
    print(f"PyTables linked to HDF5: {tables.get_hdf5_version()}")
    except ImportError as e:
    print(f"PyTables import failed: {e}")
    raise

    # --- Main Execution Block ---
    if __name__ == "__main__":
    # --- Load connectivity data ---
    Cmat = np.loadtxt('Abeysuriya_cmat.csv', delimiter=',')
    Dmat = np.loadtxt('Abeysuriya_dmat.csv', delimiter=',') * 100 # Convert to cm

    # --- Test model ---
    test_model = WCISP(
    Cmat=Cmat,
    Dmat=Dmat * 0.1, # Convert cm to mm
    duration=30,
    exc_ext_baseline=0.31,
    K_gl=0.22,
    c_ie_0=-3.8
    )
    test_model.run()

    # --- Parameter Space ---
    params = ParameterSpace({
    "K_gl": np.linspace(0.1, 1.0, 5),
    "c_ee": np.linspace(2.5, 4.5, 3),
    "c_ei": np.linspace(2.5, 4.5, 3),
    "rho": [0.1, 0.15, 0.2],
    "exc_ext_baseline": np.linspace(0.1, 0.5, 5),
    "c_ie_0": np.linspace(-4.5, -1.5, 5),
    }, kind="grid")

    gc.disable()

    # --- Grid Search Setup ---
    search = BoxSearch(
    model=WCISP(Cmat=Cmat, Dmat=Dmat * 0.1, duration=30),
    parameterSpace=params,
    filename="wcisp-gridsearch.hdf"
    )

    # --- Safe Execution ---
    search.run(
    multiprocessing=False, # Disable until environment is stable
    chunkwise=True,
    bold=True,
    backend="sequential"
    # Reduce verbosity
    )

    print("\nGrid search completed successfully!")


    I've tried a few things: Cretaing new environments, and installing compatible versions of packages as well environment hardening

    export PYTHONHASHSEED=0
    export OBJC_DISABLE_INITIALIZE_FORK_SAFETY=YES


    Not sure what is wrong, would love some help! Have been stuck on this for quite some time now.

    Continue reading...

Compartilhe esta Página