Changeset 709 for mds-and-trees


Ignore:
Timestamp:
10/01/17 00:06:54 (6 years ago)
Author:
konrad
Message:

tree-genealogy.py should now work correctly with the order of events in the logs BORN -> OFFSPRING -> DIED

File:
1 edited

Legend:

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

    r708 r709  
    711711            return self.ids[id]
    712712
     713        def try_to_load(input):
     714            creature = False
     715            try:
     716                creature = json.loads(input)
     717            except ValueError:
     718                print("Json format error - the line cannot be read. Breaking the loading loop.")
     719                # fixing arrays by removing the last element
     720                # ! assuming that only the last line is broken !
     721                self.parents.pop()
     722                self.children.pop()
     723                self.time.pop()
     724                self.kind.pop()
     725                self.life_lenght.pop()
     726            return creature
     727
    713728        file = open(filename)
    714729
     
    720735                if line_arr[0] == CLI_PREFIX:
    721736                    line_arr = line_arr[1].split(' ', 1)
    722                 if line_arr[0] == "[OFFSPRING]":
     737                if line_arr[0] == "[BORN]":
    723738                    nodes += 1
    724739
     
    736751        loaded_so_far = 0
    737752        lasttime = timelib.time()
     753
     754        get_id("virtual_parent")
     755
    738756        for line in file:
    739757            line_arr = line.split(' ', 1)
     
    741759                if line_arr[0] == CLI_PREFIX:
    742760                    line_arr = line_arr[1].split(' ', 1)
    743                 if line_arr[0] == "[OFFSPRING]":
    744                     try:
    745                         creature = json.loads(line_arr[1])
    746                     except ValueError:
    747                         print("Json format error - the line cannot be read. Breaking the loading loop.")
    748                         # fixing arrays by removing the last element
    749                         # ! assuming that only the last line is broken !
    750                         self.parents.pop()
    751                         self.children.pop()
    752                         self.time.pop()
    753                         self.kind.pop()
    754                         self.life_lenght.pop()
     761                if line_arr[0] == "[BORN]":
     762                    creature = try_to_load(line_arr[1])
     763                    if not creature:
    755764                        nodes -= 1
    756765                        break
    757766
     767                    creature_id = get_id(creature["ID"])
     768
     769                    # debug
     770                    if loaded_so_far%1000 == 0:
     771                        #print(". " + str(creature_id) + " " + str(timelib.time() - lasttime))
     772                        lasttime = timelib.time()
     773
     774                    if "Time" in creature:
     775                        self.time[creature_id] = creature["Time"]
     776
     777                    for prop in creature:
     778                        if prop not in default_props:
     779                            if prop not in self.props:
     780                                self.props[prop] = [0 for i in range(nodes)]
     781                            self.props[prop][creature_id] = creature[prop]
     782
     783                    loaded_so_far += 1
     784
     785                if line_arr[0] == "[OFFSPRING]":
     786                    creature = try_to_load(line_arr[1])
     787                    if not creature:
     788                        nodes -= 1
     789                        break
     790
    758791                    if "FromIDs" in creature:
    759 
    760792                        # make sure that ID's of parents are lower than that of their children
    761793                        for i in range(0, len(creature["FromIDs"])):
     
    763795                                get_id("virtual_parent")
    764796
    765                         creature_id = get_id(creature["ID"])
    766 
    767                         # debug
    768                         if loaded_so_far%1000 == 0:
    769                             #print(". " + str(creature_id) + " " + str(timelib.time() - lasttime))
    770                             lasttime = timelib.time()
     797                        creature_id = get_id(creature["ID"])#, False)
    771798
    772799                        # we assign to each parent its contribution to the genotype of the child
     
    781808                            self.parents[creature_id][parent_id] = inherited
    782809
    783                         if "Time" in creature:
    784                             self.time[creature_id] = creature["Time"]
    785 
    786810                        if "Kind" in creature:
    787811                            self.kind[creature_id] = creature["Kind"]
    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
    796812                    else:
    797813                        raise LoadingError("[OFFSPRING] misses the 'FromIDs' field!")
    798                 if line_arr[0] == "[DIED]":
    799                     creature = json.loads(line_arr[1])
     814
     815                if line_arr[0] == "[DIED]" or line_arr[0] == "[BORN]":
     816                    creature = try_to_load(line_arr[1])
     817                    if not creature:
     818                        nodes -= 1
     819                        break
    800820                    creature_id = get_id(creature["ID"], False)
    801821                    if creature_id is not None:
     
    811831
    812832        print("NOTE: all individuals with parent not provided or missing were connected to a single 'virtual parent' node: " + str(merged_with_virtual_parent))
     833
     834        for c_id in range(1, nodes):
     835            if not self.parents[c_id]:
     836                self.parents[c_id][get_id("virtual_parent")] = 1
    813837
    814838        for k in range(len(self.parents)):
Note: See TracChangeset for help on using the changeset viewer.