import pandas as pd
log = pd.read_csv("../datasets/git_log_numstat_dropover.csv")
log.head()

"""
Explanation: Estimating the Knowledge Distribution within a Modular System
Question
Which author does know which parts of a system?
Idea
Working assumption: The number of commits from an author within a certain module corresponds to the existing knowledge about that module.
Data Loading
Retrieve Git log information from a Git repository
End of explanation
"""


java_prod = log[log['file'].str.contains("backend/src/main/java/")].copy()
java_prod = java_prod[~java_prod['file'].str.contains("package-info.java")]
java_prod.head()

"""
Explanation: Data Cleaning
Just keep the production code
End of explanation
"""


author_knowledge = java_prod['author'].value_counts() / java_prod['author'].count()
author_knowledge

"""
Explanation: Analysis
Which author has how much overall "knowledge"?
End of explanation
"""


java_prod['module'] = java_prod['file'].str.split("/").str[6]
java_prod['commit'] = 1
java_prod.head()

"""
Explanation: Which author does "know" which module?
End of explanation
"""


knowledge_per_module = java_prod.groupby(['module', 'author'])[['commit']].count()
knowledge_per_module['all'] = knowledge_per_module.groupby(['module'])['commit'].transform('sum')
knowledge_per_module['ratio'] = knowledge_per_module['commit'] / knowledge_per_module['all']
knowledge_per_module.head()

"""
Explanation: Interpretation
List all the existing knowledge ratios per modules and authors
End of explanation
"""


%matplotlib inline
knowledge_per_module.unstack()['ratio'].plot.bar(stacked=True);

"""
Explanation: Visualization
Plot the result for each module in a bar chart
End of explanation
"""


