Пример #1
0
 def MIMO_probability_estimate(self):    
     """ Simulates 1000 system responses with specified quantity of 
     noise, predicts response with saved model and plots prediction
     and validation data  points and coefficient of determination."""
     
     if not(path.exists(self.errorCSV)):
         print("No simulation for these parameters exists in Uncertainty data. Proceeding with simulation")
        
         # Initialize the models that are saved using the parameters declared above
         predictor = Model(self.nstep)
         predictor.load_MIMO()
             
         deviations = np.arange(0,self.maxError)
         
         stdev = np.array([0])
         error=np.array([0])
         b_predicted = np.array([0])
         a_predicted = np.array([0])
         
         b_true_value = np.array([0])
         a_true_value = np.array([0])
         
         for deviation in deviations:
             numTrials = self.numTrials; nstep = self.nstep
             timelength = self.timelength; trainFrac = self.trainFrac
             
             # then simulates using the initialized model
             sig = Signal(numTrials,nstep,timelength,trainFrac,stdev=deviation)
             sig.MIMO_simulation(b_possible_values=[1,10],a_possible_values=[1,10])
             
             # In this case, since we are only loading the model, not trying to train it,
             # we can use function simulate and preprocess
             xData,yData = sig.MIMO_validation()
         
             # Function to make predictions based off the simulation 
             predictor.predict_MIMO(sig,savePredict=False,plotPredict=False,probabilityCall=True)
             
             error = np.concatenate((predictor.errors,error))
             b_predicted = np.concatenate((predictor.b_model_predictionictions.ravel(),b_predicted))
             a_predicted = np.concatenate((predictor.a_model_predictionictions.ravel(),a_predicted))
             
             b_true_value = np.concatenate((sig.aVals.ravel(),b_true_value))
             a_true_value = np.concatenate((sig.aVals.ravel(),a_true_value))
             stdev = np.concatenate((np.full_like(predictor.errors,deviation),stdev))
         
         # Transfer to pandas to save to CSV
         sd = pd.DataFrame()
         sd['b_model_prediction'] = b_predicted
         sd['a_model_prediction'] = a_predicted
         sd['b_true_value'] = b_true_value
         sd['a_true_value'] = a_true_value
         
         sd.to_csv(self.errorCSV, index=False)
       
     # If the simulation does exist for this set of parameters,
     # load from CSV and plot
     else:
         print("Data exists for the parameters, proceeding to producing uncertainty estimate")
         try:
             sd = pd.read_csv(self.errorCSV).drop(['Unnamed: 0'],axis=1)
             sd.drop(sd.tail(1).index,inplace=True)
             
         except:
             sd = pd.read_csv(self.errorCSV)
             sd.drop(sd.tail(1).index,inplace=True)
     
     # Plot histogram and predicted/true parameters
     self.errorDict = {}  
     pathPrefix = self.prefix+"MIMO/Plots/"
     prefixes = ['b','a']
     for (i,prefix) in enumerate(prefixes):
         sd[prefix+'Error'] = (sd[prefix+'Pred']-sd[prefix+'True'])
         h = np.std(sd[prefix+'Error'])
         self.errorDict[prefix] = h
         
         if self.plotUncertainty:
             plt.figure(dpi=200)
             plt.hist(sd[prefix+'Error'],bins=100,label='Max Error = %i%%' % self.maxError)
             plt.xlabel('Standard Error in '+ prefix)
             plt.ylabel("Frequency Distribution")
             plt.legend()
             savePath = pathPrefix + "histogram_" + prefix + ".png"
             plt.savefig(savePath)
             
             plt.figure(dpi=200)
             plt.plot(sd[prefix+'True'],sd[prefix+'Pred'],'.',
                      label='Max Error = %i%%' % self.maxError)
             plt.plot(np.linspace(1,10),np.linspace(1,10),'r--',
                      label="r\u00b2 = %.3f" % r2_score(sd[prefix+'True'],sd[prefix+'Pred']))
             plt.plot(np.linspace(1,10),np.linspace(1,10)+h,'g--',label="Stdev = %.3f" % h)
             plt.plot(np.linspace(1,10),np.linspace(1,10)-h,'g--')
             plt.ylabel("Predicted Value of "+prefix)
             plt.xlabel("True Value of "+prefix)
             plt.legend()
             savePath = pathPrefix + "determination_" + prefix + ".png"
             plt.savefig(savePath)