Skip to content

[ENH] Optimize example_models loading using pickle. #2456

@daehyun99

Description

@daehyun99

Is your feature request related to a problem? Please describe.

Save example models stored in BIF format as Python objects using the pickle library (or joblib).

This reduces the loading time for pgmpy's example models.
It also reduces the CI test execution time in GitHub Actions.

Thanks.

  • Example script for measuring model load time
import pickle
import os
from pgmpy.utils import get_example_model
import time

start_time = time.time()
origin_model = get_example_model("diabetes")
end_time = time.time()

print("Model Loading Time(BIF)", end_time - start_time)

# Save as Pickle
file_pkl = f"{"model_name"}_test.pkl"
with open(file_pkl, 'wb') as f:
    pickle.dump(origin_model, f)

start_time = time.time()
with open(file_pkl, 'rb') as f:
    pkl_model = pickle.load(f)
end_time = time.time()

print("Model Loading Time(Python Object)", end_time - start_time)
print("Model Equal Check: ", origin_model.__eq__(pkl_model))
  • Output
Model Loading Time(BIF) 55.59602880477905
Model Loading Time(Python Object) 0.01642322540283203
Model Equal Check:  True

Describe the solution you'd like

  • Step:
  1. Create TestPickleReader and TestPickleWriter classes in the pgmpy/test_readwrite/ path.
  2. Create PickleReader and PickleWriter classes in the pgmpy/readwrite/ path.
  3. Save the example model as a Python object (*.pkl) using pickle.
  4. Modify the filenames path in the get_example_model function to the file path created in step 3.
  5. Add code to the get_example_model function to load the Python object using PickleReader.
  6. Check if the execution time of the get_example_model function has been reduced.

Describe alternatives you've considered

  • pickle
  • joblib

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions