import numpy as np
import pandas as pd
from matplotlib import pyplot as plt

plt.rcParams['font.family'] = ['Microsoft YaHei']

def process(df, col):
    df = df[col]
    df = df.sort_values(by=col[1]).to_numpy()
    name = df[:-1, 0]
    data = df[:-1, 1]
    dSum = df[-1, 1]

    lng = len(data) - 1
    value = []
    label = []

    for k in range(lng):
        if data[lng - k] / dSum > 0.01:
            value.append(data[lng - k])
            label.append(name[lng - k])
        else:
            break

    value.append(dSum - np.sum(value))
    label.append('else')

    return value, label

df = pd.read_csv('result/anl-P.csv')

value, label = process(df, ['Particle', 'energy deposition'])
# plt.title('能量沉积（按粒子划分）')
plt.pie(value, labels=label, autopct='%0.2f%%')
plt.savefig('docs/eDep-P.png', bbox_inches='tight')
plt.close()

value, label = process(df, ['Particle', 'absorbed dose'])
# plt.title('吸收剂量（按粒子划分）')
plt.pie(value, labels=label, autopct='%0.2f%%')
plt.savefig('docs/aDoes-P.png', bbox_inches='tight')
plt.close()

value, label = process(df, ['Particle', 'equivalent dose'])
# plt.title('当量剂量（按粒子划分）')
plt.pie(value, labels=label, autopct='%0.2f%%')
plt.savefig('docs/eqDose-P.png', bbox_inches='tight')
plt.close()

value, label = process(df, ['Particle', 'effective dose'])
# plt.title('有效剂量（按粒子划分）')
plt.pie(value, labels=label, autopct='%0.2f%%')
plt.savefig('docs/efDose-P.png', bbox_inches='tight')
plt.close()

df = pd.read_csv('result/anl-O.csv')

value, label = process(df, ['Organ', 'energy deposition'])
# plt.title('能量沉积（按器官划分）')
plt.pie(value, labels=label, autopct='%0.2f%%')
plt.savefig('docs/eDep-O.png', bbox_inches='tight')
plt.close()

value, label = process(df, ['Organ', 'absorbed dose'])
# plt.title('吸收剂量（按器官划分）')
plt.pie(value, labels=label, autopct='%0.2f%%')
plt.savefig('docs/aDoes-O.png', bbox_inches='tight')
plt.close()

value, label = process(df, ['Organ', 'equivalent dose'])
# plt.title('当量剂量（按器官划分）')
plt.pie(value, labels=label, autopct='%0.2f%%')
plt.savefig('docs/eqDose-O.png', bbox_inches='tight')
plt.close()

value, label = process(df, ['Organ', 'effective dose'])
# plt.title('有效剂量（按器官划分）')
plt.pie(value, labels=label, autopct='%0.2f%%')
plt.savefig('docs/efDose-O.png', bbox_inches='tight')
plt.close()
