utils.py 3.0 KB

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