# ---
# jupyter:
#   jupytext:
#     text_representation:
#       extension: .py
#       format_name: light
#       format_version: '1.5'
#       jupytext_version: 1.14.4
#   kernelspec:
#     display_name: Python [conda root]
#     language: python
#     name: conda-root-py
# ---

# ## load data from tinberg app

# http://dev.pawelsz.eu/2016/05/read-from-file-length-prefixed-protocol.html

from io import BufferedReader, open
from google.protobuf.internal import decoder
from protobuf_to_dict import protobuf_to_dict


def ReadItm(fname, constructor, size_limit = 0):
    f = open(fname, 'rb')
    reader = BufferedReader(f)
    bytes_read = 0
    while size_limit<=0 or bytes_read<size_limit:
        buffer = reader.peek(10)
        if len(buffer) == 0:
            break
        (size, position) = decoder._DecodeVarint(buffer, 0)
        reader.read(position)
        itm = constructor()
        itm.ParseFromString(reader.read(size))
        bytes_read = bytes_read + position + size
        yield protobuf_to_dict(itm)
    f.close()


# ### load protobufs

# To make 
#
# protoc --python_out=../python behavior.proto

# cd './python'

import behavior_pb2

# cd ..

# ### load behavior

pwd

# +
#fname = '../logs/000005/behavior/behavior.data'
fname = './logs/behavior.data'
constructor = behavior_pb2.Data

from pandas import DataFrame
behavior = DataFrame(ReadItm(fname, constructor))
# -

# ## Plot timeseries

# %matplotlib inline

import matplotlib.pyplot as plt

plt.plot(behavior.time/1000, behavior.delta);
plt.ylim([0, 10])

# +
from numpy import percentile

print behavior[2:].delta.max()
print percentile(behavior[2:].delta, 99.99)
print percentile(behavior[2:].delta, 99.9)
print percentile(behavior[2:].delta, 99)
print behavior[2:].delta.mean()
# -

# ### load trials

fname = './logs/000001/behavior/trial.data'
constructor = trial_pb2.Data

trials = list(ReadItm(fname, constructor))

# ### Plot maze



StarcoderdataJupyterScriptsDedupFiltered