Changeset 711 for mds-and-trees


Ignore:
Timestamp:
10/06/17 17:08:12 (6 years ago)
Author:
konrad
Message:

Logs without [BORN] entries (only [OFFSPRING]) should load properly now

File:
1 edited

Legend:

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

    r710 r711  
    727727            return creature
    728728
     729        def load_creature_props(creature):
     730            creature_id = get_id(creature["ID"])
     731            for prop in creature:
     732                if prop not in default_props:
     733                    if prop not in self.props:
     734                        self.props[prop] = [0 for i in range(nodes)]
     735                    self.props[prop][creature_id] = creature[prop]
     736
     737        def load_born_props(creature):
     738            nonlocal max_time
     739            creature_id = get_id(creature["ID"])
     740            if "Time" in creature:
     741                self.time[creature_id] = creature["Time"] + time_offset
     742                max_time = max(self.time[creature_id], max_time)
     743
     744        def load_offspring_props(creature):
     745            creature_id = get_id(creature["ID"])#, False)
     746            if "FromIDs" in creature:
     747                # make sure that ID's of parents are lower than that of their children
     748                for i in range(0, len(creature["FromIDs"])):
     749                    if creature["FromIDs"][i] not in self.ids:
     750                        get_id("virtual_parent")
     751
     752
     753                # we assign to each parent its contribution to the genotype of the child
     754                for i in range(0, len(creature["FromIDs"])):
     755                    if creature["FromIDs"][i] in self.ids:
     756                        parent_id = get_id(creature["FromIDs"][i])
     757                    else:
     758                        if creature["FromIDs"][i] not in merged_with_virtual_parent:
     759                            merged_with_virtual_parent.append(creature["FromIDs"][i])
     760                        parent_id = get_id("virtual_parent")
     761                    inherited = (creature["Inherited"][i] if 'Inherited' in creature else 1)
     762                    self.parents[creature_id][parent_id] = inherited
     763
     764                if "Kind" in creature:
     765                    self.kind[creature_id] = creature["Kind"]
     766            else:
     767                raise LoadingError("[OFFSPRING] misses the 'FromIDs' field!")
    729768
    730769        # counting the number of expected nodes
    731 
    732         nodes = 0
     770        nodes_born, nodes_offspring = 0, 0
    733771        for filename in filenames:
    734772            file = open(filename)
     
    739777                        line_arr = line_arr[1].split(' ', 1)
    740778                    if line_arr[0] == "[BORN]":
    741                         nodes += 1
    742 
     779                        nodes_born += 1
     780                    if line_arr[0] == "[OFFSPRING]":
     781                        nodes_offspring += 1
     782        # assuming that either BORN or OFFSPRING is present for each individual (as it should...)
     783        nodes = max(nodes_born, nodes_offspring)
    743784        nodes = min(nodes, max_nodes if max_nodes != 0 else nodes)+1
     785
    744786        self.parents = [{} for x in range(nodes)]
    745787        self.children = [[] for x in range(nodes)]
     
    776818                            break
    777819
    778                         creature_id = get_id(creature["ID"])
    779 
    780                         # debug
    781                         if loaded_so_far%1000 == 0:
    782                             #print(". " + str(creature_id) + " " + str(timelib.time() - lasttime))
    783                             lasttime = timelib.time()
    784 
    785                         if "Time" in creature:
    786                             self.time[creature_id] = creature["Time"] + time_offset
    787                             max_time = max(self.time[creature_id], max_time)
    788 
    789                         for prop in creature:
    790                             if prop not in default_props:
    791                                 if prop not in self.props:
    792                                     self.props[prop] = [0 for i in range(nodes)]
    793                                 self.props[prop][creature_id] = creature[prop]
    794 
    795                         loaded_so_far += 1
     820                        if get_id(creature["ID"], False) is None:
     821                            loaded_so_far += 1
     822
     823                        load_born_props(creature)
     824                        load_creature_props(creature)
    796825
    797826                    if line_arr[0] == "[OFFSPRING]":
     
    801830                            break
    802831
    803                         if "FromIDs" in creature:
    804                             # make sure that ID's of parents are lower than that of their children
    805                             for i in range(0, len(creature["FromIDs"])):
    806                                 if creature["FromIDs"][i] not in self.ids:
    807                                     get_id("virtual_parent")
    808 
    809                             creature_id = get_id(creature["ID"])#, False)
    810 
    811                             # we assign to each parent its contribution to the genotype of the child
    812                             for i in range(0, len(creature["FromIDs"])):
    813                                 if creature["FromIDs"][i] in self.ids:
    814                                     parent_id = get_id(creature["FromIDs"][i])
    815                                 else:
    816                                     if creature["FromIDs"][i] not in merged_with_virtual_parent:
    817                                         merged_with_virtual_parent.append(creature["FromIDs"][i])
    818                                     parent_id = get_id("virtual_parent")
    819                                 inherited = (creature["Inherited"][i] if 'Inherited' in creature else 1)
    820                                 self.parents[creature_id][parent_id] = inherited
    821 
    822                             if "Kind" in creature:
    823                                 self.kind[creature_id] = creature["Kind"]
    824                         else:
    825                             raise LoadingError("[OFFSPRING] misses the 'FromIDs' field!")
    826 
    827                     if line_arr[0] == "[DIED]" or line_arr[0] == "[BORN]":
     832                        if get_id(creature["ID"], False) is None:
     833                            loaded_so_far += 1
     834                            # load time only if there was no [BORN] yet
     835                            load_born_props(creature)
     836
     837                        load_offspring_props(creature)
     838
     839                    if line_arr[0] == "[DIED]":
    828840                        creature = try_to_load(line_arr[1])
    829841                        if not creature:
    830842                            nodes -= 1
    831843                            break
    832                         creature_id = get_id(creature["ID"], False)
    833                         if creature_id is not None:
    834                             for prop in creature:
    835                                 if prop not in default_props:
    836                                     if prop not in self.props:
    837                                         self.props[prop] = [0 for i in range(nodes)]
    838                                     self.props[prop][creature_id] = creature[prop]
     844                        if get_id(creature["ID"], False) is not None:
     845                            load_creature_props(creature)
     846                        else:
     847                            print("NOTE: entry about the death of individual " + creature["ID"] + " before it was born")
     848
     849                # debug
     850                if loaded_so_far%1000 == 0:
     851                    #print(". " + str(creature_id) + " " + str(timelib.time() - lasttime))
     852                    lasttime = timelib.time()
    839853
    840854                # breaking both loops
Note: See TracChangeset for help on using the changeset viewer.