Changeset 633 for mds-and-trees


Ignore:
Timestamp:
11/23/16 17:58:33 (7 years ago)
Author:
konrad
Message:

Added measure of progress (based on regression of time-distances between next children), some minor bug fixes

File:
1 edited

Legend:

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

    r628 r633  
    66import time as timelib
    77from PIL import Image, ImageDraw, ImageFont
     8from scipy import stats
     9import numpy as np
    810
    911class LoadingError(Exception):
     
    129131           end_text = "Time " + str(max(self.design.tree.time))
    130132        if self.design.TIME == "GENERATIONAL":
    131            start_text = "Depth " + str(self.design.props['adepth']['min'])
    132            end_text = "Depth " + str(self.design.props['adepth']['max'])
     133           start_text = "Depth " + str(self.design.props['adepth_min'])
     134           end_text = "Depth " + str(self.design.props['adepth_max'])
    133135
    134136        self.add_dashed_line(file, (self.width*0.7, self.h_margin), (self.width, self.h_margin))
     
    362364        self.compute_kind()
    363365        self.compute_time()
     366        self.compute_progress()
    364367        self.compute_custom()
    365368
     
    387390        x1_dist = 0
    388391        x2_dist = 0
    389         miny = y-500
    390         maxy = y+500
     392        miny = y-20
     393        maxy = y+20
    391394        i_left = bisect.bisect_left(self.y_sorted, miny)
    392395        i_right = bisect.bisect_right(self.y_sorted, maxy)
     
    554557        self.normalize_prop('children')
    555558
     559    def compute_progress(self):
     560        self.props["progress"] = [0 for x in range(len(self.tree.children))]
     561        for i in range(len(self.props['children'])):
     562            times = sorted([self.props["time"][self.tree.children[i][j]]*100000 for j in range(len(self.tree.children[i]))])
     563            if len(times) > 4:
     564                times = [times[i+1] - times[i] for i in range(len(times)-1)]
     565                #print(times)
     566                slope, intercept, r_value, p_value, std_err = stats.linregress(range(len(times)), times)
     567                self.props['progress'][i] = slope if not np.isnan(slope) and not np.isinf(slope) else 0
     568
     569        for i in range(0, 5):
     570            self.props['progress'][self.props['progress'].index(min(self.props['progress']))] = 0
     571            self.props['progress'][self.props['progress'].index(max(self.props['progress']))] = 0
     572
     573        mini = min(self.props['progress'])
     574        maxi = max(self.props['progress'])
     575        for k in range(len(self.props['progress'])):
     576            if self.props['progress'][k] == 0:
     577                self.props['progress'][k] = mini
     578
     579        #for k in range(len(self.props['progress'])):
     580        #        self.props['progress'][k] = 1-self.props['progress'][k]
     581
     582        self.normalize_prop('progress')
     583
    556584    def normalize_prop(self, prop):
    557         noneless = [v for v in self.props[prop] if type(v)==int or type(v)==float]
     585        noneless = [v for v in self.props[prop] if type(v)!=str]
    558586        if len(noneless) > 0:
    559587            max_val = max(noneless)
    560588            min_val = min(noneless)
     589            print(prop, max_val, min_val)
    561590            self.props[prop +'_max'] = max_val
    562591            self.props[prop +'_min'] = min_val
    563592            for i in range(len(self.props[prop])):
    564593                if self.props[prop][i] is not None:
    565                     self.props[prop][i] = 0 if max_val == 0 else (self.props[prop][i] - min_val) / max_val
    566 
     594                    qqq = self.props[prop][i]
     595                    self.props[prop][i] = 0 if max_val == min_val else (self.props[prop][i] - min_val) / (max_val - min_val)
    567596
    568597class TreeData:
     
    584613        default_props = ["Time", "FromIDs", "ID", "Operation", "Inherited"]
    585614
    586         ids = {}
     615        self.ids = {}
    587616        def get_id(id, createOnError = True):
    588617            if createOnError:
    589                 if id not in ids:
    590                     ids[id] = len(ids)
     618                if id not in self.ids:
     619                    self.ids[id] = len(self.ids)
    591620            else:
    592                 if id not in ids:
     621                if id not in self.ids:
    593622                    return None
    594             return ids[id]
     623            return self.ids[id]
    595624
    596625        file = open(filename)
     
    630659                        # make sure that ID's of parents are lower than that of their children
    631660                        for i in range(0, len(creature["FromIDs"])):
    632                             if creature["FromIDs"][i] not in ids:
     661                            if creature["FromIDs"][i] not in self.ids:
    633662                                get_id("virtual_parent")
    634663
     
    642671                        # we assign to each parent its contribution to the genotype of the child
    643672                        for i in range(0, len(creature["FromIDs"])):
    644                             if creature["FromIDs"][i] in ids:
     673                            if creature["FromIDs"][i] in self.ids:
    645674                                parent_id = get_id(creature["FromIDs"][i])
    646675                            else:
Note: See TracChangeset for help on using the changeset viewer.