-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathAnalysisTools.py
More file actions
executable file
·75 lines (60 loc) · 2.24 KB
/
Copy pathAnalysisTools.py
File metadata and controls
executable file
·75 lines (60 loc) · 2.24 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
'''
An aggregate script for independent anaysis functions
'''
import pandas
import numpy as np
import multiprocessing as mp
from colorsys import hls_to_rgb
from functools import reduce
from copy import deepcopy
def clustersToMatrix(clusters, sample_list):
'''
[[[string]]] -> [DataFrame]
takes a bunch of cluster results and make them into similarity matrices
'''
#one base matrix to copy
base_matrix = pandas.DataFrame(np.zeros((len(sample_list), len(sample_list))), index = sample_list, columns = sample_list)
pool = mp.Pool(None, clusterWorkerInit, (base_matrix,))
res = pool.map(clusterWorker, clusters)
return res
def clusterWorkerInit(_base):
global base
base = _base
#decompose one cluster
def clusterWorker(cluster):
'''
does it for one cluster
inherit base from init
'''
scaffold = deepcopy(base)
for row in cluster:
scaffold.loc[row, row] = 1
return scaffold
def overallMatrix(matrices):
return reduce(lambda x, y: x + y, matrices)
def createColorTable(groups, overall_clusters, sample_list):
'''
[string], [[string]], [string] > df
though really sample list is just a flat overall_clusters
might as well keep it consistent.
takes the groups and the corresponding clusters and returns
a df that is samples x 1, filled with what color each sample should be
'''
#figure out the hue and luminosity values
n_groups = len(groups)
n_hue_pref = 9
hue_offset = 20
lum_sets = [[0.5], [0.75, 0.25], [0.75, 0.50, 0.25], [0.8, 0.6, 0.4, 0.2], [0.85, 0.7, 0.55, 0.4, 0.25, 0.1]]
n_lums = int(np.ceil(n_groups / n_hue_pref))
lums = lum_sets[n_lums - 1]
n_hues = int(np.ceil(n_groups / n_lums))
hue_max = 360-hue_offset * 2
hue_gap = hue_max // n_hues
hues = [(i * hue_gap + hue_offset)/360 for i in range(n_hues)]
rgb_vals = [hls_to_rgb(hue, lum, 1) for lum in lums for hue in hues]
#construct a table
df = pandas.DataFrame('', columns = ['color'], index = groups)
for group, rgb in zip(groups, rgb_vals[:len(groups)]):
df['color'].loc[group] = rgb
df.loc['None', 'color'] = (0,0,0)
return df