Example #1
0
 def setup(cls, session=None):
     from . import Base
     if not session:
         from pacu.profile import manager
         from pacu.ext.sqlalchemy.orm.session import get_scoped
         session = get_scoped(manager.get('db').as_resolved())
     cls.drop(session)
     cls.make(session)
     cls.dump(session)
     return session
Example #2
0
class ExpV1HandlerResource(HandlerResource):
    DB = manager.get('db')
    def __enter__(self):
        super(ExpV1HandlerResource, self).__enter__()
        if not self.component.keyword:
            raise Exception('Keyword (filename of the recording) can not be empty.')
        return self
    def service_done(self, service):
        result = super(ExpV1HandlerResource, self).service_done(service)
        return self.dump(result)
    def dump(self, result): # to DB
        try:
            payload = result.pop('payload')
            model = ExperimentV1(**result)
            for key, val in payload.items():
                for attr in 'clsname pkgname kwargs'.split():
                    ett_attr = key + '_' + attr
                    ett_val = val.get(attr)
                    setattr(model, ett_attr, ett_val)
            off_duration = model.stimulus_kwargs.get('off_duration')
            model.keyword = self.component.keyword
            model.duration = max(t for ts in model.off_time for t in ts) + off_duration
            session = self.DB.instance()
            session.add(model)
            session.commit()
        except Exception as e:
            print 'An exception from DB!', e
            result['errormsg'] = str(e)
            result['errortype'] = type(e)
            raise e
        else:
            result.update(id=model.id, created_at=model.created_at)
        finally:
            vispath = identity.path.userenv.joinpath('visstim')
            vispath.mkdir_if_none()
            ftime = datetime.now().strftime('%Y-%m-%d_%H_%M_%S')
            logfile = '{}.{}.pickle'.format(ftime, self.component.keyword.replace('/', '_'))
            logpath = vispath.joinpath(logfile)
            with logpath.open(mode='wb') as f:
                cPickle.dump(dict(
                    payload=payload, result=result, keyword=self.component.keyword
                ), f)
        return result
Example #3
0
import pandas as pd
import cv2

from pacu.util.path import Path
from pacu.util import identity
from pacu.util.prop.memoized import memoized_property
from pacu.profile import manager

# from pacu.core.io.scanbox.view.sbx import ScanboxSBXView
# from pacu.core.io.scanbox.view.mat import ScanboxMatView
# from pacu.core.io.scanbox.channel import ScanboxChannel
# from pacu.core.io.scanbox.model import db as schema
# from pacu.core.model.experiment import ExperimentV1

opt = manager.instance('opt')
xulab = manager.get('db').section('xulab')
userenv = identity.path.userenv


def readcam(path):
    cap = cv2.VideoCapture(path)
    length = int(cap.get(cv2.cv.CV_CAP_PROP_FRAME_COUNT))
    print 'Read {} frames...'.format(length)
    fs = np.array([cap.read()[1] for n in range(length)])
    cap.release()
    return fs


class MiniscopeIO(object):
    def __init__(self, path):
        print 'MINISCOPE', path
Example #4
0
from pacu.util.path import Path
from pacu.util import identity
from pacu.util.prop.memoized import memoized_property
from pacu.profile import manager

from pacu.core.io.scanbox.view.sbx import ScanboxSBXView
from pacu.core.io.scanbox.view.mat import ScanboxMatView
from pacu.core.io.scanbox.channel import ScanboxChannel
from pacu.core.io.scanbox.model import db as schema
from pacu.core.model.experiment import ExperimentV1

from pacu.core.addons.export import Export

opt = manager.instance('opt')
glab = manager.get('db').section('glab')()
userenv = identity.path.userenv


class ScanboxIO(object):
    def __init__(self, path, cur_pane=0):
        self.path = userenv.joinpath('scanbox', path).ensure_suffix('.io')
        self.db_path = self.path.joinpath('db.sqlite3').absolute()
        self.mat_path = opt.scanbox_root.joinpath(path).with_suffix('.mat')
        self.sbx_path = opt.scanbox_root.joinpath(path).with_suffix('.sbx')
        self.cur_pane = cur_pane

    @property
    def mat(self):
        return ScanboxMatView(self.mat_path)
Example #5
0
import ujson as json
from pacu.profile import manager
from pacu.core.model.ed.visstim2p import VisStim2P
from pacu.core.model.experiment import ExperimentV1

DB = manager.get('db').as_resolved
ED = manager.get('db').section('ed')
LIMIT = 1000


def get(req, condtype):
    if condtype == '0':  # ScanImage with Matlab VisStim
        ed = ED()
        models = ed.query(VisStim2P.id, VisStim2P.mouse_id, VisStim2P.filename,
                          VisStim2P.spatial_frequencies,
                          VisStim2P.total_time_S,
                          VisStim2P.date).order_by(VisStim2P.id.desc())[:LIMIT]
        return json.dumps([m._asdict() for m in models])
    else:  # ScanBox with PACU VisStim
        db = DB()
        models = db.query(ExperimentV1).order_by(
            ExperimentV1.id.desc())[:LIMIT]
        rv = []
        for m in models:
            rv.append(
                dict(id=m.id,
                     filename=m.payload['handler']['kwargs']['exp_note'],
                     spatial_frequencies=m.payload['stimulus']['kwargs'].get(
                         'sfrequencies')))
        return json.dumps(rv)
Example #6
0
from pacu.util import identity
from pacu.util.path import Path
from pacu.util.prop.memoized import memoized_property
from pacu.core.service.analysis.mapper.scanbox import adapter
from pacu.profile import manager

path_manager = manager.get('path')

class ScanboxData(object):
    meta = None
    data = None
    def __init__(self, matpath):
        self.matpath = matpath
    @property
    def is_available(self):
        try: # literally existence check
            return self.meta is not None and self.data is not None
        except Exception as e:
            return False
    @memoized_property
    def meta(self):
        return adapter.get_meta(self.matpath)
    @memoized_property
    def data(self):
        return self.meta.raw.memmap
    @classmethod
    def populate(cls, path=None):
        path = Path(path) if path else path_manager.instance().scanbox_root
        paths = path.rglob('*.mat')
        data_set = [(path.name, cls(path.str)) for path in paths]
        return [(name, vars(self))
Example #7
0
class LegacyWidefieldHandlerResource(ExpV1HandlerResource):
    DB = manager.get('db')

    def __enter__(self):
        host = self.component.sync_host
        port = self.component.sync_port
        self.member_name = people[self.component.exp_by]['name']
        self.req = Request.with_host_and_port(host, port)
        self.synchronize()
        return super(LegacyWidefieldHandlerResource, self).__enter__()

    def dump(self, result):
        try:
            self.sync_close()
        except Exception as e:
            print 'Unable to close remote device!', type(e), e

        payload = result['payload']
        params = make_params(**payload)
        path = make_condpath(self.now)
        savemat(path.str, params)

        try:
            payload = result.pop('payload')
            model = ExperimentV1(**result)
            for key, val in payload.items():
                print key, val
                for attr in 'clsname pkgname kwargs'.split():
                    ett_attr = key + '_' + attr
                    ett_val = val.get(attr)
                    setattr(model, ett_attr, ett_val)
            session = self.DB.instance()
            session.add(model)
            session.commit()
        except Exception as e:
            print 'An exception from DB!', e
            result['error'] = str(e)
        else:
            result.update(id=model.id, created_at=model.created_at)

        return result

    def synchronize(self):
        # return
        self.sync_state()
        self.sync_metadata()
        self.sync_open()

    def sync_state(self):
        return get(self.req, 'state_check')

    def sync_metadata(self):
        self.now = datetime.now()
        path = make_datapath(self.member_name, self.now)
        return get(self.req, 'sync_metadata/{}'.format(path))

    def sync_open(self):
        return get(self.req, 'open')

    def sync_close(self):
        return get(self.req, 'close')
Example #8
0
    created_at = Column(DateTime, default=datetime.utcnow)
    name = Column(Unicode(256))
    type = Column(Unicode(256))
    data = Column(PickleType)

    @classmethod
    def refresh(cls, type):
        try:
            data_set = entry.get(type).populate()
            s = session.get_scoped(engine)
            purge_count = s.query(cls).filter_by(type=type).delete()
            print 'INTERIM: purged {} item(s) for {}.'.format(
                purge_count, type)
            items = [
                cls(type=type, name=name, data=data) for name, data in data_set
            ]
            s.add_all(items)
            s.commit()
        except Exception as e:
            print 'INTERIM: model failed to refresh {} type'.format(type)
            print e
        else:
            print 'INTERIM: added {} item(s) for {}.'.format(len(items), type)


print 'INTERIM: module initialize...'
engine = manager.get('db').section('interim')()
Interim.__table__.create(engine, checkfirst=True)
Interim.refresh('scanbox_data')
# s = session.get_scoped(engine)
Example #9
0
from pacu.core.model import ExperimentV1
from pacu.core.model.glams import Mice
from pacu.core.model.interim.impl import Interim
from pacu.core.model.interim.impl import engine as interim
from pacu.profile import manager
from pacu.ext.sqlalchemy.orm import session

orm_resource = dict(
    mice=[
        Mice,
        manager.get('db').section('glams'),
        lambda M, query, keyword: query.filter(M.name.contains(
            keyword)).filter(M.DOD == None).order_by(M.id.desc())
    ],
    scanbox_data=[
        Interim, lambda: interim,
        lambda M, query, keyword: query.filter(M.name.contains(
            keyword)).filter(M.type == 'scanbox_data').order_by(M.id.desc())
    ],
    experiment_v1=[
        ExperimentV1,
        manager.get('db').instance, lambda M, query, keyword: query.filter(
            M.experiment_note.contains(keyword)).order_by(M.id.desc())
    ],
)


def to_sui_search(models, upto, count):
    if upto < count:
        footer = 'First {} of {} item(s)'.format(upto, count)
    else:
Example #10
0
 def ed_session(self):
     return manager.get('db').section('ed')()
Example #11
0
 def glams_session(self):
     return manager.get('db').section('glams')()