# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python 3
#     language: python
#     name: python3
# ---

#
# ## **Calculating Metric**
# * Below you can find the calculation of the base metrics, which is a simple count for the used methods from py_trees_ros library, and similar calculation for BehaviorTree.CPP

import pandas as pd
import csv
import matplotlib.pyplot as plt
import seaborn as sns
from matplotlib.pyplot import figure
import os.path

# +
## for python codes analysis

## change the path in case the raw model data is moved
###please add where you saved the downloaded raw data for py_trees_ros
path = "/home/jovyan/src/notebooks/pytreeros/" ## the provided path is for running on docker image
count_dic = {}

for filename in os.listdir(path):
    with open(path+'/'+filename) as f:
        try:
            count_Sequence =0
            count_Selector =0
            count_Chooser =0
            count_Parallel =0
            count_ReactiveSeq = 0
            count_RSequence = 0
            count_old_decorator = 0
            for line in f:
                count_Sequence += line.count("composites.Sequence")
#                count_Sequence += line.count("Sequence(") ##refill project and smarc
                count_Selector += line.count("composites.Selector")
                count_Chooser += line.count("composites.Chooser")
                count_Parallel += line.count("composites.Parallel")
                count_ReactiveSeq += line.count("ReactiveSeq(name") ### used in Smarc
                count_RSequence += line.count("RSequence(name=") ## used in mobile_robot_project
                count_old_decorator += line.count("py_trees.meta") ## used in dyno and gizmo and refill
            count_dic[filename] = [count_Sequence,count_Selector, count_Chooser, count_Parallel,count_ReactiveSeq,count_RSequence,count_old_decorator]
        except IsADirectoryError:
            print("error : {}".format(filename)) 

      
     
print("Number of files that we calculated the metric for: {}".format(len(count_dic)))



# +
## save the metric into csv

with open('python_codes_analysis.csv', 'w') as f:  # Just use 'w' mode in 3.x
    w = csv.DictWriter(f, count_dic.keys())
    w.writeheader()
    w.writerow(count_dic)
print("Download complete for the metric into csv")


# +
## for XML codes analysis

## change the path in case the raw model data is moved
###please add where you saved the downloaded raw data for BehaviorTree.CPP
path = "/home/jovyan/src/notebooks/behaviortreecpp/" ## the provided path is for running on docker image
count_dic = {}

for filename in os.listdir(path):
    with open(path+'/'+filename) as f:
        try:
            count_Sequence =0
            count_ReactiveSelector = 0          
            count_SequenceStar = 0
            count_ReactiveSeq = 0
            count_action = 0
            count_Selector =0            
            count_condition = 0
            count_root = 0
            count_blackboard = 0
            count_RetryUntilSuccesful = 0
            count_repeat = 0
            count_RecoveryNode = 0
            count_ForceSuccess = 0
            count_ForceFailure = 0
            count_inverter = 0
            count_parallel = 0
            count_BlackboardCheckString = 0
            count_Negation = 0
            count_Switch = 0 


            for line in f:
#                if re.match("!--", line, re.IGNORECASE): 
#                    continue
#                else:
                count_condition += line.count("<Condition")
                count_action += line.count("<Action ")
                count_Sequence += line.count("</Sequence>")
                count_Sequence += line.count("</ActivationSequence>") ## IntelligentRoboticsLabs_robocup2020_bt_receptionist
                count_SequenceStar += line.count("</SequenceStar>")
                count_ReactiveSeq += line.count("</ReactiveSequence>")
                count_ReactiveSelector += line.count("</ReactiveFallback>")
                count_Selector += line.count("</Fallback>")
                count_inverter += line.count("</Inverter>")
                count_root += line.count("<root main_tree_to_execute")
                count_blackboard += line.count("<SetBlackboard")
                count_BlackboardCheckString  += line.count("</BlackboardCheckString>")
                count_RetryUntilSuccesful += line.count( "</RetryUntilSuccesful>")
                count_repeat += line.count("</Repeat>")
                count_ForceSuccess += line.count("</ForceSuccess>")
                count_ForceFailure += line.count("</ForceFailure>")
                count_Negation += line.count("</Negation>")
                count_Switch += line.count("</Switch")
                count_parallel += line.count("</Parallel>")
                count_RecoveryNode += line.count("</RecoveryNode>") ##MROS-RobMoSys-ITP_Pilot-URJC_navigate

                
                
            
            count_dic[filename] = [count_condition,count_action,count_Sequence,count_SequenceStar,count_ReactiveSeq,count_ReactiveSelector,count_Selector, count_inverter,count_root, count_blackboard, count_BlackboardCheckString,count_RetryUntilSuccesful,count_repeat,count_ForceSuccess,count_ForceFailure,count_Negation,count_Switch, count_parallel,count_RecoveryNode]
        except IsADirectoryError:
            print("error : {}".format(filename)) 


      
     
print("Number of files that we calculated the metric for: {}".format(len(count_dic)))

# -

## save the metric into csv
with open('XML_codes_analysis.csv', 'w') as f:  # Just use 'w' mode in 3.x
    w = csv.DictWriter(f, count_dic.keys())
    w.writeheader()
    w.writerow(count_dic)
print("Download complete for the metric into csv")

StarcoderdataJupyterScriptsDedupFiltered