Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -59,36 +59,35 @@ def generate_unique_combinations(built_permutation, remaining_columns, full_perm
# remove the most recent column and put it back on the remaining column list where we found it (sorted)
remaining_columns.insert(c, built_permutation.pop(-1))

import pickle
import os
from os import environ, path
master_unique_permutation_list = {}
unique_permutation_list = {}
def generate_all_unique_combinations(C, M, must_use_all_groups = False):

cache_dir_path = ASP_CACHE_DIR_DEFAULT
# The user is allowed to set the cache directory via an environment variable.
if environ.get(ASP_CACHE_DIR_ENV_VAR) is not None:
cache_dir_path = environ.get(ASP_CACHE_DIR_ENV_VAR)
Comment on lines 67 to 70
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Overall looks good to me. I would also recommend to use getenv instead of environ.get to reduce lines.

Suggested change
cache_dir_path = ASP_CACHE_DIR_DEFAULT
# The user is allowed to set the cache directory via an environment variable.
if environ.get(ASP_CACHE_DIR_ENV_VAR) is not None:
cache_dir_path = environ.get(ASP_CACHE_DIR_ENV_VAR)
cache_dir_path = os.getenv(ASP_CACHE_DIR_ENV_VAR, ASP_CACHE_DIR_DEFAULT)

cache_file_path = path.join(cache_dir_path, "master_list.pkl")
cache_file_path = path.join(cache_dir_path, f"permutations_{C}_{M}.npy")

global master_unique_permutation_list
if len(master_unique_permutation_list) == 0 and path.exists(cache_file_path):
with open(cache_file_path,"rb") as cache:
master_unique_permutation_list = pickle.load(cache)
global unique_permutation_list
if (C,M) not in unique_permutation_list:
if path.exists(cache_file_path):
unique_permutation_list[(C,M)] = np.load(cache_file_path, allow_pickle=False)

if (C,M) not in master_unique_permutation_list:
full_permutation_list = []
generate_unique_combinations([0], [c for c in range(1,C)], full_permutation_list, M)
master_unique_permutation_list[(C,M)] = full_permutation_list
if not path.exists(cache_dir_path):
os.makedirs(cache_dir_path)
with open(cache_file_path, "wb") as cache:
pickle.dump(master_unique_permutation_list, cache)
else:
full_permutation_list = []
generate_unique_combinations([0], [c for c in range(1,C)], full_permutation_list, M)
unique_permutation_list[(C,M)] = full_permutation_list
if not path.exists(cache_dir_path):
os.makedirs(cache_dir_path)
np.save(cache_file_path, full_permutation_list, allow_pickle=False)

unique_permutations = master_unique_permutation_list[(C,M)]
unique_permutations = unique_permutation_list[(C,M)]

return unique_permutations


# analytical solution
import math
def predict_unique_combinations(C, M):
Expand Down