<filename>MeshExperiments/hierarchy.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 18 10:41:42 2019

@author: enzoampil
"""

import pandas as pd
import ast
import numpy as np

df = pd.read_csv('/Users/enzoampil/ml/temasek/bootstrap/tagger/desc2019_normalized.csv')
df = df[['DescriptorName.String', 'TreeNumberList.TreeNumber']]

df.shape

# There are descriptors with multiple branches
ast.literal_eval(df['TreeNumberList.TreeNumber'].iloc[1])

#%debug
roots = []
for i, desc in enumerate(df['TreeNumberList.TreeNumber'].values.tolist()):
    if str(desc) == 'nan':
        desc_proc = desc
        continue
    if desc[0] == '[':
        desc_proc = ast.literal_eval(desc)
    else:
        desc_proc = [desc]
    roots.append(desc_proc)
    
# Count the number of branches connected to each root
root_lens = [len(root) for root in roots]

# On average, children have 2 parents
sum(root_lens)/len(root_lens)

# The len of the list created by splitting the tree code based on periods is the number of parents
# This can be seen as the descriptors level in the hierarchy
root_parent_count = [len(root[0].split('.')) for root in roots]

# The average root has 5 parents
sum(root_parent_count)/len(root_parent_count)

class RootProcessor():
    '''
    Processor for adjusting a MeSH root to one of its parent descriptors
    '''
    def __init__(self, tree_code):
        self.orig_tree_code = tree_code
        self.orig_depth = len(tree_code.split('.'))
        self.current_tree_code = tree_code
        self.current_depth = len(tree_code.split('.'))
    
    def decrease_depth(self, new_depth):
        '''
        Returns tree code at the specified lower depth
        '''
        assert new_depth < self.current_depth, 'New depth must be lower than current depth ({})'\
                                                .format(self.current_depth)
        self.current_tree_code = '.'.join(self.current_tree_code.split('.')[:new_depth])
        self.current_depth = len(self.current_tree_code.split('.'))
        
    def reset_tree_code(self):
        return RootProcessor(self.orig_tree_code)

ex = RootProcessor('D03.633.100.221.173')
ex.orig_depth
ex.decrease_depth(4)
ex.current_tree_code

<filename>MeshExperiments/hierarchy.py
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Jun 18 10:41:42 2019

@author: enzoampil
"""

import pandas as pd
import ast
import numpy as np

df = pd.read_csv('/Users/enzoampil/ml/temasek/bootstrap/tagger/desc2019_normalized.csv')
df = df[['DescriptorName.String', 'TreeNumberList.TreeNumber']]

df.shape

# There are descriptors with multiple branches
ast.literal_eval(df['TreeNumberList.TreeNumber'].iloc[1])

#%debug
roots = []
for i, desc in enumerate(df['TreeNumberList.TreeNumber'].values.tolist()):
    if str(desc) == 'nan':
        desc_proc = desc
        continue
    if desc[0] == '[':
        desc_proc = ast.literal_eval(desc)
    else:
        desc_proc = [desc]
    roots.append(desc_proc)
    
# Count the number of branches connected to each root
root_lens = [len(root) for root in roots]

# On average, children have 2 parents
sum(root_lens)/len(root_lens)

# The len of the list created by splitting the tree code based on periods is the number of parents
# This can be seen as the descriptors level in the hierarchy
root_parent_count = [len(root[0].split('.')) for root in roots]

# The average root has 5 parents
sum(root_parent_count)/len(root_parent_count)

class RootProcessor():
    '''
    Processor for adjusting a MeSH root to one of its parent descriptors
    '''
    def __init__(self, tree_code):
        self.orig_tree_code = tree_code
        self.orig_depth = len(tree_code.split('.'))
        self.current_tree_code = tree_code
        self.current_depth = len(tree_code.split('.'))
    
    def decrease_depth(self, new_depth):
        '''
        Returns tree code at the specified lower depth
        '''
        assert new_depth < self.current_depth, 'New depth must be lower than current depth ({})'\
                                                .format(self.current_depth)
        self.current_tree_code = '.'.join(self.current_tree_code.split('.')[:new_depth])
        self.current_depth = len(self.current_tree_code.split('.'))
        
    def reset_tree_code(self):
        return RootProcessor(self.orig_tree_code)

ex = RootProcessor('D03.633.100.221.173')
ex.orig_depth
ex.decrease_depth(4)
ex.current_tree_code

#!/usr/bin/env python3 # -*- coding: utf-8 -*- Created on Tue Jun 18 10:41:42 2019  @author: enzoampil # There are descriptors with multiple branches #%debug # Count the number of branches connected to each root # On average, children have 2 parents # The len of the list created by splitting the tree code based on periods is the number of parents # This can be seen as the descriptors level in the hierarchy # The average root has 5 parents Processor for adjusting a MeSH root to one of its parent descriptors Returns tree code at the specified lower depth