Changeset 702 for mds-and-trees


Ignore:
Timestamp:
09/21/17 15:05:17 (3 years ago)
Author:
konrad
Message:

Non-normalized step values for cmap can now extend outside the range of values from the data (yet min and max step values should still be the same for all color channels!)

File:
1 edited

Legend:

Unmodified
Added
Removed
  • mds-and-trees/tree-genealogy.py

    r701 r702  
    44import argparse
    55import bisect
     6import copy
    67import time as timelib
    78from PIL import Image, ImageDraw, ImageFont
     
    9293
    9394    def compile_cmaps(self):
    94 
    95         # normalization of color breaking points
    96         for part in ['dots', 'lines']:
    97             if self.settings[part]['color']['cmap'] \
    98                     and self.settings[part]['color']['normalize_cmap']:
    99                 cmap = self.settings[part]['color']['cmap']
    100                 min = self.design.props[self.settings[part]['color']['meaning'] + "_min"]
    101                 max = self.design.props[self.settings[part]['color']['meaning'] + "_max"]
    102                 for key in cmap:
    103                     for arr in cmap[key]:
    104                         arr[0] = (arr[0] - min) / (max-min)
    105                     if cmap[key][0][0] != 0:
    106                         cmap[key].insert(0, cmap[key][0][:])
    107                         cmap[key][0][0] = 0
    108                     if cmap[key][-1][0] != 1:
    109                         cmap[key].append(cmap[key][-1][:])
    110                         cmap[key][-1][0] = 1
    111 
     95        def normalize_and_compile_cmap(cmap):
     96            for key in cmap:
     97                for arr in cmap[key]:
     98                    arr[0] = (arr[0] - cmap[key][0][0]) / (cmap[key][-1][0] - cmap[key][0][0])
     99            return colors.LinearSegmentedColormap('Custom', cmap)
    112100
    113101        for part in ['dots', 'lines']:
    114102            if self.settings[part]['color']['cmap']:
    115                 cmap = self.settings[part]['color']['cmap']
    116                 for key in cmap:
    117                     new_arr = []
    118                     for arr in cmap[key]:
    119                         new_arr.append(tuple(arr))
    120                     cmap[key] = tuple(new_arr)
    121                 #print(cmap)
    122                 self.settings[part]['color']['cmap'] = colors.LinearSegmentedColormap('Custom', cmap)
     103                if self.settings[part]['color']['normalize_cmap']:
     104                    cmap = self.settings[part]['color']['cmap']
     105                    min = self.design.props[self.settings[part]['color']['meaning'] + "_min"]
     106                    max = self.design.props[self.settings[part]['color']['meaning'] + "_max"]
     107
     108                    for key in cmap:
     109                        if cmap[key][0][0] > min:
     110                            cmap[key].insert(0, cmap[key][0][:])
     111                            cmap[key][0][0] = min
     112                        if cmap[key][-1][0] < max:
     113                            cmap[key].append(cmap[key][-1][:])
     114                            cmap[key][-1][0] = max
     115
     116                    og_cmap = normalize_and_compile_cmap(copy.deepcopy(cmap))
     117
     118                    col2key = {'red':0, 'green':1, 'blue':2}
     119                    for key in cmap:
     120                        # for color from (r/g/b) #n's should be the same for all keys!
     121                        n_min = (min - cmap[key][0][0]) / (cmap[key][-1][0] - cmap[key][0][0])
     122                        n_max = (max - cmap[key][0][0]) / (cmap[key][-1][0] - cmap[key][0][0])
     123
     124                        min_col = og_cmap(n_min)
     125                        max_col = og_cmap(n_max)
     126
     127                        cmap[key][0] = [min, min_col[col2key[key]], min_col[col2key[key]]]
     128                        cmap[key][-1] = [max, max_col[col2key[key]], max_col[col2key[key]]]
     129                print(self.settings[part]['color']['cmap'])
     130                self.settings[part]['color']['cmap'] = normalize_and_compile_cmap(self.settings[part]['color']['cmap'])
    123131
    124132    def draw_dots(self, file, min_width, max_width, max_height):
Note: See TracChangeset for help on using the changeset viewer.