# ---
# 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
# ---

# %load_ext line_profiler
# %load_ext memory_profiler

# +
from matplotlib import rcParams
from smithplot import SmithAxes

def Smith_2lines(xx=[0.0,0],y1=[0.0,0],y2=[0.0,0],name0=0,name1=0,name2=0.0):
    xx1=xx
    xx2=xx
    yy1=y1
    yy2=y2
    
    ax = plt.subplot(1, 1, 1, projection='smith')
    plt.delaxes(ax)
    
    ax = plt.subplot(1, 1, 1, projection='smith')
    rcParams['figure.figsize'] = 12, 12
    rcParams['axes.linewidth'] = 1
    rcParams['font.size'] = 15
#    X_test1=xx1
#    y_pred1=yy1
#    nested_pred1 = np.c_[X_test1,y_pred1.T] #add y_pred as a new column to X_test
#    R1_n = np.unique(X_test1[:, 0]) / 50 #extract R1 from X_test in Z format
#    I1_n = np.unique(X_test1[:, 1]) / 50
#    board_1 = np.full([len(I1_n),len(R1_n)], np.nan) #generate empty board to match contour() format
    
    X_test1=xx1
    y_pred1=yy1
    nested_pred1 = np.c_[X_test1,y_pred1.T] #add y_pred as a new column to X_test
    R1_n = np.unique(X_test1[:, 0]) / 50 #extract R1 from X_test in Z format
    I1_n = np.unique(X_test1[:, 1]) / 50
    board_1 = np.full([len(I1_n),len(R1_n)], np.nan) #generate empty board to match contour() format
    pred_pan1 = pd.DataFrame(board_1, index = I1_n , columns = R1_n)
    
    X_test2=xx2
    y_pred2=yy2
    nested_pred2 = np.c_[X_test2,y_pred2.T] #add y_pred as a new column to X_test
    R2_n = np.unique(X_test2[:, 0]) / 50 #extract R1 from X_test in Z format
    I2_n = np.unique(X_test2[:, 1]) / 50
    board_2 = np.full([len(I2_n),len(R2_n)], np.nan) #generate empty board to match contour() format
    pred_pan2 = pd.DataFrame(board_2, index = I2_n , columns = R2_n)
    
    for i in range(len(nested_pred1)): #fill the values
        pred_pan1[nested_pred1[i, 0] / 50][nested_pred1[i, 1] / 50] = nested_pred1[i, 2]
    for i in range(len(nested_pred2)): #fill the values
        pred_pan2[nested_pred2[i, 0] / 50][nested_pred2[i, 1] / 50] = nested_pred2[i, 2]
    y_for_xx1 = pred_pan1.values #convert from pandas to numpy
    y_for_xx2 = pred_pan2.values

    h, v = np.meshgrid(R1_n, I1_n)
    #P_e1 = np.reshape(y_pred,(len(I1_n),len(R1_n)))
    heights = [10.0, 20.0, 30.0, 40.0, 50.0, 60.0, 70.0, 80.0, 90.0]
    cs1 = plt.contour(h, v, y_for_xx1, levels = heights, colors = 'blue', linewidths = 0.9, linestyles = 'solid', alpha = 0.7)
    cs2 = plt.contour(h, v, y_for_xx2, levels = heights, colors = 'red', linewidths = 0.9, linestyles = 'solid', alpha = 0.7)
    plt.clabel(cs1, inline = 1, inline_spacing = 18, fontsize = 10, fmt = '%1.1f')
    plt.clabel(cs2, inline = 1, inline_spacing = 18, fontsize = 10, fmt = '%1.1f')

    cs1.collections[0].set_label('Original')
    cs2.collections[0].set_label('Predicted')
    plt.legend(loc="upper right",frameon = True)
    plt.show()
    #plt.savefig('./XGB_output/'+str(name0)+'_'+str(name1)+'_'+str(name2)+'.pdf')


# -

# ## Start Profiling with importing the model
#
# What you can import include:<br/>
# sl_rf_memory<br/>
# sl_xgboost_memory<br/>
# sl_dnn_memory<br/>
# sl_svr_memory

# +
import sl_rf_memory
from sl_rf_memory import *

# import warnings filter
from warnings import simplefilter
# ignore all future warnings
simplefilter(action='ignore', category=FutureWarning)
# -

# ### Try to draw a smith chart 
#
# You do not want to jump over this step and it would be fine if errors occur because lack of data.

Smith_2lines()

# ### Memory line-by-line profiling

# #%memit sl_rf_memory.run_it()
# %mprun -f sl_rf_memory.run_it sl_rf_memory.run_it()

# ### Runtime line-by-line profiling

# %lprun -f sl_rf_memory.run_it sl_rf_memory.run_it()

StarcoderdataJupyterScriptsDedupFiltered