Issue
How do I shy away from using self, init, try and except. To give some background, this is code for a live biosensor stream. However, I am struggling to dumb the code down. The code constantly updates and then the code is plotted in matplotlib.
I want to get rid of all these methods because the logic and code is hard to work with.
MAIN QUESTION: What is the process of this code? What si the order fo the functions?
Solution
While I personally recommend classes as they will make it very easy to store data and transfer it from one function to another, it is cetainly possible to re-write the code outside of a class. You would need to remove the contents of the__init__
function and move them to the main indentation level, remove "self." from wherever you see it, and input the necessary variables to each function instead of self, i.e. update_vars(self)
would become def updateVars(board, display_window, board_channel)
Instead of doing this though, I recommend trying to learn how classes work. To start, when a class is initialized, in this case with stream = CytonStream()
the __init__
function is called to initialize an instance of the CythonStream object. Inside of the CytonStream methods, self
refers to this instance of the class. self
references inside the class are functionally the same as stream
references outside of the class because if you were to call stream.updateVars()
, stream
will get passed into updateVars
in the same way as if you called CytonStream.updateVars(stream)
.
Inside __init__
you will notice that attributes are assigned to the class instance self
:
self.display_window = DataFilter.get_nearest_power_of_two(500)
This variable now is accessible anywhere that you can access self
or stream
. So in updateVars
you can access this attribute.
all_data = self.board.get_current_board_data(self.display_window)
And if you were to do stream.display_window
it would give you this same attribute.
As far as what is happening in your code, when you run stream = CytonStream()
, this calls CytonStream.__init__()
. Inside this function,
self.ani = matplotlib.animation.FuncAnimation(self.fig, self.updateFig,
interval=5, blit=True)
an animation is created which will call self.updateFig()
, which will call self.updateVars()
Answered By - Modularizer
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.