Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.
Originally posted by: feanor0I'm trying to get to grips with the Python node to use the numpy package for stats. I define a Python node with three parameters 'Mu', 'Sigma', and 'NumSamples'. The code I put into the node is below.
Questions:
1. Is there a way to create NumSamples output records and write them out once, rather than within the for-loop as below?
2. Does the method below violate the pump philosophy of only doing a small bit of work? For example, I might ask for 100,000 samples to be created.
3. Where can I see the definition and methods for the BrainNodeClass?
Any suggestions for improvement are welcome!
import braininfo
import numpy as np
PROPERTY_BASE = "ls.brain.node.normDist"
def setup(brainNodeControlObj, BrainNodeClass):
''' Must return a class that inherits from BrainNodeClass. '''
class BrainNode(BrainNodeClass):
def initialize(self):
''' Called at node initialization, before first pump.'''
super(BrainNode, self).initialize()
self.logMedium("Initializing the NormDist node")
self.mu = self.properties.getString(PROPERTY_BASE + ".Mu")
self.sigma = self.properties.getString(PROPERTY_BASE + ".Sigma")
self.numSamples = self.properties.getString(PROPERTY_BASE + ".NumSamples")
m = self.newMetadata()
m.append("NormDist", "double")
self.outputs[0].metadata = m
def finalize(self, val):
''' Called at node end, after last pump call. '''
super(BrainNode, self).finalize(val)
def pump(self, quant):
''' Will continue to be called until False is returned.
Only a small amount of work should be done in pump without checking
quant.permitsRunning. When that returns False, return from pump
with a True value to be called again, a False value when finished.'''
while quant.permitsRunning(self):
rec = self.inputs[0].read()
if not rec:
return False
# your code here
randnorm = np.random.normal(rec[self.mu], rec[self.sigma], rec[self.numSamples])
for k in range(rec[self.numSamples]):
outputRec = self.outputs[0].newRecord()
outputRec[0] = randnorm[k]
self.outputs[0].write(outputRec)
# when complete
return False
# finished your quantum, you want pump called again
return True
# If you implement your logic here, you don't need code below. If you want
# to inherit, and add more logic at next level, uncomment below code
# py3File = brainNodeControlObj.properties.getString("ls.brain .node.BrainPython.python3ImplementationFile")
# braininfo.loadModule(py3File, "py3", brainNodeControlObj)
# import py3
# BrainNodeImpl = py3.setup(brainNodeControlObj, BrainNode)
# return BrainNodeImpl
return BrainNode