utils.py 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475
  1. import pandas as pd
  2. from matplotlib.offsetbox import AnchoredOffsetbox, TextArea, HPacker, VPacker
  3. int_types = ['N', 'binsize', 'bin_num', 'seed_a', 'seed_b', 'seed']
  4. float_types = ['t_start', 't_stop', 'rate', 'bkgr_correlation',
  5. 'score', 'pvalue', 'epsilon', 'sigma_ex', 'sigma_in', 'simtime',
  6. 'f', 'mu']
  7. dtypes = dict.fromkeys(int_types, 'Int64') | dict.fromkeys(float_types, float)
  8. colormap = {'weights': "#870156",
  9. 'correlations': "#039857",
  10. 'ratio': "#D29402",
  11. 'rate_correlation': "#2c53af",
  12. 'rate_correlation_exc': "#2c53af",
  13. 'exc': "#2c53af",
  14. 'rate_correlation_inh': "#e8685b",
  15. 'inh': "#e8685b",
  16. }
  17. def load_df(path):
  18. df = pd.read_csv(path, dtype=dtypes)
  19. df.drop(df.columns[df.columns.str.contains('unnamed', case=False)],
  20. axis=1, inplace=True)
  21. return df
  22. def compact_column(df, compact_column='matrix', expand_column=['score', 'pvalue']):
  23. compact_values = df[compact_column].unique()
  24. if len(compact_values) <= 1:
  25. return df
  26. elif len(compact_values) > 2:
  27. raise ValueError('Can not compact on column with more than 2 values')
  28. df_left, df_right = [df[df[compact_column] == compact_value].copy()
  29. for compact_value in compact_values]
  30. df_left.drop(compact_column, axis=1, inplace=True)
  31. df_right.drop(compact_column, axis=1, inplace=True)
  32. out = expand_column if type(expand_column) == list else [expand_column]
  33. out += [compact_column]
  34. on = [col for col in df_left.columns.to_list() if col not in out]
  35. df_compact = df_left.merge(df_right, how='left', on=on,
  36. suffixes=(f'_{compact_values[0]}',
  37. f'_{compact_values[1]}'))
  38. return df_compact
  39. def stack_columns(df, prefixes=['pvalue'], suffixes=['weights', 'correlations'],
  40. new_column='matrix'):
  41. dfs = []
  42. for suffix in suffixes:
  43. data = df.copy()
  44. data[new_column] = suffix
  45. for prefix in prefixes:
  46. data[prefix] = data[f'{prefix}_{suffix}']
  47. for suf in suffixes:
  48. data.drop(f'{prefix}_{suf}', axis=1, inplace=True)
  49. dfs += [data]
  50. return pd.concat(dfs, axis=0, ignore_index=True)
  51. def multicolor_ylabel(ax, list_of_strings, list_of_colors, anchorpad=0,
  52. bbox_to_anchor=(-0.05, 0.5), **kw):
  53. ax.set_ylabel('')
  54. textprops=dict(ha='left', va='bottom', rotation=90, **kw)
  55. boxes = [TextArea(text, textprops=textprops | dict(color=color))
  56. for text, color in zip(list_of_strings[::-1], list_of_colors[::-1])]
  57. ybox = VPacker(children=boxes, align="center", pad=0, sep=5)
  58. anchored_ybox = AnchoredOffsetbox(loc='center', child=ybox, pad=anchorpad,
  59. frameon=False, bbox_to_anchor=bbox_to_anchor,
  60. bbox_transform=ax.transAxes, borderpad=0.)
  61. ax.add_artist(anchored_ybox)
  62. return None