Skip to content

WIP: Realtime - #382

Closed
mluessi wants to merge 14 commits into
mne-tools:masterfrom
mluessi:realtime
Closed

mluessi wants to merge 14 commits into
mne-tools:masterfrom
mluessi:realtime

Conversation

@mluessi

@mluessi mluessi commented Jan 14, 2013

Copy link
Copy Markdown
Contributor

This is a rework of #213. My idea is to have a real time epochs class RtEpochs that behaves similarly to Epochs, but the data is received continuously from an mne_rt_server via an RtClient instance which pushes the received raw buffers to the RtEpochs instance(s). You can e.g. do

import mne
from mne.realtime import RtClient, RtEpochs
client = RtClient('localhost')
rt_epochs = RtEpochs(client, 1, -0.2, 0.5, 5, reject=dict(eeg=80e-6, eog=150e-6))
rt_epochs.start()

while True:
    evoked = rt_epochs.average()
    mne.viz.plot_evoked(evoked)
    pl.show()
    time.sleep(0.5)

This code will receive 5 (good) epochs, compute an evoked response, and plot it. Then the next 5 epochs are received (5 because the n_epochs parameter in the constructor is 5). The idea is that it will be possible to pass an instance of RtEpochs to most functions that accept Epochs as input, e.g., apply_inverse_epochs, spectral_connectivity (for the latter, the n_epochs argument is useful, as only n_epochs will be used for the connectivity computation).

The code works but one problem is that RtEpochs is derived from Epochs, which has many methods which don't work when real time processing is used. Right now, those methods are disabled using an ugly hack. The proper solution would be create a new class BaseEpochs and derive both Epochs and RtEpochs from that class. Is anyone opposed to this solution?

@larsoner

Copy link
Copy Markdown
Member

So basically, we'd have BaseEpochs, and then Epochs would basically be the static / file I/O based version, while RtEpochs would be a stripped-down and/or specialized version for realtime processing? This seems reasonable to me.

It occurred to me that it would be useful for testing and such (for nosetests but also for people who will actually use the code ) to have a "simulation mode", where an epochs FIF file can be used to fake realtime inputs to RtEpochs, perhaps randomly providing epochs based on a inter-trial intervals being randomly distributed between receive_t_min and receive_tmax or some such. It would also make providing examples simpler. Not sure if you have something like this in mind (haven't looked at the code yet), but it could be useful...

@dengemann

Copy link
Copy Markdown
Member

Both proposals seem reasonable to me. BaseEpochs seems nicer than 'ugly hack'!

On 14.01.2013, at 22:14, Eric89GXL [email protected] wrote:

So basically, we'd have BaseEpochs, and then Epochs would basically be the static / file I/O based version, while RtEpochs would be a stripped-down and/or specialized version for realtime processing? This seems reasonable to me.

It occurred to me that it would be useful for testing and such (for nosetests but also for people who will actually use the code ) to have a "simulation mode", where an epochs FIF file can be used to fake realtime inputs to RtEpochs, perhaps randomly providing epochs based on a inter-trial intervals being randomly distributed between receive_t_min and receive_tmax or some such. It would also make providing examples simpler. Not sure if you have something like this in mind (haven't looked at the code yet), but it could be useful...

—
Reply to this email directly or view it on GitHub.

@mluessi

mluessi commented Jan 14, 2013

Copy link
Copy Markdown
Contributor Author

On 01/14/13 16:14, Eric89GXL wrote:

So basically, we'd have |BaseEpochs|, and then |Epochs| would basically
be the static / file I/O based version, while |RtEpochs| would be a
stripped-down and/or specialized version for realtime processing? This
seems reasonable to me.

Yes, that's the idea. It is not strictly necessary to have a common base
class (we could use duck typing), but this creates a lot of duplicate code.

It occurred to me that it would be useful for testing and such (for
nosetests but also for people who will actually use the code ) to have a
"simulation mode", where an epochs FIF file can be used to fake realtime
inputs to |RtEpochs|, perhaps randomly providing epochs based on a
inter-trial intervals being randomly distributed between |receive_t_min|
and |receive_tmax| or some such. It would also make providing examples
simpler. Not sure if you have something like this in mind (haven't
looked at the code yet), but it could be useful...

Yes, we will need a simulator for testing. Right now I use mne_rt_server
(part of mne-cpp), which has a fif file simulation mode. In the final
version, I will add a dummy simulation server for testing which behaves
like mne_rt_server (assuming this is not too difficult..).

@larsoner

Copy link
Copy Markdown
Member

Sounds good -- I'd much prefer one more layer of abstraction (BaseEpochs) over having to duplicate code or "hacking" functions to do nothing. If every time I changed a function in normal Epochs I'd have to tinker with your RtEpochs object it would likely make neither me nor you happy :) Having to think about whether to put something in BaseEpochs or Epochs when I want to add or change something doesn't sound as bad...

@agramfort

Copy link
Copy Markdown
Member

+1 For a BaseEpochs object.

I would call RtEpochs just Epochs as the import name makes it obvious
as it's a realtime object. We don't need to repeat ourselves

@mluessi

mluessi commented Jan 15, 2013

Copy link
Copy Markdown
Contributor Author

I don't think renaming RtEpochs to Epochs is a good idea. I know it is redundant but don't you think it will be confusing to the user when there are two classes that have the same name but behave differently?

@agramfort

Copy link
Copy Markdown
Member

ok fair enough

@chdinh

chdinh commented Jan 16, 2013

Copy link
Copy Markdown
Member

@mluessi fixed the calibration issue: simulator isn't anymore de-calibrating before sending. Ouptut should be now the same as reading raw data directly.

@mluessi

mluessi commented Jan 16, 2013

Copy link
Copy Markdown
Contributor Author

@chdinh there was never a problem with the calibration (the strange output in #213 was due to a wrong reshape). Does the mne_rt_server apply the calibration when reading from the neuromag system, or is this done before by the "neuromag collector"? We should either always have calibrated data coming from the server or always assume the client applies the calibration.

@chdinh

chdinh commented Jan 17, 2013

Copy link
Copy Markdown
Member

Good point - right now calibration isn't applied to the neuromag data within the mne_rt_server. I'll change this, that it will be consistent with the fiff simulator.

@mluessi

mluessi commented Jan 17, 2013

Copy link
Copy Markdown
Contributor Author

Is it possible that we lose precision if we apply calibration but keep the data 32bit float? In mne-python we use 64 bit floats when we apply calibration, but if you do so and send the data over the network it would double the amount of data. So if precision is a problem we should apply the calibration at the client side.

@chdinh

chdinh commented Jan 17, 2013

Copy link
Copy Markdown
Member

Actually within mne-cpp internally everything is treaded as doubles. But for storing, data are casted back to float and so in the case of broadcasting data over TCP/IP. We can also broadcast data using double - we have enough resources to do that.

@mluessi

mluessi commented Jan 17, 2013

Copy link
Copy Markdown
Contributor Author

If 32 bit floats after calibration cause potential precision problems, I think we should do the calibration on the client side. While it is true that we right now have the resources to send doubles over the network, we can reduce the amount of data by a factor of two without any negative consequences. Only the calibration has to be done on the client side, but this is very easy to do (and computationally cheap).

Maybe @agramfort and @mshamalainen can weigh on this issue as well.

@agramfort

Copy link
Copy Markdown
Member

+1 for transfering float32 and do calibration on client side after a cast
in float64

@mluessi

mluessi commented Jan 17, 2013

Copy link
Copy Markdown
Contributor Author

I think ideally, the mne_rt_server should be able to receive both float32 and float64 data from the plugin and then transparently (=without doing any processing) forward the data as raw buffers to the client. For current systems, we use float32 and apply the calibration at the client side, but if for some reason the plugin or "collector program" wants to do processing and it is better to send float64 data, this is supported as well and doesn't require any changes to mne_rt_server. WDYT?

@agramfort

Copy link
Copy Markdown
Member

sounds reasonable to me

@mluessi

mluessi commented Jan 17, 2013

Copy link
Copy Markdown
Contributor Author

Added BaseEpochs which is the common base class for Epochs and RtEpochs. The CTF compensation is currently broken, see #395.

This will probably stay WIP for a while as we are currently implementing some of the communication with the mne_rt_server plugins using JSON, see mne-tools/mne-cpp#5.

@chdinh

chdinh commented Jan 18, 2013

Copy link
Copy Markdown
Member

Okay - we can transparently forward the data buffers. What we should keep in mind is: that mne_rt_server runs mostly on this powerfull Linux neuromag workstations by basically doing nothing computational intensive - so this neuromag workstation is kind of bored. Also mne_rt_server is highly threaded that we can do computational intensive parts in different parallel jobs. I see that you wanna have the full control about what's going on within the python side - but for stuff like calibration... WDYT?

@mluessi

mluessi commented Jan 18, 2013

Copy link
Copy Markdown
Contributor Author

I think we should keep mne_rt_server as transparent as possible. Given that applying the calibration on the client side is simple to do and computationally very cheap (just one multiplication per sample), it IMHO just does not make sense to increase the the code complexity on the server side and double the amount of data that needs to be transmitted by moving the calibration to the server.

What do you think about supporting both float32 and float64 raw buffers at the same time*? This will give people the freedom to do processing in the mne_rt_server plugin if they want to and the transmit the data as float64.

*Of course a plugin would either use float32 or float64, but the server wouldn't need to be recompiled.

@chdinh

chdinh commented Jan 18, 2013

Copy link
Copy Markdown
Member

okay than we should do that - but we still should have in mind that one of the fastest matrix multiplication algorithms (coppersmith) implementation still needs a computational effort of O(n^2.376) and even if that operation only needs 2% of the processor capabilities we have 2% less to do some fancy stuff. Especially when I think of compiling a real-time display for a tablet device... battery life and so on...

@mluessi

mluessi commented Apr 23, 2013

Copy link
Copy Markdown
Contributor Author

@mainakjas I rebased this old PR using the newest master. It is till WIP, i.e., unit tests need to be added and I have to double check that the changes don't break anything else that was recently introduced. Have a look at the example, let me know if you have comments / questions.

Comment thread mne/fiff/proj.py

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

When does this happen? Is this something that the RT server does? It would be better to update this comment if that's the case.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yeah.. I agree it's a dirty temporary fix. For some reason the data coming from the RT server is transposed. I will update the comment.

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oh let me know if there is something wired - I also had a transposing issue. Can be the fault of mne_rt_server.

@mainakjas

Copy link
Copy Markdown
Contributor

@mluessi Thanks a lot! Looking into it ... will let you know soon if/when
I've questions.

On Tue, Apr 23, 2013 at 10:07 PM, Martin Luessi [email protected]:

@mainakjas https://github.com/mainakjas I rebased this old PR using the
newest master. It is till WIP, i.e., unit tests need to be added and I have
to double check that the changes don't break anything else that was
recently introduced. Have a look at the example, let me know if you have
comments / questions.

—
Reply to this email directly or view it on GitHubhttps://github.com//pull/382#issuecomment-16889808
.

@chdinh

chdinh commented Apr 24, 2013

Copy link
Copy Markdown
Member

fixed it -> rows = length(names)

@mainakjas

Copy link
Copy Markdown
Contributor

@mluessi I get an error which goes something like this:

[1] run realtime/compute_rt_moving_average.py

========================================================
Compute real-time evoked responses using moving averages
========================================================

This example demonstrates how to connect to an MNE Real-time server
using the RtClient and use it together with RtEpochs to compute
evoked responses using moving averages.

Note: The MNE Real-time server (mne_rt_server), which is part of mne-cpp,
has to be running on the same computer.

Traceback (most recent call last):

File "/usr/local/lib/python2.7/dist-packages/ipython-0.13.2-py2.7.egg/IPython/utils/py3compat.py", line 178, in execfile
__builtin__.execfile(filename, *where)

File "/home/mainakjas/github/mne-python/examples/realtime/compute_rt_moving_average.py", line 24, in <module>
from mne.realtime import RtClient, RtEpochs

ImportError: No module named realtime

The realtime folder is present and other folders examples appear to be running right. make clean did not help either. Any idea what is going wrong?

@mluessi

mluessi commented Apr 26, 2013

Copy link
Copy Markdown
Contributor Author

@mainakjas the realtime module was missing from setup.py it should work now.

@mainakjas

Copy link
Copy Markdown
Contributor

@mluessi thanks! it works now (or at least it doesn't give any error) and I see that a plot is updated in real-time :-) Exploring further now ...

@mainakjas

Copy link
Copy Markdown
Contributor

another quick question @chdinh. I have to cd to mne-cpp/bin to run ./rt_server Is that normal? This is how my PATH variable looks:

mainakjas@Mainak:~/github/mne-cpp$ echo $PATH
/home/mainakjas/github/mne-cpp/bin:/opt/MNE-2.7.0-3106-Linux-i686/bin:/data/freesurfer/bin:/data/freesurfer/fsfast/bin:/data/freesurfer/tktools:/data/freesurfer/mni/bin:/opt/Qt/qt-creator-2.6.2-src/bin:/opt/Qt/Qt5.0.1/bin:/usr/lib/lightdm/lightdm:/usr/local/sbin:/usr/local/bin:/usr/sbin:/usr/bin:/sbin:/bin:/usr/games

So, it does have the mne-cpp/bin directory but when I run ./rt_server in any other directory, I get:

mainakjas@Mainak:~/github$ ./mne_rt_server
bash: ./mne_rt_server: No such file or directory

It's not a big thing as I can cd to the directory and run it, but still curious ...

@mluessi

mluessi commented Apr 26, 2013

Copy link
Copy Markdown
Contributor Author

@mainakjas glad it works now :). To run mne_rt_server from another directory you just type mne_rt_server (without the initial './`). I'm not sure if it works though, it may be looking for the sample data in the current directory and not in the directory where the rt server executable is stored..

@mainakjas

Copy link
Copy Markdown
Contributor

Oh, that was it! Yes, leaving out the ./ solved the problem.

@mainakjas

Copy link
Copy Markdown
Contributor

well, the sample data problem is there too -- leaving out the ./ starts the server alright but then I get:

Opening raw data ../../mne-cpp/bin/MNE-sample-data/MEG/sample/sample_audvis_raw.fif...
Cannot open ../../mne-cpp/bin/MNE-sample-data/MEG/sample/sample_audvis_raw.fif
Error: Not able to read raw info!
    send measurement info to FiffStreamClient (ID: 0)

We should probably specify the absolute path (by using the environment variable) rather than relative path: i.e. ../../mne-cpp/bin/MNE-sample-data/MEG/sample/sample_audvis_raw.fif should be replaced by $MNE_CPP/MNE-sample-data/MEG/sample/sample_audvis_raw.fif

@chdinh

chdinh commented Apr 26, 2013

Copy link
Copy Markdown
Member

thx good hint - I'll look into it. Meanwhile one have to run mne_rt_server from inside the bin directory - sorry for that inconvinience

@chdinh

chdinh commented Apr 26, 2013

Copy link
Copy Markdown
Member

@mainakjas

Copy link
Copy Markdown
Contributor

ok, I'll try to look at it over the weekend - I am not sure if the environment variables would work within C. Depends on how you have implemented it, but I don't mind prying into the code :-)

@larsoner

larsoner commented Jun 6, 2013

Copy link
Copy Markdown
Member

What is the relationship between this PR and the one @mainakjas has open? In other words, can we close this one in favor of his, or do they implement different functionality?

@larsoner

larsoner commented Jun 6, 2013

Copy link
Copy Markdown
Member

I just checked, and it looks like we should be able to just have @mainakjas's PR to work from. I'm going to close this to move discussion to that PR. @mluessi if you think that's counterproductive, feel free to reopen.

@larsoner larsoner closed this Jun 6, 2013
@mluessi

mluessi commented Jun 7, 2013

Copy link
Copy Markdown
Contributor Author

My idea was to merge this soon after the 0.6 release, after a bit more polishing and after the mock server and unit tests have been added. @mainakjas decoding PR (#382) can then be recreated on top of the new master. So I'm reopening it..

@mluessi mluessi reopened this Jun 7, 2013
@mainakjas

Copy link
Copy Markdown
Contributor

By the way, should I keep rebasing from master in the meanwhile? Or is
there a way to rebase from this PR? Or can I just let my branch remain as
it is and keep building on it (=no rebasing, this is what I'm doing as of
now). What's the best strategy?

On Fri, Jun 7, 2013 at 4:29 PM, Martin Luessi [email protected]:

My idea was to merge this soon after the 0.6 release, after a bit more
polishing and after the mock server and unit tests have been added.
@mainakjas https://github.com/mainakjas decoding PR (#382#382)
can then be recreated on top of the new master. So I'm reopening it..

—
Reply to this email directly or view it on GitHubhttps://github.com//pull/382#issuecomment-19106735
.

@mluessi

mluessi commented Jun 7, 2013

Copy link
Copy Markdown
Contributor Author

I think for now it is best to just keep working on your branch, no rebasing required. Once this PR has been merged, you can make a new decoding branch that just includes your commits.

@mainakjas

Copy link
Copy Markdown
Contributor

ok good, that's simple. Thanks!

On Fri, Jun 7, 2013 at 6:09 PM, Martin Luessi [email protected]:

I think for now it is best to just keep working on your branch, no
rebasing required. Once this PR has been merged, you can make a new
decoding branch that just includes your commits.

—
Reply to this email directly or view it on GitHubhttps://github.com//pull/382#issuecomment-19112812
.

@agramfort

Copy link
Copy Markdown
Member

closing this as it's now in #615

@agramfort agramfort closed this Jul 9, 2013
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

Projects

None yet

Development

Successfully merging this pull request may close these issues.

6 participants