<jupyter_start><jupyter_text>Asset Management Principles For Modern Power Systems          Topic 4: Risk assessment           			    <NAME>, Ph.D    			    <EMAIL>                      Copyright (c) 2021 dlalvareza    	                                                   	                                                                                 		                                                                  		                                                                                                            		                            IntroductionWith this notebook the asset management plan  is assessed by using Montecarlo Simulations considering the probability of failure and their impact (criticality) on the net.  CIGRE network benchmark DER in Medium Voltage Systems![title](../STATIC/01_CIGRE_MV_Distribution_DER_Assets_Criticality.svg)<jupyter_code>#!conda install --yes pyarrow<jupyter_output><empty_output><jupyter_text>Neccesary libraries<jupyter_code>import sys
import datetime 
import pandas as pd
import calendar
from ipywidgets import interact
from ipywidgets import fixed
import plotly.express as px
from plotly.subplots import make_subplots
from bokeh.io import show, push_notebook,  output_notebook<jupyter_output><empty_output><jupyter_text>Load PywerAPM libraries<jupyter_code>sys.path.insert(0,'../CASES/05_Challenge_Data/')
sys.path.insert(0,'../APM/BIN/')
from ST_AM_Contingencies_Analysis import Real_Time_Contingencies as Cont_Assessment
from APM_Module import APM 
from APM_Run import run_condition
from ARM_Run import load_criticality
from PywerAM_Scenario_Assessment import Decision_Making
from PywerAM_bokeh_tools import plot_condition_assessment#,plot_decision_making, plot_scenario,plot_condition_forecast, Plot_HI_Forecast_Stacked<jupyter_output><empty_output><jupyter_text>1. Normal opertaion   1.1 Import case settings<jupyter_code>from PywerAPM_Case_Setting import*
_,_,assets = run_condition()
asset_list_id   = list(assets.Asset_Portfolio.keys())        # List of asset id
df_ACP           = load_criticality()    # Montecarlo simulations
df_ACP['Cr']     = df_ACP['Cr']
# Load fixed criticality
df_Fixed_Cr           = load_criticality(cr_type=case_settings['Cr'],assets=assets.Asset_Portfolio_List)
df_Fixed_Cr<jupyter_output><empty_output><jupyter_text>1.2 Create contingencies assessment object<jupyter_code>%time
DMS            = Decision_Making(assets,DF_ACP=df_ACP,df_AC_Fixed=df_Fixed_Cr)
DMS.R          = 0.13   # Discount rate
DMS.date_beg   = date_beg
DMS.N_days     = n_days
# Scenario do nothing
DMS.run_scenario_base()
#DMS.load_scenario_base()
output_notebook()
def update_HI_plot(Asset_Id):
    asset         = assets.Asset_Portfolio[Asset_Id]
    df            = DMS.scenario['Base'][Asset_Id]['Con']
    print(df.head())
    p    = plot_condition_assessment(df)
    show(p, notebook_handle=True)
    push_notebook()
interact(update_HI_plot, Asset_Id=asset_list_id);
from bokeh.plotting import figure
def update_Cr_plot(Asset_Id):
    asset_name     = assets.Asset_Portfolio_List.loc[Asset_Id].Name
    df             = df_ACP[df_ACP[asset_name]==True]
    p              = figure(title="PyweAM - Asset: "+asset_name, plot_height=500, plot_width=950,background_fill_color='#808080',x_axis_type='datetime')
    p.circle(df["Date"], df["Cr"], fill_alpha=0.2, size=10)
    show(p, notebook_handle=True)
    push_notebook()
    
interact(update_Cr_plot, Asset_Id=asset_list_id);<jupyter_output><empty_output><jupyter_text>4 Risk Index\begin{equation*}RI_i \left( t \right) = POF_i \left( t \right) \times  Cr_i \left( t \right)\end{equation*}<jupyter_code>from OPT_Module import OPT
asset_id    = 1
l_asset     = assets.Asset_Portfolio[asset_id]
data        = DMS.scenario['Base'][asset_id]
opt_des     = OPT(l_asset,data)  
df_current  = opt_des.Current_Con_Rel_asseesment(n_years) 
df_current
from ST_AM_Contingencies_Analysis import Real_Time_Contingencies as Cont_Assessment
Cont_A = Cont_Assessment(case_settings,pp_case='json')
print(Cont_A.AM_Plan)
plan = Cont_A.AM_Plan
DMS.run_scenario('Test',plan)
def update_Cr_plot(Asset_Id,Scenario):
    # # # # # # # # # #            
    asset_name     = assets.Asset_Portfolio_List.loc[Asset_Id].Name
    df              = DMS.scenario[Scenario][Asset_Id]['RI']
    p             = figure(title="PyweAM - Asset: "+asset_name, plot_height=500, plot_width=950,background_fill_color='#808080',x_axis_type='datetime')
    p.line(df["date"], -df["Cr"])
    p.line(df["date"], -df["RI"], color='#FF0000',)
    
    show(p, notebook_handle=True)
    push_notebook()
    print('Risk with out AM plan '+str(round(DMS.scenario['Base'][Asset_Id]['PV'],2)))
    print('Risk with an AM plan '+ str(round(DMS.scenario['Test'][Asset_Id]['PV'],2)))
    df
interact(update_Cr_plot, Asset_Id=asset_list_id,Scenario=DMS.scenario.keys());<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.09991793188346328, 'fraction_numerical': 0.011284366023799754, 'mean_word_length': 3.4765840220385673, 'pattern_counts': {'":': 0, '<': 27, '<?xml version=': 0, '>': 27, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 17, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '202057', 'license': 'MIT', 'n_tokens_mistral': 1651, 'n_tokens_neox': 1566, 'n_words': 279, 'path': '01_Note_Books/04_Challenge_Risk_Assessment.ipynb', 'repo_name': 'cmdavidb/PywerAPM', 'size': '4901'}