LAE

Welcome to the LAE community!  Please feel free to start a discussion in the discussion tab or join in a conversation.

Discussions

Members

Resources

Events

 View Only
  • 1.  Python node and stats

    Employee
    Posted 02-03-2014 10:16

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: feanor0

    I'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


  • 2.  RE: Python node and stats

    Employee
    Posted 02-04-2014 16:14

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: ejones

    I'm glad to see that you are using LAE for this type of thing. This is one reason LAE now integrates with R.

    Is the code working? Because just glancing through it, it looks like it might work.

    You need not worry about the about the "small amount of work" for this. With around 100K records you'll be fine. If you did find a convenient way to return with a value of True the method will be called again, then you probably should. But based on what I'm seeing with your code it would be much more trouble than it is worth to add it.

    As of LAE 4.6.1 there is a new document describing how to create Python nodes. I believe it should help with your question about the BrainNodeClass. I have it in the directory named "C:\Program Files (x86)\Lavastorm\LAE 4.6\docs"


  • 3.  RE: Python node and stats

    Employee
    Posted 02-05-2014 02:24

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: feanor0

    Yes, the code works, but only if there's one row of data input:

    Mu Sigma NumSamples
    0.1 0.3 100000

    For multiple rows, I've had to use an allDone boolean value that is set to False within the for-loop, and True at the top of the while loop.

    I thought it might be more efficient to write out all the computed records in one shot, rather than having to grab a record from the output and writing record-by-record at each iteration of the for-loop. For 100,000 samples, the node takes a bit more than 2 seconds.

    Thanks for the pointer to the documentation. I'll take a look.


  • 4.  RE: Python node and stats

    Employee
    Posted 02-05-2014 09:32

    Note: This was originally posted by an inactive account. Content was preserved by moving under an admin account.

    Originally posted by: ejones

    2 seconds in my opinion is great.

    I haven't seen any documented reason for why we should worry about the "small amount of work". I've guessed that it has to do with needing to check periodically to see if the node run needs to be aborted. I've also seen Python code where it seems that no consideration for this has been taken and have not seen any ill effects from this.