def dematerialize(parent_name, parent_node):  # FIXME we need to demat more than just leaves!
    #FIXME still an issue: Fornix, Striatum, Diagonal Band
    """ Remove nodes higher in the tree that occur further down the
        SAME branch. If they occur down OTHER branchs leave them alone.

        NOTE: modifies in place!
    """
    lleaves = {}
    children = parent_node[parent_name]

    if not children:  # children could be empty ? i think this only happens @ root?
        #print('at bottom', parent_name)
        lleaves[parent_name] = None
        return lleaves

    children_ord = reversed(sorted(sorted(((k, v)
                                           for k, v in children.items()),
                                          key=alphasortkey),
                                          #key=lambda a: f'{a[0]}'.split('>')[1] if '>' in f'{a[0]}' else f'a[0]'),
                                          #key=lambda a: a[0].split('>') if '>' in a[0] else a[0]),
                                   key=tcsort))  # make sure we hit deepest first

    for child_name, _ in children_ord:  # get list so we can go ahead and pop
        #print(child_name)
        new_lleaves = dematerialize(child_name, children)
        if child_name == 'magnetic resonance imaging':  # debugging failing demat
            pass
            #embed()

        if child_name in new_lleaves or all(l in lleaves for l in new_lleaves):
            # if it is a leaf or all childs are leaves as well
            if child_name in lleaves:  # if it has previously been identified as a leaf!
                #print('MATERIALIZATION DETECTED! LOWER PARENT:',
                      #lleaves[child_name],'ZAPPING!:', child_name,
                      #'OF PARENT:', parent_name)
                children.pop(child_name)
                #print('cn', child_name, 'pn', parent_name, 'BOTTOM')
            #else:  # if it has NOT previously been identified as a leaf, add the parent!
                #new_lleaves[child_name] = parent_name  # pass it back up to nodes above
                #print('cn', child_name, 'pn', parent_name)
        #else:  # it is a node but we want to dematerizlize them too!
        lleaves[child_name] = parent_name

        lleaves.update(new_lleaves)

    return lleaves
def dematerialize ( parent_name , parent_node ) : lleaves = { } children = parent_node [ parent_name ] if not children : lleaves [ parent_name ] = None return lleaves children_ord = reversed ( sorted ( sorted ( ( ( k , v ) for k , v in children . items ( ) ) , key = alphasortkey ) , key = tcsort ) ) for child_name , _ in children_ord : new_lleaves = dematerialize ( child_name , children ) if child_name == 'magnetic resonance imaging' : pass if child_name in new_lleaves or all ( l in lleaves for l in new_lleaves ) : if child_name in lleaves : children . pop ( child_name ) lleaves [ child_name ] = parent_name lleaves . update ( new_lleaves ) return lleaves
Remove nodes higher in the tree that occur further down the
        SAME branch. If they occur down OTHER branchs leave them alone.

        NOTE: modifies in place!