# -*- coding: utf-8 -*-
"""
Neo segment viewer
-----------------------------------------------

Simple code to open neo.Segment viewer and all related object.

"""

import sys
sys.path.append('..')

if __name__ == '__main__':

    from OpenElectrophy.gui.viewers import SegmentViewer
    from OpenElectrophy import TryItIO
    from PyQt4.QtGui import QApplication
    app = QApplication([])

    bl = TryItIO().read(nb_segment=1, duration=100)[0]
    w = SegmentViewer(segment=bl.segments[0])
    w.show()

    app.exec_()
sys.path.append('..')

if __name__ == '__main__':

    from OpenElectrophy import open_db, sql, neo_to_oe, TryItIO
    import datetime
    import numpy

    # connection to a DB
    open_db(url='sqlite:///test.sqlite',
            myglobals=globals(),
            use_global_session=True)

    # create 5 Blocks with different dates
    for i in range(5):
        neo_bl = TryItIO().read(nb_segment=2, duration=2)
        bl = neo_to_oe(neo_bl, cascade=True)
        bl.rec_datetime = datetime.datetime(2012, 06, 1 + i, 12, 30, 40)
        bl.name = 'test sql %d' % i
        bl.save()
        print bl

    # select all Block.id after 3 june 2012 and Segment name is 1
    query = """
                SELECT Block.id, Segment.id, AnalogSignal.id
                FROM Block, Segment, AnalogSignal
                WHERE
                Block.id = Segment.block_id
                AND Segment.id = AnalogSignal.segment_id
                AND Block.rec_datetime > '2012-06-03'