Tuesday 2 December 2014

HTML5 CSS Javascript

The browser in becoming increasingly important in computing. 

This seems to be inline with the rise of cloud based computing and storage. The fracturing of the smartphone market has also made HTML5 apps more attractive for phone application developers. As HTML5 apps can be be run in any modern browser and smartphones run those browsers, HTML5 apps are automatically supported on any smartphone OS.

Last year I played with the Bitalino board and was surprised by how responsive their HTML5 based application was with the Bitalino connected by Bluetooth. It looked good and the non-blocking nature of Javascript made the interface very responsive. Python in the back end to do the serial communication and hardware interfacing made for a slick and reliable experience.

With the arrival of the OpenBCI headset imminent, I'd like to know more about the possibilities of coding in the browser.

When I came to the conclusion that programming was a necessary part of neuroscience, I started looking for the perfect language. One that would do all the things I needed so I wouldn't waste time learning inferior languages. After a lot of reading and a little experience, I understand why more experienced programmers don't spend a lot of time answering that question. There is no one best language, they all have their tradeoffs.

Except for Matlab..... Matlab is just bad. ;-)

You will probably end up learning a few languages.

Please excuse my explanations of Javascript, I'm not experienced with it and my knowledge is growing all the time. I'll reread this in a few months and see if I can make it clearer and fix up any mistakes.

Javascript has emerged as the Queen of the browser, it has nothing to do with JAVA despite the name, and is probably the most widely used languages due to this. It is an event driven, asynchronous (or non-blocking) language, meaning that unlike other languages where each instruction is performed in turn in a single thread, Javascript has a system of requests and callbacks that allow other work to be done while waiting for a requested resource (typically disk or network resources).

When I want to learn a programming language, I look around for articles on the nature of the language. Not tutorials but discussions about what it looks, feels and smells like. I try to get a feel for the personality of the language so I have some framework to piece together the information I will acquire. Next I do some online tutorials involving guided coding in a browser. Something like codeacademy. or Didacto

UPDATE 1
I have been doing a lot of reading and video watching about web frameworks. One web page and video that really open my eyes up is Pixel Monkeys piece on lightweight web frameworks. In it Andrew Montalenti talks about the connection between HTML CSS Javascript and light web frameworks like Flask. Flask is a Python framework that does templating and handles connections. Along with these tools, integration with databases and a lot of nifty editors like Emmet are mentioned.

But first a little HTML. Why? Because we want to use Javascript in the browser, and HTML is the markup language that makes a place for the Javacript to live and display it's results.

This material is taken from the codeacademy site.

the basic syntax of HTML is

< something >  ....... < something />

With the somethings (tags) defining what goes between these markers (thus the term - markup language).

It can be headers (h1 - h6)

<h2> This is a heading </ h2>

Or what we're interested in, a javascript file

<script src="js/all.min.js"></script>
 
 
 
<p> Text can go between paragraph tags </p> 
 
In the opening tag we can also state some attributes. 
In the next line, the 'a' tag denotes links and 
the 'href' is an attribute.
In this case it is a web address and notice it is in quotation marks. 
The text between the tags 'links' will be what the user sees and clicks on.

<a href='http://www.codecademy.com'> links </a>
 
images are also bracketed by tags and look a lot like the javascript markup.
Images have an attribute 'source' which gives the path to an image file. 
 
 
<img source="picture.png"></img>
 
 

CSS

The styling of the different elements of the html page are defined by the CSS file.

h1{
color:red;

The 'h1' part is a selector, the 'color' part is a property and the 'red' is it's value. A property:value pair is called a CSS rule.
 
 


 
 

Using LaTeX

To get started with LaTeX you first need to install it. Then an IDE is a good idea. It is possible to write raw tex but there are several great packages that make the task a lot easier. I'm going to use texmaker on the mac and this program works on Apple, Microsoft and Linux operating systems. It is also free and open source, of course.

To start a LaTeX document with texmaker, we can use the 'Quick Start' wizard and put in author and title as well as choose some other options. These chosen options end up in the preamble of the created tex document. As most of my writting involves referencing, I usually start up a bibtex file at the same time. The \use package{natbib} goes in the preamble and \bibliographystyle{humannat} goes before the \bibliography{path_to_bib}, which goes at the end before \end{document}


\documentclass[12pt,a4paper]{article}
\usepackage{natbib}


\author{Alistair Walsh}
\title{Neurofeedback Non-Learners and Brain Computer Interface Illiteracy. 
Epidemiology, identifying features and differentiation.}

\begin{document}
 
\maketitle
 
\begin{abstract}

Not everyone who is provided training in neurofeedback can learn cortical control 
nor that tries to operate a brain computer interface is able to. 
This inability is refered to in NFB as NFB non-learning and in BCI as BCI-illiteracy. 
Very little has been written on this area yet it affects an estimated 30\% to 50\
of participants. It has been suggested that various attributes can be used to identify 
NFB-nonlearning and BCI-illiteracy. These include  high hand dexterity, 
external locus of control, high hypnotisability, high disassociative index, 
and strong reward response. Discovering the  reason for some not learning 
cortical control would be of benefit to those receiving treatment and would 
inform the field on the mechanism behind cortical control. It would also possibly 
improve the techniques of teaching it.
 
\end{abstract}

\section{Introduction}


\citet{Grubler:2014aa} in a paper involving both patients and 
clinicians, the possible ethical concerns were raised.

\citet{Suk:2014aa} estimated the incidence of BCI-illiteracy at around 20\%

\citet{Ahn:2013aa} in a large study of 52 people, suggested that high theta and 
low alpha might predict BCI-I


\bibliographystyle{humannat}
\bibliography{/Users/Wintermute/Dropbox/research_2015/
litreview_BCI-illiteracy/BCI-illiteracy}

\end{document}
 
Results in the following Document 
 
 














 
 
 

Sunday 9 November 2014

Full Stack STEM

Working on lesson plans for the 'Scientific tool chain' . https://github.com/alistairwalsh/neuralcode/wiki

A dialog about neurology

On July 2nd 2014, Rohan posted on the BCI2000 mailing list. Since then we've been corresponding about BCI and EEG. I've posted the conversation here and I hope this will help out those who are new to EEG and prompt discussion. Please post comments if anything isn't clear or you disagree with any of my answers.

Hi everyone, 

 I am a 15 year old boy very passionate in the field of artificial intelligence and brain computer interface. I want to design a setup whereby I can detect brain waves with eeg and use them to control devices on the basis of amplitude. How should I go about it.

Thank You.

Regards,

Rohan

Hi Rohan,

You'll need access to an EEG amplifier and electrode cap. OpenBCI are almost ready with their project and I think that will be the cheapest way to buy your own. The other option is belong to a university that has this equipment.
Problem with the university is they may make you do years of study before they let you play with an EEG setup. I was in 4th year before I was allowed access to an EEG setup on a regular basis to work out BCI. The good side is they will teach you the theory about electrical brain activity and how EEG works, as well as the theory behind the relationship between this activity and what we call thought and behavior.

After you have the hardware, there is a choice of software. BCI2000 is great. After trying out a few others I stuck with BCI2000 to build a BCI based on the Mu rhythm.  If you know C++ or can learn it, it will help when you want to do things with BCI2000. There is also the option of using the BCPY2000 project which uses Python.
I'd suggest learning Python through the many excellent online courses (Codeacademy's python classes and Coursera's "Programming for Everyone"). Python is a great language to start with, you may stick with it as you learn BCI or you may move on to C like languages once you understand programming concepts. It also gives you an alternative to MATLAB when you want to analyse EEG data. MATLAB is expensive and that's fine if the university you belong to buys a license that everyone can use, otherwise open source is the way to go.

Using amplitude is a great way to drive a BCI, I'd suggest you add these details to your technique.

"Amplitude, in a particular location (i.e. over the motor cortex, electrode locations C3 and C4) and in a particular frequency band (i.e. reduction in the Alpha band and increase in the Beta band)". 

This activity is produced in most people when they move their right hand (C3) or their left hand (C4) or even when they think about moving them. So it's great for moving a cursor left and right across a screen.

Knowing the terminology like 'motor cortex' and the names of the electrode locations 'C3 and C4' as well as the names for the different frequency bands is important because that's one way you can convince others you know what you are talking about. Think of it like a secret language you must learn to be granted access to the magical cave of the EEG amplifier! Just a different version of "Open Sesame!". You'll find lots of the information on Wikipedia.

BCI2000 has a great tutorial on exactly how to do Mu rhythm BCI (I think this is what you want to do) so maybe you can start there.

Good Luck!

Hello sir, 

I had a question related to arduino and atmega ...... If an arduino gets an input of frequencies below 20 hz can it be programmed that if there is an input of frequencies then a light or led will turn on and if not the led will remain off???

Regards

Rohan

Dear Rohan

Two ways to do this as I see it. The first would require FFT calculations (if I am understanding your question correctly) and the humble ATmega328 is not really powerful enough to do this in real-time. The teensie 3.0 https://www.pjrc.com/store/teensy31.html with a 32 bit ARM processor is though. It can be programmed using the Arduino IDE. https://learn.adafruit.com/fft-fun-with-fourier-transforms/hardware has a tutorial for doing FFT on a signal with the teensie. It uses the  CMSIS DSP math library for the teensie. This tutorial also happens to have LEDs lighting up at different frequencies, I hope this helps.
The other way would be to create a low pass, or band pass filter in hardware, and detect a certain amplitude of signal after the filter.
Inline image 1
This is a basic Butterworth filter from http://www.corollarytheorems.com/Design/filter.htm. The choice of resistor and capacitor values will result in different cutoff frequencies.  

I believe it is possible to use the simulink package for MATLAB to design a filter in software and transfer it to the Arduino but I have no experience doing this.

Cheers

Hello sir, 

I am currently using a 9-12 hz band pass filter to allow only this specific range of frequency to pass through.I have attached a schematic picture of the circuit can you help me identify that whether the circuit is correct . I Have also used an instrumentation amplifier ad620 to amplify the brain waves. Can You please help me??

Thank you.

 

Displaying 20140718_100144.jpg


hello sir, 

 I wanted to develop an EEG circuit , could you please help me or get me in contact with some expert who could help me...

Thank you..

 

hello Ma'am

There is a great news, I have completed the project and I am able to control a led using brain waves. This wouldn't have been possible without your help, I am certainly grateful to you. Thank you once again. I hope you don't mind if I remain in touch with you and if I have further questions in the near future....

Thank you.

Excellent news Rohan,
Please keep in touch and I hope I can be of assistance in the future.

Hello sir ,

 I came across this article on PubMed relating brain interfacing and they stated that they could control a rats tail and even humans by brain to brain interaction. It is not explained deeply in the article. Could you please enlighten me on how exactly are the brain waves of humans and rats interacted.

thank you.

Rohan 


Hi Rohan,
I can't see an article. It may not have been attached or the mailing list might have removed it. If you can give me the reference it I could look it up.

sir ,

 here are the links

 I am unclear with the concept that they have used to transmit signals, could you please help me out.

thank you.

The recording is being done with EEG and the triggering of the second persons reaction is being done with Trans-cranial Magnetic Stimulation (TMS). TMS creates a magnetic pulse which causes the neurons within its magnetic field to fire. You can see in the first article, the man wearing the purple cap, has the TMS 'coil' over his left motor cortex. The left motor cortex controls the right side of the body. When the TMS pulses (when it receives the signal from the EEG connected to the other participant) it briefly creates a strong field over the motor cortex and causes the neurons to fire, which causes movement of his hand.  

The second article talks about using ultrasound which I haven't come across before. The principle is the same, it is a method of causing the neurons in the motor cortex that are in control of the tail to fire.

Sir,

 

 generally there are four different kind of brain waves - alpha, beta, delta and theta all varying from 0-30 Hz. But now it seems that more importance is being laid to the High Gamma waves having a frequency from 70-200 Hz. Why is it so? Why are gamma waves of importance in The research of Fmri and Electrocorticography and EEG?

Gamma is thought to have the role of collecting or combining the information from the various specialised parts of the brain. It is also extremely difficult to record and is usually contaminated by muscle activity ( as measured by an EMG). The sheets of muscle that lie on the skull, produce high frequency noise even when a participant is completely still. This was examined by a group of researchers who recorded EEG while under general anesthetic. Their findings suggest that a lot of the Gamma that is recorded by EEG is actually tonic muscle activity rather than brain activity. That is not to say gamma EEG doesn't exist, it just points out how hard it is to record without resorting to intra-cranial recording. Gamma is very interesting because of its connection with higher thought ( for want of a better word) but as always, the brain acts as a unified whole ( like an orchestra ) . It is interesting to look at individual elements but the meaning is in how it relates to the whole.
Good question! Thanks.

Sir,
Usually BCI work on the principle of brain waves say for example the alpha waves or the MU rythm for controlling devices based on these waves. But is there actually a way of differentiating between thoughts and putting them into actions? The BCI i have made is an eeg based Bci working on the principle of alpha waves measures over the occipital lobe but can i actually differentiate between thoughts. 

Hello sir, 

  I had made an EEG based Bci working on the principle of the alpha waves measured over the regions of o1 and o2 . I could switch on and off a led using the alpha waves whose amplitude is controlled by eye blink which is measured over the occipital lobe. How do I make further advancements and something more complicated, efficient. or something based on p300 erp.

Thank you

Rohan

 Dear Rohan,

My apologies for taking so long to reply. Your question is a good one and I've been thinking a lot about how to answer. Your question about identifying individual thoughts is something that many people in neuroscience are trying to answer. While it is true to say that there is a connection between brain activity and thought, they do not seem to be the same thing. There are also different types of thoughts from direct action based thoughts about the physical world like thinking about clenching my hand to abstract thoughts about constructs that don't really exist like how I feel about the Wizard of Oz story.

There are also different ways to measure and relate brain activity. From location to frequency and time relationships. The areas of the brain that are associated with hand movements are located in a well defined area of the cortex. Sensors over that area will show activity when the area is activated and no activity otherwise. 
Thinking about a movie or reflecting on my feelings about it wouldn't be so neatly confined to a small area. It would involve many areas of the brain interacting, including brain regions deeper than the cortex  when I involve memory and emotion.

The event related potentials are distinctive brain activity in response to a particular stimuli. The P300 is a distinctive positive going wave at approximately 300ms after identifying and storing in memory an distinctive event. The 'oddball' paradigm is something that reliably produces this response and consists of a high tone (the oddball) amongst more common low tones. The low tones are more common, so the high tone is unusual when it occurs. If the participant is asked to count the number of oddballs, the P300 is produced as the identify and update their memory. So this is one way to identify a particular thought type. 

The Alpha wave can be thought of as a carrier wave. When the brain is doing active processing, the alpha wave decreases and the other frequencies are higher in amplitude. When that task is done and the participant relaxes and does no processing, the Alpha wave will return again. Over the occipital cortex (O1 and O2) the Alpha wave is quite prominent when the participant closes their eyes. The theory is that the occipital lobe has no input from the eyes and therefore has little to do so the Alpha wave dominates.
So a BCI based on Alpha over the occipital cortex is differentiating activity from no activity.
How is your BCI setup? What determines if the LED is lit or not? 
Sincerely,
Alistair



Thursday 23 October 2014

Wednesday 15 October 2014

Programming for Scientists

I'm assuming that everyone has come to the conclusion now that programming is a necessary part of any scientific pursuit. Computers are an integral part of how we all gather, analyse and present our data. There are also techniques of code abstraction, version control and code testing that facilitate writing error free code that is easy to maintain and over time becomes a resource. These techniques are slowly making their way from the computer science field to the behavioral sciences. Unfortunately it has taken some high profile retractions of journal articles to push this issue forward.

As those I have been studying with have entered their PhD phase, there is a growing realisation that learning to program is essential. Along with that realisation I am also hearing a common statement. Most people assume that somewhere along the line, they would be sat down and shown how to program, that there would be some course to teach them what they need to know about programming. At least enough to analyse fMRI, MEG or EEG data. Most are shocked when they are handed a data set and told to 'now go analyse it'.

I went looking for teaching materials to meet this need and help my friends out. We talked a lot about what was necessary and arrived at a few realisations.

1. You learn best when the problem you are trying to solve is your own.
2. Everyone is too busy to learn programming right now but they will be busier any time in the future.
3. The easiest way to get someone into programming is to teach them something that will benefit them right now.
4. Teaching what you have learned to someone is of huge benefit and therefore absolute beginners who can be taught by those who are a little more advanced are a valuable resource for any learning group.
5. There needs to be a certain level of uncertainty, it greatly improves the internalisation of the material once the uncertainty is removed through experimentation. We are mostly scientists and the process of hypothesizing and testing is a skill we can use here too.  

The Facebook page gets a lot of views and a good amount of 'likes'. There are three or four people who turn up on a regular basis and are making great progress. The big struggle now is to get more people to actually turn up and do some coding.

The weekly sessions have taken on a definite style and are challenging but really enjoyable. The general format is to propose a common task and work slowly through the steps needed to solve this problem. The main feature is to pause at the start of each step and have everyone talk about their expectations. Having people talk about possible strategies and how they might use ideas from other solutions helps understand where everyone is in their thinking. It usually clears up misconceptions before they become too confusing.

Learning these techniques is very similar to learning a musical instrument. You can study the subject as much as you like but  you won't really improve until you practice. Trying things out and making mistakes is much more valuable that having the final answer presented to you with all the anguish removed.

Serious writers write, inspired or not

I have many reasons for writing this blog. One of them is to practice writing.

As a scientist, I think my main task is to communicate. Usually as written reports but also at conferences and in classrooms. Although gathering and analysing data is a crucial part of scientific inquiry, it is a little bit pointless if I can't transmit what I learn to others. In an effort to be better at that, I take pointers from two sources.

The chain

 

This post about the Jerry Seinfeld's "Don't break the chain" method is why I put my writing online.
The main message is that if you want to be a great comic, you need great jokes. To have great jokes you need to write lots of jokes to get better at it and to be able to pick the better jokes from the ok jokes. To write lots of jokes, you need to write often - this is where the chain comes in. If you have a wall calendar and make a big cross on every day you write, the crosses form a pattern. After you write on a few consecutive days, you want to keep writing so you 'Don't break the chain'. It's a way to consciously create a habit.

This blog is a public place where it is obvious if I'm regularly putting up content, it's obvious how productive I'm being. I have to write complete post, not just 'to do' lists. I have to write for someone else, not just notes for myself. This means I need to express ideas clearly and fully.

In the same vein, the  NeuralCode  meetings mean I must research and prepare material on scientific computing every week. I also post related articles to the feed as a way to engage the members of the group and send support material to the email list. It's a way of getting to where I want to be.

I hope to be someone who gives lectures and tutorials on scientific computing. I am really passionate about modernising research and the practice of reproducible research. To one day be that person I need to have a body of work that shows I know what I'm talking about and can express that well. I also need to practice expressing those ideas. Because just like writing a joke, even if the original idea is great, if it practiced and honed it can be taken to a whole new level of great.

Scientific writing

A main theme is to simplify your expression and to follow the simple rules of good writing. The purpose of scientific writing is to communicate clearly. Einstein is reported to have said that if your science is any good, you should be able to explain it to a child. Part of this is using the clearest expression that conveys precisely what you mean. Using a complex and obscure word where a simple and common word would have the same meaning is something that creeps into our writing. It sounds more scientific. We read it in the papers of senior researchers. Knowing what big words mean gives us a sense of actually knowing something in a field where we are usually in a state of having no idea what's going on. So we hide behind the big words and cling to them like mantras so we don't feel like such fakes.

fa5.jpg

Unfortunately this makes understanding some literature harder than it needs to be. Obviously there is a need for very specific terminology at times, terms that may be very specialised and require the reader to search out the meaning if they haven't come across them before. There is a balance that needs to be found.

A great bit from the scientific writing course:

- from William Zinsser's book On Writing Well, which is a great book to pick up to read for this course if you have time.
He says,
"The secret of good writing is to strip every sentence to it's cleanest components.
Every word that serves no function, every long word that could be a short word,
every adverb that carries the same meaning that's already in the verb, every passive
construction that leaves the reader unsure of who is doing what.
These are the thousand and one adulterants that weaken the strength of a sentence.
And they usually occur in proportion to the education and rank (of the writer)."




Wednesday 1 October 2014

Psychopy in the IPython shell

Since finding Psychopy I've wanted the convenience of the IPython shell when writing scripts. The editor in Psychopy is OK but doesn't have all the nice features of IPython.

I had hoped that there might be a package in the conda environment but no such luck. The solution turned out to be Binstar.
I had been curious about Binstar since it started popping up as a suggestion whenever I tried to install something with conda and the package couldn't be found. It is basically user created packages and there was a package for Psychopy from Eric Kastman. I created a new conda environment with the command -
>> conda create -n psyk anaconda

I then activated this environment and installed Psychopy with Eric's package.

>> conda install -c https://conda.binstar.org/erik psychopy


I copied some tutorial code from  Psych711 created by Prof. Gary Lupya and ran it. 

There is a problem when I try to stop the Psychopy window but other than that 
it runs great!