Source code for vampire.util
import os
import pickle
import pandas as pd
[docs]def read_pickle(path):
"""
Loads content in the pickle file from `path`.
Parameters
----------
path : str
Path of pickle file to be loaded.
Returns
-------
content
Content of the pickle file.
"""
path = os.path.normpath(path)
opened_file = open(path, 'rb')
content = pickle.load(opened_file)
opened_file.close()
return content
[docs]def write_pickle(path, variable):
"""
Writes `variable` to `path` as a pickle file.
Parameters
----------
path : str
Path of pickle file to be saved.
variable
A variable to be saved in pickle file.
"""
path = os.path.normpath(path)
opened_file = open(path, 'wb')
pickle.dump(variable, opened_file)
opened_file.close()
[docs]def generate_file_paths(filepath, filename, filter_info, extension):
"""
Generates file paths according to regex filters.
Parameters
----------
filepath : str
Path to the file.
filename : str
Common filename of the file.
filter_info : ndarray
Regex filter(s) of image filenames to be analyzed.
Empty if no filter needed.
extension : str
Extension of the file, including the dot `.`.
Returns
-------
file_path : str
Path to named file.
"""
filter_info = pd.Series(filter_info)
prohibited_char_regex = r'(\\)|(\/)|(\,)|(\:)|(\*)|(\")|(\<)|(\>)|(\|)'
replacement = '-'
filter_info = filter_info.str.replace(prohibited_char_regex, replacement, regex=True)
filter_tag = '_'.join(filter_info)
file_path = os.path.join(filepath, f'{filename}__{filter_tag}{extension}')
return file_path
[docs]def get_properties_pickle_path(filepath, filter_info):
return generate_file_paths(filepath, 'raw-properties', filter_info, '.pickle')
[docs]def get_properties_csv_path(filepath, filter_info):
return generate_file_paths(filepath, 'raw-properties', filter_info, '.csv')
[docs]def get_model_pickle_path(filepath, filter_info, model):
model_name = model.model_name
n_points = model.n_points
n_clusters = model.n_clusters
n_pcs = model.n_pcs
return generate_file_paths(
filepath,
f'model_{model_name}_({n_points}_{n_clusters}_{n_pcs})',
filter_info,
'.pickle'
)
[docs]def get_apply_properties_csv_path(filepath, filter_info, model, img_set_name):
model_name = model.model_name
n_points = model.n_points
n_clusters = model.n_clusters
n_pcs = model.n_pcs
return generate_file_paths(
filepath,
f'apply-properties_{model_name}_on_{img_set_name}_({n_points}_{n_clusters}_{n_pcs})',
filter_info,
'.csv'
)
[docs]def get_apply_properties_pickle_path(filepath, filter_info, model, img_set_name):
model_name = model.model_name
n_points = model.n_points
n_clusters = model.n_clusters
n_pcs = model.n_pcs
return generate_file_paths(
filepath,
f'apply-properties_{model_name}_on_{img_set_name}_({n_points}_{n_clusters}_{n_pcs})',
filter_info,
'.pickle'
)