<s> """<STR_LIT>""" <EOL> from windml . datasets . nrel import NREL <EOL> from windml . visualization . plot_timeseries import plot_timeseries <EOL> from windml . preprocessing . preprocessing import destroy <EOL> from windml . preprocessing . preprocessing import interpolate <EOL> from windml . preprocessing . preprocessing import repair_nrel <EOL> from sklearn . neighbors import KNeighborsRegressor <EOL> from sklearn import linear_model <EOL> from sklearn . svm import SVR <EOL> from sklearn . grid_search import GridSearchCV <EOL> from sklearn . cross_validation import KFold , cross_val_score <EOL> from sklearn . neighbors import KNeighborsRegressor <EOL> from sklearn import linear_model <EOL> from windml . mapping . power_mapping import PowerMapping <EOL> from sklearn . neighbors import KNeighborsRegressor <EOL> import matplotlib . pyplot as plt <EOL> import matplotlib . dates as md <EOL> from pylab import * <EOL> from matplotlib . ticker import FuncFormatter <EOL> from numpy import array <EOL> from itertools import product , chain <EOL> parks = { <EOL> '<STR_LIT>' : <NUM_LIT> , <EOL> } <EOL> timestep = <NUM_LIT> <EOL> methods = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> rates = [ <NUM_LIT:0.1> , <NUM_LIT> , <NUM_LIT:0.5> , <NUM_LIT> , <NUM_LIT> ] <EOL> destroy_method = '<STR_LIT>' <EOL> labels = { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : '<STR_LIT>' } <EOL> def static_mreg_knn ( windpark ) : <EOL> target = windpark . get_target ( ) <EOL> measurements = repair_nrel ( target . get_measurements ( ) [ : <NUM_LIT> ] ) <EOL> neighbors = windpark . get_turbines ( ) [ : - <NUM_LIT:1> ] <EOL> nseries = [ repair_nrel ( t . get_measurements ( ) [ : <NUM_LIT> ] ) for t in neighbors ] <EOL> iargs = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : timestep , <EOL> '<STR_LIT>' : nseries , <EOL> '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : { '<STR_LIT>' : <NUM_LIT:5> , '<STR_LIT:n>' : [ <NUM_LIT:5> , <NUM_LIT:10> , <NUM_LIT:20> , <NUM_LIT:50> , <NUM_LIT:100> ] , '<STR_LIT>' : '<STR_LIT>' } } <EOL> return iargs , nseries <EOL> def static_mreg_lin ( windpark ) : <EOL> target = windpark . get_target ( ) <EOL> measurements = repair_nrel ( target . get_measurements ( ) [ : <NUM_LIT> ] ) <EOL> neighbors = windpark . get_turbines ( ) [ : - <NUM_LIT:1> ] <EOL> nseries = [ repair_nrel ( t . get_measurements ( ) [ : <NUM_LIT> ] ) for t in neighbors ] <EOL> iargs = { '<STR_LIT>' : '<STR_LIT>' , <EOL> '<STR_LIT>' : timestep , <EOL> '<STR_LIT>' : nseries , <EOL> '<STR_LIT>' : '<STR_LIT>' } <EOL> return iargs , nseries <EOL> def static_linear ( windpark ) : <EOL> target = windpark . get_target ( ) <EOL> measurements = repair_nrel ( target . get_measurements ( ) [ : <NUM_LIT> ] ) <EOL> neighbors = windpark . get_turbines ( ) [ : - <NUM_LIT:1> ] <EOL> nseries = [ repair_nrel ( t . get_measurements ( ) [ : <NUM_LIT> ] ) for t in neighbors ] <EOL> return { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : timestep } , nseries <EOL> def static_forwardcopy ( windpark ) : <EOL> target = windpark . get_target ( ) <EOL> measurements = repair_nrel ( target . get_measurements ( ) [ : <NUM_LIT> ] ) <EOL> neighbors = windpark . get_turbines ( ) [ : - <NUM_LIT:1> ] <EOL> nseries = [ repair_nrel ( t . get_measurements ( ) [ : <NUM_LIT> ] ) for t in neighbors ] <EOL> return { '<STR_LIT>' : '<STR_LIT>' , '<STR_LIT>' : timestep } , nseries <EOL> argfuncs = { '<STR_LIT>' : static_linear , <EOL> '<STR_LIT>' : static_forwardcopy , <EOL> '<STR_LIT>' : static_mreg_knn , <EOL> '<STR_LIT>' : static_mreg_lin } <EOL> def experiment ( method , windpark , windpark_test , damaged , rate ) : <EOL> args , nseries = argfuncs [ method ] ( windpark ) <EOL> reconstructed = interpolate ( damaged , ** args ) <EOL> target = windpark . get_target ( ) <EOL> measurements = repair_nrel ( target . get_measurements ( ) [ : <NUM_LIT> ] ) <EOL> turbines = windpark . get_turbines ( ) <EOL> for t in range ( len ( turbines ) ) : <EOL> turbines [ t ] . add_measurements ( repair_nrel ( turbines [ t ] . get_measurements ( ) [ : <NUM_LIT> ] ) ) <EOL> turbines [ - <NUM_LIT:1> ] . add_measurements ( reconstructed ) <EOL> feature_window , horizon = <NUM_LIT:3> , <NUM_LIT:3> <EOL> mapping = PowerMapping ( ) <EOL> X = mapping . get_features_park ( windpark , feature_window , horizon ) <EOL> Y = mapping . get_labels_turbine ( target , feature_window , horizon ) <EOL> train_to = int ( math . floor ( len ( X ) ) ) <EOL> train_step , test_step = <NUM_LIT:1> , <NUM_LIT:1> <EOL> reg = linear_model . LinearRegression ( ) <EOL> reg = reg . fit ( X [ <NUM_LIT:0> : train_to : train_step ] , Y [ <NUM_LIT:0> : train_to : train_step ] ) <EOL> turbines = windpark_test . get_turbines ( ) <EOL> for t in turbines : <EOL> t . add_measurements ( repair_nrel ( t . get_measurements ( ) [ : <NUM_LIT> ] ) ) <EOL> target_test = windpark_test . get_target ( ) <EOL> XT = mapping . get_features_park ( windpark_test , feature_window , horizon ) <EOL> test_to = int ( math . floor ( len ( XT ) ) ) <EOL> YT = mapping . get_labels_turbine ( target_test , feature_window , horizon ) [ : test_to ] <EOL> y_hat = reg . predict ( XT [ : test_to ] ) <EOL> mse_y_hat = <NUM_LIT:0> <EOL> for i in range ( <NUM_LIT:0> , len ( y_hat ) ) : <EOL> y = YT [ i ] <EOL> mse_y_hat += ( y_hat [ i ] - y ) ** <NUM_LIT:2> <EOL> mse_y_hat /= float ( len ( y_hat ) ) <EOL> return mse_y_hat <EOL> data = [ ] <EOL> for park in parks . keys ( ) : <EOL> windpark = NREL ( ) . get_windpark_nearest ( parks [ park ] , <NUM_LIT:5> , <NUM_LIT> ) <EOL> windpark_test = NREL ( ) . get_windpark_nearest ( parks [ park ] , <NUM_LIT:5> , <NUM_LIT> ) <EOL> target = windpark . get_target ( ) <EOL> measurements = repair_nrel ( target . get_measurements ( ) [ : <NUM_LIT> ] ) <EOL> for i in xrange ( <NUM_LIT:2> ) : <EOL> damaged_series = { } <EOL> de = lambda ( rate ) : ( rate , ( destroy ( measurements , method = destroy_method , percentage = rate ) [ <NUM_LIT:0> ] ) ) <EOL> dseries = map ( de , rates ) <EOL> for rate , series in dseries : <EOL> damaged_series [ rate ] = series <EOL> def run ( pars ) : <EOL> method , rate = pars <EOL> mse = experiment ( method , windpark , windpark_test , damaged_series [ rate ] , rate ) <EOL> return method , rate , mse <EOL> results = map ( run , list ( chain ( product ( methods , rates ) ) ) ) <EOL> encoding = lambda method , rate , mse , park : { "<STR_LIT>" : method , "<STR_LIT>" : rate , "<STR_LIT>" : mse , "<STR_LIT>" : park } <EOL> results = [ encoding ( method , rate , mse , park ) for ( method , rate , mse ) in results ] <EOL> for result in results : <EOL> data . append ( result ) <EOL> mse = { } <EOL> for result in data : <EOL> keys = mse . keys ( ) <EOL> key = ( result [ '<STR_LIT>' ] , result [ '<STR_LIT>' ] , result [ '<STR_LIT>' ] ) <EOL> if ( key not in keys ) : <EOL> mse [ key ] = [ ] <EOL> mse [ key ] . append ( result [ '<STR_LIT>' ] ) <EOL> mean = { } <EOL> var = { } <EOL> std = { } <EOL> for key in mse . keys ( ) : <EOL> if ( key not in mean . keys ( ) ) : <EOL> mean [ key ] = [ ] <EOL> mean [ key ] = array ( mse [ key ] ) . mean ( ) <EOL> for key in mse . keys ( ) : <EOL> if ( key not in var . keys ( ) ) : <EOL> var [ key ] = [ ] <EOL> var [ key ] = array ( mse [ key ] ) . var ( ) <EOL> for key in mse . keys ( ) : <EOL> if ( key not in std . keys ( ) ) : <EOL> std [ key ] = [ ] <EOL> std [ key ] = array ( mse [ key ] ) . std ( ) <EOL> amean = { } <EOL> astd = { } <EOL> for method in methods : <EOL> for rate in rates : <EOL> means = [ ] <EOL> for park in parks . keys ( ) : <EOL> means . append ( mean [ method , rate , park ] ) <EOL> amean [ method , rate ] = array ( means ) . mean ( ) <EOL> astd [ method , rate ] = array ( means ) . std ( ) <EOL> with plt . style . context ( "<STR_LIT>" ) : <EOL> park = "<STR_LIT>" <EOL> figure = plt . figure ( figsize = ( <NUM_LIT:8> , <NUM_LIT:5> ) ) <EOL> for method in methods : <EOL> rmses = [ ] <EOL> yerrs = [ ] <EOL> for rate in rates : <EOL> yerrs . append ( std [ method , rate , park ] ) <EOL> rmses . append ( mean [ method , rate , park ] ) <EOL> plt . errorbar ( rates , rmses , yerr = yerrs , label = labels [ method ] , linestyle = "<STR_LIT>" , marker = "<STR_LIT:o>" ) <EOL> plt . xlabel ( "<STR_LIT>" ) <EOL> plt . ylabel ( "<STR_LIT>" ) <EOL> plt . xlim ( [ <NUM_LIT:0.1> , <NUM_LIT> ] ) <EOL> plt . ylim ( [ <NUM_LIT> , <NUM_LIT:16> ] ) <EOL> plt . legend ( loc = "<STR_LIT>" ) <EOL> plt . show ( ) </s>
