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

Mandd/fixes uniform discrete #2219

Open
wants to merge 4 commits into
base: devel
Choose a base branch
from
Open
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
31 changes: 12 additions & 19 deletions ravenframework/Distributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1920,11 +1920,11 @@ def initializeFromDict(self, inputDict):
@ In, inputDict, dict, dictionary containing the np.arrays for xAxis and pAxis
@ Out, None
"""
self.strategy = inputDict['strategy']
self.categoricalDist = Categorical()
self.categoricalDist.initializeFromDict(inputDict)
initialPerm = randomUtils.randomPermutation(inputDict['outcome'].tolist(),self)
self.pot = np.asarray(initialPerm)
self.strategy = inputDict['strategy']
self.lowerBound = inputDict['lowerBound']
self.upperBound = inputDict['upperBound']
self.nPoints = inputDict['nPoints']
self.initializeDistribution()

def pdf(self,x):
"""
Expand Down Expand Up @@ -1973,23 +1973,16 @@ def selectedRvs(self,discardedElems):
@ In, discardedElems, np array, list of discarded elements
@ Out, rvsValue, float, the random state
"""
if self.nPoints is None:
self.xArray = np.arange(self.lowerBound,self.upperBound+1)
else:
self.xArray = np.linspace(self.lowerBound,self.upperBound,self.nPoints)
xArray = np.setdiff1d(self.xArray,discardedElems)
pdfArray = 1/xArray.size * np.ones(xArray.size)

self.xArray = np.setdiff1d(self.xArray,discardedElems)

self.pdfArray = 1/self.xArray.size * np.ones(self.xArray.size)
paramsDict={}
paramsDict['outcome'] = self.xArray
paramsDict['state'] = self.pdfArray
paramsDict['strategy'] = self.strategy

self.tempUniformDiscrete = UniformDiscrete()
self.tempUniformDiscrete.initializeFromDict(paramsDict)
paramsDict['outcome'] = xArray
paramsDict['state'] = pdfArray
self.categoricalDist = Categorical()
self.categoricalDist.initializeFromDict(paramsDict)
rvsValue = self.categoricalDist.rvs()

rvsValue = self.tempUniformDiscrete.rvs()
return rvsValue

def reset(self):
Expand Down
2 changes: 1 addition & 1 deletion ravenframework/Models/PostProcessors/Factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,4 +65,4 @@
factory = EntityFactory('PostProcessorInterface', needsRunInfo=True)
factory.registerAllSubtypes(PostProcessorInterface, alias=renaming)

factory.registerType('External', ExternalPostProcessor)
factory.registerType('External', ExternalPostProcessor)
2 changes: 1 addition & 1 deletion ravenframework/OutStreams/PlotInterfaces/Factory.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,4 +34,4 @@
factory.registerType('OptPath', OptPath)
factory.registerType('SyntheticCloud', SyntheticCloud)
factory.registerType('PopulationPlot', PopulationPlot)
factory.registerType('OptParallelCoordinatePlot', OptParallelCoordinatePlot)
factory.registerType('OptParallelCoordinatePlot', OptParallelCoordinatePlot)
45 changes: 29 additions & 16 deletions tests/framework/unit_tests/Distributions/TestDistributions.py
Original file line number Diff line number Diff line change
Expand Up @@ -1219,18 +1219,26 @@ def getDistribution(xmlElement):

discardedElems = np.array([5,6])

checkAnswer("UniformDiscrete rvs1",UniformDiscrete.selectedRvs(discardedElems),3)
checkAnswer("UniformDiscrete rvs2",UniformDiscrete.selectedRvs(discardedElems),3)
checkAnswer("UniformDiscrete rvs3",UniformDiscrete.selectedRvs(discardedElems),3)
checkAnswer("UniformDiscrete rvs1",UniformDiscrete.selectedRvs(discardedElems),4)
checkAnswer("UniformDiscrete rvs2",UniformDiscrete.selectedRvs(discardedElems),4)
checkAnswer("UniformDiscrete rvs3",UniformDiscrete.selectedRvs(discardedElems),4)
checkAnswer("UniformDiscrete rvs4",UniformDiscrete.selectedRvs(discardedElems),4)
checkAnswer("UniformDiscrete rvs5",UniformDiscrete.selectedRvs(discardedElems),3)
checkAnswer("UniformDiscrete rvs6",UniformDiscrete.selectedRvs(discardedElems),3)
checkAnswer("UniformDiscrete rvs7",UniformDiscrete.selectedRvs(discardedElems),4)
checkAnswer("UniformDiscrete rvs5",UniformDiscrete.selectedRvs(discardedElems),4)
checkAnswer("UniformDiscrete rvs6",UniformDiscrete.selectedRvs(discardedElems),4)
checkAnswer("UniformDiscrete rvs7",UniformDiscrete.selectedRvs(discardedElems),3)
checkAnswer("UniformDiscrete rvs8",UniformDiscrete.selectedRvs(discardedElems),4)
checkAnswer("UniformDiscrete rvs9",UniformDiscrete.selectedRvs(discardedElems),4)
checkAnswer("UniformDiscrete rvs10",UniformDiscrete.selectedRvs(discardedElems),3)
checkAnswer("UniformDiscrete rvs11",UniformDiscrete.selectedRvs(discardedElems),4)
checkAnswer("UniformDiscrete rvs12",UniformDiscrete.selectedRvs(discardedElems),3)
checkAnswer("UniformDiscrete rvs12",UniformDiscrete.selectedRvs(discardedElems),4)
checkAnswer("UniformDiscrete rvs13",UniformDiscrete.selectedRvs(discardedElems),3)
checkAnswer("UniformDiscrete rvs14",UniformDiscrete.selectedRvs(discardedElems),4)
checkAnswer("UniformDiscrete rvs15",UniformDiscrete.selectedRvs(discardedElems),3)
checkAnswer("UniformDiscrete rvs16",UniformDiscrete.selectedRvs(discardedElems),3)
checkAnswer("UniformDiscrete rvs17",UniformDiscrete.selectedRvs(discardedElems),3)
checkAnswer("UniformDiscrete rvs18",UniformDiscrete.selectedRvs(discardedElems),4)
checkAnswer("UniformDiscrete rvs19",UniformDiscrete.selectedRvs(discardedElems),4)
checkAnswer("UniformDiscrete rvs20",UniformDiscrete.selectedRvs(discardedElems),4)

#Test UniformDiscrete not integer values
# >>> np.linspace(0,1,9)
Expand All @@ -1245,16 +1253,21 @@ def getDistribution(xmlElement):
UniformDiscrete2 = getDistribution(UniformDiscreteElement2)

checkAnswer("UniformDiscrete2 pdf(0.125)",UniformDiscrete2.pdf(0.125),0.11111111111111111111111)
checkAnswer("UniformDiscrete2 rvs1",UniformDiscrete2.rvs(),0.125)
checkAnswer("UniformDiscrete2 rvs2",UniformDiscrete2.rvs(),0.25)
checkAnswer("UniformDiscrete2 rvs3",UniformDiscrete2.rvs(),0.25)
checkAnswer("UniformDiscrete2 rvs4",UniformDiscrete2.rvs(),0.875)
checkAnswer("UniformDiscrete2 rvs5",UniformDiscrete2.rvs(),0.875)
checkAnswer("UniformDiscrete2 rvs6",UniformDiscrete2.rvs(),1.0)
checkAnswer("UniformDiscrete2 rvs3",UniformDiscrete2.rvs(),0.25)
checkAnswer("UniformDiscrete2 rvs4",UniformDiscrete2.rvs(),0.25)
checkAnswer("UniformDiscrete2 rvs5",UniformDiscrete2.rvs(),0.875)
checkAnswer("UniformDiscrete2 rvs1",UniformDiscrete2.rvs(),0.5)
checkAnswer("UniformDiscrete2 rvs2",UniformDiscrete2.rvs(),0.125)
checkAnswer("UniformDiscrete2 rvs3",UniformDiscrete2.rvs(),0.125)
checkAnswer("UniformDiscrete2 rvs4",UniformDiscrete2.rvs(),0.75)
checkAnswer("UniformDiscrete2 rvs5",UniformDiscrete2.rvs(),0.125)
checkAnswer("UniformDiscrete2 rvs6",UniformDiscrete2.rvs(),0.25)
checkAnswer("UniformDiscrete2 rvs7",UniformDiscrete2.rvs(),0.25)
checkAnswer("UniformDiscrete2 rvs8",UniformDiscrete2.rvs(),0.875)
checkAnswer("UniformDiscrete2 rvs9",UniformDiscrete2.rvs(),0.875)
checkAnswer("UniformDiscrete2 rvs10",UniformDiscrete2.rvs(),1.0)

UniformDiscreteDict = {'strategy': "withReplacement",'lowerBound': 0, 'upperBound': 1, 'nPoints': 9}
UniformDiscrete3 = Distributions.factory.returnInstance("UniformDiscrete")
UniformDiscrete3.initializeFromDict(UniformDiscreteDict)
checkAnswer("UniformDiscrete3 pdf(0.125)",UniformDiscrete3.pdf(0.125),0.11111111111111111111111)

print(results)

Expand Down