Monday, September 12, 2016

Using plt.imshow to label timeseries data with matplotlib

Recently I've been trying to find the most effective way to color-code time series data using matplotlib.

For instance, if I have a raster plot like this:
...and I want to label it like this:
How?

After lots of failed attempts involving horrific for loops and ax.fill_between, ax.fill, and several other inferior solutions, I found a quick and easy fix:

After plotting your time series raster plot,

    Given: 
      - an array colors in (R, G, B) format (same length as your time series data)
      - nr_neurons is the number of axes (in this case, 4)
      - vspace is a float representing the space between each time axis
      - A timeline variable, tspace (created with np.linspace)


Note the parameter aspect='auto'. This makes sure the image takes the shape of your existing axis.

Tuesday, August 23, 2016

QGIS Area of Availability Plugin

8/23/16

I'm working on a set of QGIS plugins, and one that I think should be out there for grabs is an area of availability calculator. (EDIT: you should be able to install the plugin via the public QGIS repository soon)

Given a start point and a maximum distance to travel, we want to find the boundary of accessible regions via a road network.

Boundary of area of availability, plotted as points


We build a road network graph and calculate the road distances using Dijkstra's algorithm. We can then find the points corresponding to the outer boundary of the area of availability.

The code for this calculation is below (taken from this page):

from PyQt4.QtCore import *
from PyQt4.QtGui import *

from qgis.core import *
from qgis.gui import *
from qgis.networkanalysis import *

vl = qgis.utils.iface.mapCanvas().currentLayer()
director = QgsLineVectorLayerDirector(vl, -1, '', '', '', 3)
properter = QgsDistanceArcProperter()
director.addProperter(properter)
crs = qgis.utils.iface.mapCanvas().mapRenderer().destinationCrs()
builder = QgsGraphBuilder(crs)

pStart = QgsPoint(65.5462, 57.1509)
delta = qgis.utils.iface.mapCanvas().getCoordinateTransform().mapUnitsPerPixel() * 1

rb = QgsRubberBand(qgis.utils.iface.mapCanvas(), True)
rb.setColor(Qt.green)
rb.addPoint(QgsPoint(pStart.x() - delta, pStart.y() - delta))
rb.addPoint(QgsPoint(pStart.x() + delta, pStart.y() - delta))
rb.addPoint(QgsPoint(pStart.x() + delta, pStart.y() + delta))
rb.addPoint(QgsPoint(pStart.x() - delta, pStart.y() + delta))

tiedPoints = director.makeGraph(builder, [pStart])
graph = builder.graph()
tStart = tiedPoints[0]

idStart = graph.findVertex(tStart)

(tree, cost) = QgsGraphAnalyzer.dijkstra(graph, idStart, 0)

upperBound = []
r = 2000.0
i = 0
while i < len(cost):
  if cost[i] > r and tree[i] != -1:
    outVertexId = graph.arc(tree [i]).outVertex()
    if cost[outVertexId] < r:
      upperBound.append(i)
  i = i + 1

for i in upperBound:
  centerPoint = graph.vertex(i).point()
  rb = QgsRubberBand(qgis.utils.iface.mapCanvas(), True)
  rb.setColor(Qt.red)
  rb.addPoint(QgsPoint(centerPoint.x() - delta, centerPoint.y() - delta))
  rb.addPoint(QgsPoint(centerPoint.x() + delta, centerPoint.y() - delta))
  rb.addPoint(QgsPoint(centerPoint.x() + delta, centerPoint.y() + delta))
  rb.addPoint(QgsPoint(centerPoint.x() - delta, centerPoint.y() + delta))

I'm not quite finished with my plugin implementation, but you can check out the source code on my GitHub.

If you want to try your hand at developing a QGIS plugin, I strongly recommend starting with Plugin Builder.

Sunday, July 31, 2016

7/31/16, Current Work
  Since December 2015, I've been collaborating on a research publication. The publication will be centered on a Python software package called FreqPy.
  FreqPy is a tool designed to assist with qualitative analysis of neural firing rate data and to bolster arguments for neural population coding in subpopulations of neurons.
  We're currently in the verification/justification stages; I synthesized some sine waves as test data to verify that our methodology is working and just finished creating a set of figures.
  Check out the video below: