Skip to content

Commit

Permalink
feat: update the changes made for merge_json_file
Browse files Browse the repository at this point in the history
  • Loading branch information
OmaymaMahjoub committed Jan 30, 2024
1 parent 6d69772 commit f7e8b0d
Showing 1 changed file with 14 additions and 6 deletions.
20 changes: 14 additions & 6 deletions marl_eval/json_tools/merge_json_files.py
Original file line number Diff line number Diff line change
Expand Up @@ -62,10 +62,16 @@ def _check_seed(concatenated_data: Dict, algo_data: Dict, seed_number: str) -> s
return seed_number


def concatenate_files(directory: str, json_path: str = "./concatenation") -> Dict:
def concatenate_files(
input_directory: str, output_json_path: str = "concatenated_json_files/"
) -> Dict:
"""Concatenate all json files in a directory and save the result in a json file."""
# Read all json files in a directory
json_data = _read_json_files(directory)
# Read all json files in a input_directory
json_data = _read_json_files(input_directory)

# Create target folder
if not os.path.exists(output_json_path):
os.makedirs(output_json_path)

# Using defaultdict for automatic handling of missing keys
concatenated_data: Dict = defaultdict(
Expand All @@ -88,11 +94,13 @@ def concatenate_files(directory: str, json_path: str = "./concatenation") -> Dic
] = algo_data

# Save concatenated data in a json file
with open(f"{json_path}.json", "w") as f:
if output_json_path[-1] != "/":
output_json_path += "/"
with open(f"{output_json_path}metrics.json", "w") as f:
json.dump(concatenated_data, f, indent=4)

print(
f"{Fore.CYAN}{Style.BRIGHT}Concatenated data saved in {json_path}.json\
successfully!{Style.RESET_ALL}"
f"{Fore.CYAN}{Style.BRIGHT}Concatenated data saved in "
+ f"{output_json_path}metrics.json successfully!{Style.RESET_ALL}"
)
return concatenated_data

0 comments on commit f7e8b0d

Please sign in to comment.