import pandas
import numpy as np

"""
Explanation: Solution of Fox et al. 2015
End of explanation
"""


fox = pandas.read_csv("../data/Fox2015_data.csv")

fox.columns

"""
Explanation: First, we read the input, and take a look at the column names
End of explanation
"""


unique_ms = list(set(fox['MsID']))
num_ms = len(unique_ms)
print(num_ms)

"""
Explanation: Extract the unique manuscripts and count them
End of explanation
"""


num_reviewers = []
final_decision = []
year = []

for ms in unique_ms:
    # extract the rows
    subset = fox[fox['MsID'] == ms]
    # count number of reviewers by summing ReviewerAgreed
    num_reviewers.append(sum(subset['ReviewerAgreed']))
    # extract final decision
    if list(subset['FinalDecision'])[0] == 1:
        final_decision.append(1)
    else:
        final_decision.append(0)
    # extract year
    year.append(list(subset['Year'])[0])

# convert to np.array
num_reviewers = np.array(num_reviewers)
final_decision = np.array(final_decision)
year = np.array(year)

"""
Explanation: Now we want to elaborate the data, by saving into lists the number of reviewers, the final decision, and the year for each manuscript. At the end of the code, we convert the lists into np.arrays, as it is much easier to subset them.
End of explanation
"""


def get_prob_rejection(my_year = 'all'):
    # subset the data
    if my_year != 'all':
        my_num_reviewers = num_reviewers[year == my_year]
        my_final_decision = final_decision[year == my_year]
    else:
        my_num_reviewers = num_reviewers
        my_final_decision = final_decision
    # start printing output
    print("===============================")
    print("Year:", my_year)
    print("Submissions:", len(my_final_decision))
    print("Overall rejection rate:", 
          round(my_final_decision.mean(),3))
    print("NumRev", '\t', "NumMs", '\t', "rejection rate")
    for i in range(max(my_num_reviewers) + 1):
        print(i, '\t',
              len(my_final_decision[my_num_reviewers == i]), '\t',
              round(my_final_decision[my_num_reviewers == i].mean(), 3))
    print("===============================")

"""
Explanation: Now we write a function that takes a year as input, and prints the rejection rate for each number of reviewers, along with some other summary information. If we call the function with 'all' instead of a year, then the analysis is performed on the whole data set.
End of explanation
"""


get_prob_rejection('all')

"""
Explanation: ### Compile a table measuring the probability of rejection given the   number of reviewers. Does having more reviewers increase the   probability of being rejected?
End of explanation
"""


get_prob_rejection(2009)

for yr in range(2004, 2015):
    get_prob_rejection(yr)

"""
Explanation: It seems so. Especially, look at the difference between one and two reviewers...
Repeat the analysis above for each year represented in the database.
We can simply call the function for each year. For example:
End of explanation
"""


