-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_map.py
81 lines (60 loc) · 2.6 KB
/
create_map.py
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
76
77
78
79
80
81
""" Create RGB color maps from bins """
import os
import sys
import collections
import linecache
import argparse
import colorsys
import math
import pandas as pd
import numpy as np
from sklearn import preprocessing
from io import StringIO
from colour import Color
PARSER = argparse.ArgumentParser(
description='Create RGB color maps from bins.')
PARSER.add_argument('means', metavar='MEANS', type=str,
help='the means file')
PARSER.add_argument('stds', metavar='STDS', type=str,
help='the stds file')
PARSER.add_argument('output', metavar='OUTPUT', type=str,
help='the output (map) file')
PARSER.add_argument('-d', '--delimiter', type=str, default=',',
help='use DELIMITER instead of \',\' for field delimiter')
PARSER.add_argument('-i', '--index', type=int, required=True,
help='the index for the column to be exported as an RGB color map')
PARSER.add_argument('-q', '--quantile', type=float, default=0.25,
help='use QUANTILE instead of \'0.25\' to determine the min and max')
ARGS = PARSER.parse_args()
ARGS.output = os.path.splitext(ARGS.output)[0]
MEANS = pd.read_csv(ARGS.means, sep=ARGS.delimiter, usecols=[ARGS.index - 1], header=None)
STDS = pd.read_csv(ARGS.stds, sep=ARGS.delimiter, usecols=[ARGS.index - 1], header=None)
STDS = STDS.fillna(0)
STDS = STDS.rename(columns = {0:1})
Q1 = MEANS.quantile(ARGS.quantile).values[0]
Q3 = MEANS.quantile(1.0 - ARGS.quantile).values[0]
IQR = Q3 - Q1
MIN = Q1 - 1.5 * IQR
MAX = Q3 + 1.5 * IQR
MEANS[MEANS < MIN] = MIN
MEANS[MEANS > MAX] = MAX
Q1_S = STDS.quantile(ARGS.quantile).values[0]
Q3_S = STDS.quantile(1.0 - ARGS.quantile).values[0]
IQR_S = Q3_S - Q1_S
MIN_S = Q1_S - 1.5 * IQR_S
MAX_S = Q3_S + 1.5 * IQR_S
if MAX_S == 0: MAX_S = 0.001
STDS[STDS < MIN_S] = MIN_S
STDS[STDS > MAX_S] = MAX_S
MEANS = MEANS.apply(lambda x: (0.85 * (x - np.min(x)) / (np.max(x) - np.min(x))) + 0.66).values
STDS = STDS.apply(lambda x: 1 - (0.5 * (x - np.min(x))) / (np.max(x) - np.min(x))).values
#DATA = pd.concat([DATA, STDS], axis=1)
with open(ARGS.output + '.map', 'a+') as f:
for i, row in enumerate(MEANS):
if math.isnan(STDS[i][0]) or STDS[i][0] == 0 or math.isinf(STDS[i][0]):
STDS[i][0] = 0.99
if row[0] > 1.0: row[0] = row[0] - 1.0
row[0] = (1 - row[0]) + 0.33
if row[0] > 1.0: row[0] = row[0] - 1.0
c = Color(hue=min(max(row[0], 0.0), 1.0), saturation=min(max(STDS[i][0], 0.0), 1.0), luminance=0.5)
f.write(str(round(c.red, 2)) + ',' + str(round(c.green, 2)) + ',' + str(round(c.blue, 2)) + '\n')