##### Importing module ######################## |
import numpy as np |
import pandas as pd |
import matplotlib.pyplot as plt |
from tkinter import* |
from tkinter import filedialog |
##### Importing user data ###################### |
root = Tk() |
Profile = filedialog.askopenfile() |
user_cols = [“Distance”, “Dose”] |
df1 = pd.read_table(Profile, names=user_cols, header=None) |
#####Define 1st and 2nd derivative with central divide difference using superposition principle -function |
h = (df1.ix[1,“Distance”] - df1.ix[0,“Distance”]) |
X = df1[“Distance”] |
Y = df1[“Dose”] |
def f1(): |
f1 = (((df1.ix[count + 1, “Dose”]) - (df1.ix[count - 1, “Dose”])) / (2 * h)) |
return f1 |
def f2(): |
f2 = (((df1.ix[count + 2, “Dose”]) - 2*(df1.ix[count, “Dose”]) + (df1.ix[count - 2, “Dose”])) / (4 * h * h)) |
return f2 |
##### Calculation first and second derivatives ##### |
X1 = df1.iloc[1:len(X)-1,0] |
X2 = df1.iloc[2:len(X)-2,0] |
Y1 = [] |
Y2 = [] |
for i in df1[“Dose”]: |
count = 1 |
while count <= (len(Y)-2): |
f1_store = Y1.append(f1()) |
count += 1 |
count = 2 |
while count <= (len(Y)-3): |
f2_store = Y2.append(f2()) |
count += 1 |
break |
df2 = pd.DataFrame(list(zip(Y1, Y2)), columns = [“f’(x)”, “f”(x)”]) |
Data = pd.concat([df1, df2], axis = 1) |
###### Finding the Beam Profile parameters ###### |
## Inflection Points |
Rt_maxslope = int(Y1.index(max(Y1))) |
Rt_ipvalues = Y2[(Rt_maxslope-6):(Rt_maxslope+4)] |
Rt_disvalue = X[(Rt_maxslope-4):(Rt_maxslope+6)] |
Rt_dosevalue = list(Y[(Rt_maxslope-4):(Rt_maxslope+6)]) |
RtSign_Change = int(np.where(np.diff(np.sign(Rt_ipvalues)))[0]) |
Lt_minslope = int(Y1.index(min(Y1))) |
Lt_ipvalues = Y2[(Lt_minslope-5):(Lt_minslope+5)] |
Lt_disvalue = X[(Lt_minslope-3):(Lt_minslope+7)] |
Lt_dosevalue = list(Y[(Lt_minslope-3):(Lt_minslope+7)]) |
LtSign_Change = int(np.where(np.diff(np.sign(Lt_ipvalues)))[0]) |
RtIP_df = pd.DataFrame(list(zip(Rt_disvalue, Rt_dosevalue, Rt_ipvalues)), columns = [“Rt_dis”, “Rt_dose”, “f”(x)”]) |
LtIP_df = pd.DataFrame(list(zip(Lt_disvalue, Lt_dosevalue, Lt_ipvalues)), columns = [“Lt_dis”, “Lt_dose”, “f”(x)”]) |
Rt_RDV = (RtIP_df.ix[RtSign_Change, “Rt_dose”] + RtIP_df.ix[RtSign_Change+1, “Rt_dose”])/2 |
Lt_RDV = (LtIP_df.ix[LtSign_Change, “Lt_dose”] + LtIP_df.ix[LtSign_Change+1, “Lt_dose”])/2 |
RDV = ((Rt_RDV)+ (Lt_RDV))/2 |
## Field Size |
Rt_Fwidth = (RtIP_df.ix[RtSign_Change, “Rt_dis”] + RtIP_df.ix[RtSign_Change+1, “Rt_dis”])/2 |
Lt_Fwidth = (LtIP_df.ix[LtSign_Change, “Lt_dis”] + LtIP_df.ix[LtSign_Change+1, “Lt_dis”])/2 |
Field_Width = (Lt_Fwidth - Rt_Fwidth) |
Field_Size = Field_Width/10 |
## Beam Penumbra |
ZeroPoint = int(df1[df1[“Distance”] = = 0].index.values.astype(int)) |
Rt_X = X[0:ZeroPoint+1] |
Rt_Y = Y[0:ZeroPoint+1] |
Rt_df = pd.DataFrame(list(zip(Rt_X, Rt_Y)), columns=[“Rt_X”, “Rt_Y”]) |
Rt_Pa = 1.6*Rt_RDV |
Rt_Pb = 0.4*Rt_RDV |
Lt_X = X[ZeroPoint:len(X)] |
Lt_Y = Y[ZeroPoint:len(X)] |
Lt_df = pd.DataFrame(list(zip(Lt_X, Lt_Y)), columns=[“Lt_X”, “Lt_Y”]) |
Lt_Pa = 1.6*Lt_RDV |
Lt_Pb = 0.4*Lt_RDV |
pa = [] |
pb = [] |
for value in Rt_Y: |
if value >= Rt_Pa: |
pa = Rt_df[Rt_df[“Rt_Y”] = = value].index.values.astype(int) |
break |
for value in Rt_Y: |
if value >= Rt_Pb: |
pb = Rt_df[Rt_df[“Rt_Y”] = = value].index.values.astype(int) |
break |
pa_Rt = (Rt_df.ix[int(pa), “Rt_X”] + Rt_df.ix[int(pa-1), “Rt_X”])/2 |
pb_Rt = (Rt_df.ix[int(pb), “Rt_X”] + Rt_df.ix[int(pb-1), “Rt_X”])/2 |
Rt_penumbra = (pb_Rt - pa_Rt) |
for value in Lt_Y: |
if value <= Lt_Pa: |
pa = Lt_df[Lt_df[“Lt_Y”] = = value].index.values.astype(int) |
break |
for value in Lt_Y: |
if value <= Lt_Pb: |
pb = Lt_df[Lt_df[“Lt_Y”] = = value].index.values.astype(int) |
break |
pa_Lt = (Lt_df.ix[int(pa-1), “Lt_X”] + Lt_df.ix[int(pa), “Lt_X”])/2 |
pb_Lt = (Lt_df.ix[int(pb-1), “Lt_X”] + Lt_df.ix[int(pb), “Lt_X”])/2 |
Lt_penumbra = (pb_Lt - pa_Lt) |
print(Data) |
print(“Right IP=”, Rt_RDV) |
print(“Left IP=”, Lt_RDV) |
print(“Average RDV=”, RDV) |
print(“Field Size=”, Field_Size, “cm”) |
print(“Rt Penumbra=”, Rt_penumbra, “mm”) |
print(“Lt Penumbra=”, Lt_penumbra, “mm”) |
###### Plotting Graph ########################### |
fig = plt.figure() |
graph = fig.add_subplot(1, 1, 1) |
graph.spines[’left’].set_position((’data’, 0.0)) |
graph.set_xlim(-150, 150) |
graph.set_ylim(-5, 100) |
major_xticks = np.arange(-150, 150, 10) |
major_yticks = np.arange(-5, 105, 5) |
minor_xticks = np.arange(-150, 150, 1) |
minor_yticks = np.arange(-5, 105, 1) |
graph.set_xticks(major_xticks) |
graph.set_xticks(minor_xticks, minor=True) |
graph.set_yticks(major_yticks) |
graph.set_yticks(minor_yticks, minor=True) |
plt.grid(b=True, which=’major’, color=’black’, linestyle=’-’) |
plt.grid(b=True, which=’minor’, color=’red’, linestyle=’-’) |
plt.plot(X, Y, label=“Beam Profile”, color=“blue”, linewidth=0.5, marker=“*”, ms=1) |
plt.plot(X2, Y2, label=“Inflection Points”, color=“black”, linewidth=0.5, marker=“*”, ms=1) |
plt.xlabel(“Distance from central axis (cm)”) |
plt.ylabel(“% Relative Dose”) |
plt.title(Profile.name) |
plt.legend() |
plt.show() |
root.mainloop() |
####################### End of program code ########################## |