Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fixed single-value-duplication error for SKL ROMs #555

Merged
merged 2 commits into from
Feb 1, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion framework/SupervisedLearning.py
Original file line number Diff line number Diff line change
Expand Up @@ -2207,7 +2207,7 @@ def __trainLocal__(self,featureVals,targetVals):
if self.intrinsicMultiTarget:
self.ROM[0].fit(featureVals,targetVals)
else:
if not all([len(np.unique(targetVals[:,index]))>1 for index in range(len(self.ROM))]):
if all([len(np.unique(targetVals[:,index])) == 1 for index in range(len(self.ROM))]):
self.myNumber = [np.unique(targetVals[:,index])[0] for index in range(len(self.ROM)) ]
self.evaluate = self._readdressEvaluateConstResponse
else:
Expand Down
129 changes: 129 additions & 0 deletions tests/framework/ROM/TimeDepSKL/projectile.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,129 @@
# Copyright 2017 Battelle Energy Alliance, LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#***************************************
#* Simple analytic test ExternalModule *
#***************************************
#
# Simulates time-dependent track of a projectile through the air from start to 0,
# assuming no air resistance.
# Inputs:
# (x0,y0) - initial position
# v0 - initial total velocity
# ang - angle of initial motion, in degrees, with respect to flat ground
# Outputs:
# (x,y) - vector positions of projectile in time
# t - corresponding time steps
#
import numpy as np

def prange(v,th,y0=0,g=9.8):
"""
Calculates the analytic range.
@ In, v, float, velocity of the projectile
@ In, th, float, angle to the ground for initial projectile motion
@ In, y0, float, optional, initial height of projectile
@ In, g, float, optional, gravitational constant (m/s/s)
@ Out, prange, float, range
"""
return v*np.cos(th)/g * (v*np.sin(th) + np.sqrt(v*v*np.sin(th)**2+2.*g*y0))

def time_to_ground(v,th,y0=0,g=9.8):
"""
Calculates the analytic time of flight
@ In, v, float, velocity of the projectile
@ In, th, float, angle to the ground for initial projectile motion
@ In, y0, float, optional, initial height of projectile
@ In, g, float, optional, gravitational constant (m/s/s)
@ Out, time_to_ground, float, time projectile is above the ground
"""
return v*np.sin(th)/g + np.sqrt(v*v*np.sin(th)**2+2.*g*y0)/g

def x_pos(x0,v,t):
"""
Calculates the x position in time
@ In, x0, float, initial horizontal position
@ In, v, float, velocity of the projectile
@ In, t, float, time of flight
@ Out, x_pos, float, horizontal position
"""
return x0 + v*t

def y_pos(y0,v,t):
"""
Calculates the analytic vertical position in time
@ In, y0, float, initial vertical position
@ In, v, float, velocity of the projectile
@ In, t, float, time of flight
@ Out, y_pos, float, vertical position
"""
return y0 + v*t - 4.9*t*t

def run(self,Input):
"""
Method require by RAVEN to run this as an external model.
@ In, self, object, object to store members on
@ In, Input, dict, dictionary containing inputs from RAVEN
@ Out, None
"""
x0 = Input.get('x0',0.0)
y0 = Input.get('y0',0.0)
v0 = Input.get('v0',1.0)
ang = Input.get('angle',45.)*np.pi/180.
self.x0 = x0
self.y0 = y0
self.v0 = v0
self.ang = ang

ts = np.linspace(0,0.1,10)
#ts = np.linspace(0,time_to_ground(v0,ang,y0),20)

vx0 = np.cos(ang)*v0
vy0 = np.sin(ang)*v0
r = prange(v0,ang,y0)

self.x = np.zeros(len(ts))
self.y = np.zeros(len(ts))
self.r = np.zeros(len(ts))
for i,t in enumerate(ts):
self.x[i] = x_pos(x0,vx0,t)
self.y[i] = y_pos(y0,vy0,t)
self.r[i] = r
self.time = ts

#can be used as a code as well
if __name__=="__main__":
import sys
inFile = sys.argv[sys.argv.index('-i')+1]
outFile = sys.argv[sys.argv.index('-o')+1]
#construct the input
Input = {}
for line in open(inFile,'r'):
arg,val = (a.strip() for a in line.split('='))
Input[arg] = float(val)
#make a dummy class to hold values
class IO:
"""
Dummy class to hold values like RAVEN does
"""
pass
io = IO()
#run the code
run(io,Input)
#write output
outFile = open(outFile+'.csv','w')
outFile.writelines('x0,y0,v0,ang,r,t,x,y\n')
inpstr = ','.join(str(i) for i in (io.x0,io.y0,io.v0,io.ang))
for i in range(len(io.time)):
outFile.writelines(inpstr+',%f,%f,%f,%f\n' %(io.r[i],io.x[i],io.y[i],io.time[i]))
outFile.close()
11 changes: 11 additions & 0 deletions tests/framework/ROM/gold/TimeDepSKL/innerHS_0.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
y,x,time
0.0,0.0,0.0
0.00725180374158,0.00785674201318,0.0111111111111
0.0132937309399,0.0157134840264,0.0222222222222
0.0181257815951,0.0235702260396,0.0333333333333
0.0217479557071,0.0314269680527,0.0444444444444
0.0241602532758,0.0392837100659,0.0555555555556
0.0253626743013,0.0471404520791,0.0666666666667
0.0253552187836,0.0549971940923,0.0777777777778
0.0241378867228,0.0628539361055,0.0888888888889
0.0217106781187,0.0707106781187,0.1
11 changes: 11 additions & 0 deletions tests/framework/ROM/gold/TimeDepSKL/innerHS_1.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
y,x,time
0.0,0.0,0.0
0.0229652877679,0.0235702260396,0.0111111111111
0.0447206989927,0.0471404520791,0.0222222222222
0.0652662336742,0.0707106781187,0.0333333333333
0.0846018918125,0.0942809041582,0.0444444444444
0.102727673408,0.117851130198,0.0555555555556
0.11964357846,0.141421356237,0.0666666666667
0.135349606968,0.164991582277,0.0777777777778
0.149845758934,0.188561808316,0.0888888888889
0.163132034356,0.212132034356,0.1
11 changes: 11 additions & 0 deletions tests/framework/ROM/gold/TimeDepSKL/innerHS_2.csv
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
y,x,time
0.0,0.0,0.0
0.0386787717943,0.0392837100659,0.0111111111111
0.0761476670454,0.0785674201318,0.0222222222222
0.112406685753,0.117851130198,0.0333333333333
0.147455827918,0.157134840264,0.0444444444444
0.181295093539,0.19641855033,0.0555555555556
0.213924482618,0.235702260396,0.0666666666667
0.245343995153,0.274985970461,0.0777777777778
0.275553631145,0.314269680527,0.0888888888889
0.304553390593,0.353553390593,0.1
127 changes: 127 additions & 0 deletions tests/framework/ROM/skl_timedep.xml
Original file line number Diff line number Diff line change
@@ -0,0 +1,127 @@
<Simulation verbosity='debug'>
<TestInfo>
<name>framework.ROM.SKL_time</name>
<author>talbpaul</author>
<created>2018-01-25</created>
<classesTested>SupervisedLearning.SciKitLearn</classesTested>
<description>
Tests the construction and sampling of time-dependent ScitKitLearn ROMs. In particular,
tests that if one target (time) has all the same values, but not others, right action is still
taken. NOTE that when this conflicts with the dataobject-rework branch, accept ALL the dataobject-rework
versions.
</description>
</TestInfo>

<RunInfo>
<WorkingDir>TimeDepSKL</WorkingDir>
<Sequence>sample,train,resample,print</Sequence>
<batchSize>1</batchSize>
</RunInfo>

<Steps>
<MultiRun name="sample">
<Input class="DataObjects" type="PointSet">placeholder</Input>
<Model class="Models" type="ExternalModel">projectile</Model>
<Sampler class="Samplers" type="Grid">grid</Sampler>
<Output class="DataObjects" type="HistorySet">train</Output>
</MultiRun>
<MultiRun name="resample">
<Input class="DataObjects" type="PointSet">placeholder</Input>
<Model class="Models" type="ExternalModel">knn</Model>
<Sampler class="Samplers" type="Grid">grid</Sampler>
<Output class="DataObjects" type="PointSet">innerPS</Output>
<Output class="DataObjects" type="HistorySet">innerHS</Output>
</MultiRun>
<IOStep name="print">
<Input class="DataObjects" type="PointSet">innerPS</Input>
<Input class="DataObjects" type="HistorySet">innerHS</Input>
<Input class="DataObjects" type="HistorySet">train</Input>
<Output class="OutStreams" type="Print">innerPS</Output>
<Output class="OutStreams" type="Print">innerHS</Output>
<Output class="OutStreams" type="Print">train</Output>
</IOStep>
<RomTrainer name="train">
<Input class="DataObjects" type="PointSet">train</Input>
<Output class="Models" type="ROM">knn</Output>
</RomTrainer>
</Steps>

<DataObjects>
<PointSet name="placeholder">
<Input>v0,y0,ang</Input>
<Output>OutputPlaceHolder</Output>
</PointSet>
<PointSet name="innerPS">
<Input>v0,y0,ang</Input>
<Output>y,x</Output>
</PointSet>
<HistorySet name="train">
<Input>v0,y0,ang</Input>
<Output>y,x,time</Output>
<options>
<pivotParameter>time</pivotParameter>
</options>
</HistorySet>
<HistorySet name="innerHS">
<Input>v0,y0,ang</Input>
<Output>y,x,time</Output>
<options>
<pivotParameter>time</pivotParameter>
</options>
</HistorySet>
</DataObjects>

<Models>
<ExternalModel name="projectile" ModuleToLoad="projectile" subType="">
<variables>v0,y0,ang,x,y,time</variables>
</ExternalModel>
<ROM name="knn" subType="SciKitLearn">
<Features>v0,y0,ang</Features>
<Target>x,y,time</Target>
<SKLtype>neighbors|KNeighborsRegressor</SKLtype>
<n_neighbors>1</n_neighbors>
</ROM>
<ROM name="rom" subType="SciKitLearn">
<Features>v0,y0,ang</Features>
<Target>x,y,time</Target>
<SKLtype>svm|SVR</SKLtype>
<kernel>linear</kernel>
<C>10.0</C>
<tol>0.0001</tol>
<coef0>0.0</coef0>
</ROM>
</Models>

<Samplers>
<Grid name="grid">
<variable name="v0">
<distribution>v0_dist</distribution>
<grid construction='equal' type='CDF' steps='2'>0 1</grid>
</variable>
<constant name="y0">0</constant>
<constant name="ang">45</constant>
</Grid>
</Samplers>

<Distributions>
<Uniform name="v0_dist">
<lowerBound>1</lowerBound>
<upperBound>5</upperBound>
</Uniform>
</Distributions>

<OutStreams>
<Print name="innerPS">
<source>innerPS</source>
<type>csv</type>
</Print>
<Print name="innerHS">
<source>innerHS</source>
<type>csv</type>
</Print>
<Print name="train">
<source>train</source>
<type>csv</type>
</Print>
</OutStreams>
</Simulation>
7 changes: 7 additions & 0 deletions tests/framework/ROM/tests
Original file line number Diff line number Diff line change
Expand Up @@ -24,4 +24,11 @@
prereq = timeDepGaussPoly
[../]

[./SKL_time]
# NOTE when in conflict with dataobject-rework branch, accept ALL dataobject-rework instead and delete this comment.
type = 'RavenFramework'
input = 'skl_timedep.xml'
csv = 'TimeDepSKL/innerHS_0.csv TimeDepSKL/innerHS_1.csv TimeDepSKL/innerHS_2.csv'
[../]

[]