%matplotlib inline

%%capture
!pip install https://github.com/apdavison/python-neo/archive/nwb.zip
!pip install igor

import requests
import random
import zipfile
import StringIO

import neo
print(neo.__version__)

"""
Explanation: Reading an IgorPro data file from the EPFL microcircuit database and saving in NWB format
Install required packages
We install a development branch of Neo with preliminary support for Neurodata Without Borders (NWB) format.
End of explanation
"""


bbp_uncurated_url = "http://microcircuits.epfl.ch/data/uncurated.json"

uncurated = requests.get(bbp_uncurated_url).json()

print("{}\n\n{}".format(uncurated['title'], uncurated['introduction']['text']))

items = uncurated['data_table']['table']['rows']

items[0]

"""
Explanation: Get list of uncurated datasets from the BBP Microcircuit database
End of explanation
"""


# we filter to try to ensure we get a dataset containing Igor Pro files
filtered_items = [item for item in items if "tkb" in item[0]]
folder = random.sample(filtered_items, 1)[0]
print(folder)

from HTMLParser import HTMLParser
class MyHTMLParser(HTMLParser):
    def handle_starttag(self, tag, attrs):
        if tag == "a":
            for attr in attrs:
                if attr[0] == "href":
                    self.url = attr[1]

def get_zipfile(data_item):
    parser = MyHTMLParser()
    parser.feed(data_item[0])
    data_file_url = parser.url
    f = StringIO.StringIO()
    response = requests.get("http://microcircuits.epfl.ch" + data_file_url)
    f.write(response.content)
    return zipfile.ZipFile(f)

archive = get_zipfile(folder)

len(archive.filelist)

"""
Explanation: Select a random dataset and download it into memory
End of explanation
"""


igor_file_path = random.sample([path for path in archive.namelist() if path.endswith(".ibw")], 1)[0]

igor_file_path

from neo.io.igorproio import IgorIO

local_path = archive.extract(igor_file_path)
print(local_path)

block = IgorIO(local_path).read()[0]

block

"""
Explanation: Select a random Igor Pro file from within the downloaded archive and read the data with Neo
End of explanation
"""


import matplotlib.pyplot as plt

signal = block.segments[0].analogsignals[0]

plt.plot(signal.times, signal)
plt.ylabel("Signal ({})".format(signal.units.dimensionality.string))
plt.xlabel("Time ({})".format(signal.times.units.dimensionality.string))
plt.title(signal.name)

"""
Explanation: Plot the signal
End of explanation
"""


if signal.annotations['note']:
    annotations = dict(line.split("#") for line in signal.annotations['note'].strip().split("\r"))
else:
    annotations = {}

annotations

"""
Explanation: Extract the annotations
End of explanation
"""


%%capture
!pip install h5py
!pip install https://github.com/NeurodataWithoutBorders/api-python/archive/master.zip

from neo.io import NWBIO

"""
Explanation: Export data to NWB format
End of explanation
"""


io = NWBIO(local_path.replace(".ibw", ".nwb"))

block.segments[0].name = "Segment #0"  # workaround a bug in NWBIO

io.write(block)

"""
Explanation: Here we save a single trace into the NWB file. In practice, you would probably want to combine the data from all Igor Pro files in the zip archive into a single Neo block
End of explanation
"""


%%capture
!pip install nixio

from neo.io import NixIO

io = NixIO(local_path.replace(".ibw", ".h5"), 'rw')

io.write(block)

"""
Explanation: At this point, the exported NWB file fails validation. Development of the NWBIO is ongoing.
Export data to NIX HDF5 format
An alternative HDF5-based format
End of explanation
"""


