# %%
import os, sys
import glob
import argparse
import pickle

import numpy as np
import pandas as pd


def concat_and_out(in_dir, out_dir):
    data_parent_dir = in_dir
    out_dir = out_dir

    data_to_find_path=os.path.join(
        data_parent_dir, '**', '*.pickle')

    res_pickle_list = glob.glob(
        data_to_find_path,
        recursive=True)

    all_res_list = []
    for _i_path_pickle in res_pickle_list:
      with open(_i_path_pickle, 'rb') as f:
        res = pickle.load(f)
        all_res_list.append(res)

    res_all_df = pd.concat(all_res_list, axis=0)

    col_sufix='mean'
    mean_res_all=res_all_df.groupby(axis=0, level=0).mean()
    mean_res_all.rename(
      columns=lambda col: '_'.join([col, col_sufix]),
      inplace=True)

    col_sufix='std'
    std_res_all=res_all_df.groupby(axis=0, level=0).std()
    std_res_all.rename(
      columns=lambda col: '_'.join([col, col_sufix]),
      inplace=True)

    col_sufix='count'
    count_res_all=res_all_df.groupby(axis=0, level=0).count()
    count_res_all.rename(
      columns=lambda col: '_'.join([col, col_sufix]),
      inplace=True)

    res_all=pd.concat([mean_res_all, std_res_all, count_res_all], axis=1)
    res_all.sort_index(axis=1, inplace=True)

    out_filename_base = os.path.basename(os.path.normpath(data_parent_dir))
    res_all.to_csv(
        os.path.join(out_dir, out_filename_base + '.csv'))

# %%