macros_evolution_space / create_new_usuarios.py
Maximofn's picture
feat(SRC): :rocket: Add all logic
478fd8c
from pathlib import Path
import json
from datetime import datetime
from tqdm import tqdm
# Paths
usuarios_path = 'usuarios'
usuarios_macros_difference_path = 'usuarios_macros_difference'
# Get sorted date keys
def get_sorted_date_keys(data):
keys = list(data.keys())
keys_dates = []
for key in keys:
try:
keys_dates.append(datetime.strptime(key, '%Y-%m-%d'))
except ValueError:
pass
keys_sorted = sorted(keys_dates)
keys_sorted = [key.strftime('%Y-%m-%d') for key in keys_sorted]
keys_sorted = [key.replace('-01', '-1') for key in keys_sorted]
keys_sorted = [key.replace('-02', '-2') for key in keys_sorted]
keys_sorted = [key.replace('-03', '-3') for key in keys_sorted]
keys_sorted = [key.replace('-04', '-4') for key in keys_sorted]
keys_sorted = [key.replace('-05', '-5') for key in keys_sorted]
keys_sorted = [key.replace('-06', '-6') for key in keys_sorted]
keys_sorted = [key.replace('-07', '-7') for key in keys_sorted]
keys_sorted = [key.replace('-08', '-8') for key in keys_sorted]
keys_sorted = [key.replace('-09', '-9') for key in keys_sorted]
return keys_sorted
# Get non date keys
def get_non_date_keys(data):
keys = list(data.keys())
keys_non_dates = []
for key in keys:
try:
datetime.strptime(key, '%Y-%m-%d')
except ValueError:
keys_non_dates.append(key)
return keys_non_dates
# Get macros from string data
def get_macros_from_string(data):
macros = data.split(' ')
if len(macros) != 8:
return None
for i, macro in enumerate(macros):
if '.' in macro:
macro = macro.split('.')[0]
macros[i] = macro
if macro == '':
return None
macros = [int(macro) for macro in macros]
is_all_numbers = all((type(macro)==int or type(macro)==float) for macro in macros)
if not is_all_numbers:
return None
return macros
# Add empty weight difference to all the dates
def add_empty_macros_difference(data):
new_data = {}
new_data['macros'] = data
new_data['diferencia_macros'] = '0 0 0 0 0 0 0 0'
return new_data
# Add real macros difference to all the dates
def add_real_macros_difference(data, keys_dates):
# Get the number of keys dates
number_of_keys_dates = len(keys_dates)
# Iterate over the keys dates
for i in range(1,number_of_keys_dates):
# Get the previous macros
previous_macros = data[keys_dates[i-1]]['macros']
previous_macros = get_macros_from_string(previous_macros)
if previous_macros is None:
return data
# Get the current macros
current_macros = data[keys_dates[i]]['macros']
current_macros = get_macros_from_string(current_macros)
if current_macros is None:
return data
# Calculate the difference
diferencia_macros = [current_macros[i] - previous_macros[i] for i in range(8)]
diferencia_macros = ' '.join(str(diferencia) for diferencia in diferencia_macros)
# Add the difference to the data
data[keys_dates[i]]['diferencia_macros'] = diferencia_macros
return data
if __name__ == '__main__':
# Get all the files in the formularios_path
files = Path(usuarios_path).glob('*.json')
# Sort the files by name
files = sorted(files, key=lambda x: x.name)
# Load all the files
for file in tqdm(files):
# Get data from the file
with open(file, 'r') as f:
data = json.load(f)
# Create new empty dictionary
data_sorted = {}
# Get the date and non date keys
keys_dates = get_sorted_date_keys(data)
keys_non_dates = get_non_date_keys(data)
# Add empty weight difference to all the dates
for key_date in keys_dates:
data[key_date] = add_empty_macros_difference(data[key_date])
# Add real weight difference to all the dates
data = add_real_macros_difference(data, keys_dates)
# Concatenate the non date keys and the date keys
sorted_keys = keys_non_dates + keys_dates
# Add the sorted keys to the new dictionary
for key in sorted_keys:
data_sorted[key] = data[key]
# Save the data to the file
file_path = Path(usuarios_macros_difference_path) / file.name
with open(file_path, 'w') as f:
json.dump(data_sorted, f, indent=4)