# Pre-computes and stores certain dataset properties

from datasets import *
from util import *

def safe_json_file_update(path, data):
	temp_path = path + ".tmp"
	with open(temp_path, "w") as f:
		json.dump(data, f)
	os.rename(temp_path, path)

def preprocess(dataset):
	
	# Data properties cache file
	data_properties_file = DATA_PATH + "data_properties.json"
	if os.path.isfile(data_properties_file):
		with open(data_properties_file, "r") as f:
			data_properties = json.load(f)
	else:
		data_properties = {}
	if dataset.name not in data_properties.keys():
		data_properties[dataset.name] = {}
	
	# Make sure data_range is in cache in the case of only_check_for_missing_results
	original = load_data(dataset.path, dataset.dtype)
	if "data_range" not in data_properties[dataset.name].keys():
		data_properties[dataset.name]["data_range"] = float(compute_data_range(original))
	if "data_norm" not in data_properties[dataset.name].keys():
		data_properties[dataset.name]["data_norm"] = float(np.linalg.norm(original))
	safe_json_file_update(data_properties_file, data_properties)

preprocess(engine)
