Exemplo n.º 1
0
def upload(experimenter_id, date, mouse_id, filename):
    experimenter = db.execute("SELECT name FROM lab_members WHERE id=%s",
                              (experimenter_id, ))[0][0]
    mouse_name = db.execute("SELECT name FROM mice WHERE id=%s",
                            (mouse_id, ))[0][0]
    currentdate = date.replace('-', '.')
    print(currentdate)
    if os.environ['COMPUTERNAME'] == 'ARMSTRONG':  # Kyle's computer
        pathTo2P = 'X:/Recordings/2P/' + experimenter + '/' + currentdate + '/' + mouse_name + '/'
        if os.path.isdir(pathTo2P) is False:
            pathTo2P = 'C:/Users/kyle/Desktop/recordings/' + experimenter + '/' + currentdate + '/' + mouse_name + '/'
        pathToCond = 'C:/Users/kyle/Dropbox/Data/Conditions/2P/' + time.strftime(
            '%d-%b-%Y', time.strptime(currentdate, '%Y.%m.%d')) + '/'
    elif os.environ['COMPUTERNAME'] == 'MOM1':  # Aquisiton Computer
        pathTo2P = 'D:/data/' + experimenter + '/' + currentdate + '/' + mouse_name + '/'
        pathToCond = 'C:/Users/MOM1/Documents/Dropbox/Data/Conditions/2P/' + time.strftime(
            '%d-%b-%Y', time.strptime(currentdate, '%Y.%m.%d')) + '/'
    if os.path.isdir(pathTo2P) is False:
        print('There is no directory {}'.format(pathTo2P))
        return False
    elif os.path.isdir(pathToCond) is False:
        print('There is no directory {}'.format(pathToCond))
        return False
    cond_files = os.listdir(pathToCond)
    print('pathToCond: {}'.format(pathToCond))
    for cond_file in cond_files:
        print('COND FILE: {}'.format(cond_file))
        base_name, ext = os.path.splitext(cond_file)
        print('pathTo2P: {}'.format((pathTo2P + base_name + '.tif')))
        if os.path.exists(pathTo2P +
                          cond_file) and os.path.exists(pathTo2P + base_name +
                                                        '.tif'):
            c = scipy.io.loadmat(pathToCond + cond_file)
            try:
                c2 = scipy.io.loadmat(pathTo2P + cond_file)
                # debug.enter()
            except IOError:
                print(
                    "You need the 2P conditions file as well as the stimulus conditions file"
                )
            if 'meta_data' in c.keys():
                dd = files2db(c, c2)
                dd['date'] = time.strftime(
                    '%Y-%m-%d', time.strptime(currentdate, '%Y.%m.%d'))
                dd['filename'] = base_name
                dd['experimenter_id'] = experimenter_id
                dd['mouse_id'] = mouse_id
                add2db(dd)
    print('All files uploaded to ED successfully')
Exemplo n.º 2
0
def add2db(dd):
    print('Mouse id = {}'.format(dd['mouse_id']))
    ans = db.execute(
        """
        SELECT mice.id FROM experiments
        LEFT JOIN mice ON mice.id=mouse_id
        LEFT JOIN lab_members ON experiments.lab_member_id=lab_members.id
        WHERE lab_members.id=%s
        AND experiments.date=%s
        AND mice.id=%s""", (dd['experimenter_id'], dd['date'], dd['mouse_id']))
    if ans == []:
        print('No glams entry for animal on {}'.format(dd['date']))
        return 1
    if len(ans) > 1:
        print(
            "More than one experiment was done on this mouse today.  This is ambiguous."
        )
        return 1
    dd['mouse_id'] = ans[0][0]
    db.config['database'] = 'ed'
    ans2 = db.execute(
        """SELECT mouse_id, experimenter_id, date, filename FROM 2P_visual_stims WHERE mouse_id=%s AND experimenter_id=%s AND date=%s AND filename=%s""",
        (dd['mouse_id'], dd['experimenter_id'], dd['date'], dd['filename']))
    if ans2 != []:  #if the entry is already in the database
        print('File {} from {} to database already exists.  File not added.'.
              format(dd['filename'], dd['date']))
        db.config['database'] = 'glams'
        return 1
    print('this works')
    cols = ''
    parameters = []
    for key in dd.keys():
        cols += " {}=%s,".format(key)
        parameters.append(dd[key])
    cols = cols[:-1]
    parameters = tuple(parameters)
    db.execute('INSERT INTO 2P_visual_stims SET {}'.format(cols), parameters)
    db.config['database'] = 'glams'
    print('Added file {} from {} to database.'.format(dd['filename'],
                                                      dd['date']))
Exemplo n.º 3
0
def getExperiments(lab_member_name):
    query = """SELECT r.date as experiment_date, 
                    m.name AS mousename,
                    m.DOB,
                    DATEDIFF(r.date,m.DOB) AS age,
                    r.description,
                    r.notes
            FROM mice as m
            LEFT JOIN experiments as r ON m.id=r.mouse_id
            LEFT JOIN lab_members            ON lab_members.id=r.lab_member_id
            WHERE lab_members.name='{0}' 
            ORDER BY r.date""".format(lab_member_name)
    experiments = db.execute(query)
    return experiments
Exemplo n.º 4
0
def getExperiments(lab_member_name):
    query="""SELECT r.date as experiment_date, 
                    m.name AS mousename,
                    m.DOB,
                    DATEDIFF(r.date,m.DOB) AS age,
                    r.description,
                    r.notes
            FROM mice as m
            LEFT JOIN experiments as r ON m.id=r.mouse_id
            LEFT JOIN lab_members            ON lab_members.id=r.lab_member_id
            WHERE lab_members.name='{0}' 
            ORDER BY r.date""".format(lab_member_name)
    experiments=db.execute(query)
    return experiments
Exemplo n.º 5
0
def saveResults(self):
    if self.persistant is None:
        self.alert = QtGui.QMessageBox.information(
            self, 'Save Results',
            "You must load a file in order to save the results.")
    else:
        self.persistant['ROIs'] = [
            r.getpersistant() for r in self.tiffFig.rois
        ]  #This creates a list with the numbers of interest (determined by getpersistant() for each ROI.
        p = pickle.dumps(self.persistant)
        ids = [
            self.persistant['mouse_id'], self.persistant['experimenter_id'],
            self.persistant['date'], self.persistant['filename']
        ]  #The four components of an ID (moud, experimenter, date, and filename) make a file unique.

        db.config['database'] = 'ed'
        ans = db.execute(
            "SELECT * FROM analysis2p WHERE mouse_id=%s AND experimenter_id=%s AND date=%s and filename=%s",
            tuple(ids)
        )  #This exists to check whether this file has been analyzed before
        date_analyzed = time.strftime('%Y-%m-%d', time.localtime())
        ids.insert(0, date_analyzed)
        ids.insert(1, p)
        if len(ans) > 0:
            db.execute(
                "UPDATE analysis2p SET date_analyzed=%s, data=%s WHERE mouse_id=%s AND experimenter_id=%s AND date=%s AND filename=%s",
                tuple(ids))
            self.statusBar().showMessage(
                "Results were successfully saved (Overwrote previous results)."
            )
        else:
            db.execute(
                "INSERT INTO analysis2p SET date_analyzed=%s, data=%s, mouse_id=%s, experimenter_id=%s, date=%s, filename=%s",
                tuple(ids))
            self.statusBar().showMessage("Results were successfully saved.")
        db.config['database'] = 'glams'
Exemplo n.º 6
0
def getTimedMatings():
    query = """SELECT m.name AS mousename, h1.start_date, h1.end_date, c1.name AS homecage, c2.name AS breedingcage, DATEDIFF(NOW(),h1.start_date) AS daysSinceMating
FROM mice as m 
LEFT JOIN housing as h1 ON h1.mouse_id=m.id
LEFT JOIN housing as h2 ON h2.mouse_id=m.id
LEFT JOIN housing as h3 ON h3.mouse_id=m.id
LEFT JOIN care_taker ON care_taker.cage_id=h1.cage_id
LEFT JOIN lab_members ON lab_members.id=care_taker.lab_member_id
LEFT JOIN cages AS c1 ON c1.id=h1.cage_id
LEFT JOIN cages AS c2 ON c2.id=h2.cage_id
WHERE DATEDIFF(h1.end_date,h1.start_date)=1
AND h2.cage_id=h3.cage_id
AND h2.end_date=h1.start_date
AND h3.start_date=h1.end_date
AND DATEDIFF(NOW(),h1.start_date)<21
ORDER BY c1.name"""
    matings = db.execute(query)
    return matings
Exemplo n.º 7
0
def getTimedMatings():
    query="""SELECT m.name AS mousename, h1.start_date, h1.end_date, c1.name AS homecage, c2.name AS breedingcage, DATEDIFF(NOW(),h1.start_date) AS daysSinceMating
FROM mice as m 
LEFT JOIN housing as h1 ON h1.mouse_id=m.id
LEFT JOIN housing as h2 ON h2.mouse_id=m.id
LEFT JOIN housing as h3 ON h3.mouse_id=m.id
LEFT JOIN care_taker ON care_taker.cage_id=h1.cage_id
LEFT JOIN lab_members ON lab_members.id=care_taker.lab_member_id
LEFT JOIN cages AS c1 ON c1.id=h1.cage_id
LEFT JOIN cages AS c2 ON c2.id=h2.cage_id
WHERE DATEDIFF(h1.end_date,h1.start_date)=1
AND h2.cage_id=h3.cage_id
AND h2.end_date=h1.start_date
AND h3.start_date=h1.end_date
AND DATEDIFF(NOW(),h1.start_date)<21
ORDER BY c1.name"""
    matings=db.execute(query)
    return matings
Exemplo n.º 8
0
def getInfoFromPath(path):
    '''
    This uses the path to return a dictionary containing:
    date - a python date object
    datestr - a string of the date of the format 2014-01-31
    experimenter - a string with the experimenter name as it is in GLAMS
    experimenter_id - an int with the experimenter id as it is in GLAMS
    mouse_name - a string with the mouse name as it is in GLAMS.  If the mouse_name is not in the path, it is a 'None' object
    mouse_id an int with the experimenter id as it is in GLAMS.
    filename - the name of the tif file without the path or the extension

    The path must be either of the form
    "somewhere/experimenter/date/mouse_name/filename.tif"
    or
    "somewhere/experimenter/year.month.day/filename.tif"

    If there is no mouse_name in the path, the experimenter, date, and filename must be unique in ED (the Experiment Database).  If it is not, an error will be thrown.
    '''
    d = dict()
    d['path'] = path
    path = path.split('/')
    d['filename'] = path[-1].split('.tif')[0]
    try:
        d['date'] = time.strptime(path[-2], '%Y.%m.%d')
    except ValueError:  #if index -2 is a mousename and not a date
        d['mouse_name'] = path[-2]
        d['date'] = time.strptime(path[-3], '%Y.%m.%d')
        mnameExists = True
    else:
        d['mouse_name'] = None
        mnameExists = False
    d['datestr'] = time.strftime('%Y-%m-%d', d['date'])
    if mnameExists:
        d['experimenter'] = path[-4]
    else:
        d['experimenter'] = path[-3]

    # debug.enter()

    if mnameExists is False:
        ans = db.execute(
            """
            SELECT mice.id, lab_members.id AS experimenter_id, mice.name AS mouse_name
            FROM experiments
            LEFT JOIN mice ON mice.id=mouse_id
            LEFT JOIN lab_members ON experiments.lab_member_id=lab_members.id
            WHERE lab_members.name=%s
            AND experiments.date=%s""", (d['experimenter'], d['datestr']))
        if ans == []:
            print('No GLAMS entry for an experiment by {} on {}'.format(
                d['experimenter'], d['datestr']))
            return False
        if len(ans) > 1:
            print(
                'Multiple GLAMS entries for experiments by {} on {}. Add a mouse_name to the path of this file.'
                .format(d['experimenter'], d['datestr']))
            return False
        else:
            d['mouse_name'] = ans[0][2]
    else:
        ans = db.execute(
            """
            SELECT mice.id, lab_members.id AS experimenter_id
            FROM experiments
            LEFT JOIN mice ON mice.id=mouse_id
            LEFT JOIN lab_members ON experiments.lab_member_id=lab_members.id
            WHERE lab_members.name=%s
            AND experiments.date=%s
            AND mice.name=%s""",
            (d['experimenter'], d['datestr'], d['mouse_name']))
        if ans == []:
            print('No glams entry for an experiment by {} on mouse {} on {}'.
                  format(d['experimenter'], d['mouse_name'], d['date']))
            return False
    d['mouse_id'] = ans[0][0]
    d['experimenter_id'] = ans[0][1]
    print(
        'Experimenter = {}\nDate of Recording = {}\nMouse name = {}\nFile name = {}'
        .format(d['experimenter'], d['datestr'], d['mouse_name'],
                d['filename']))
    return d
Exemplo n.º 9
0
def loadfile(maingui):
    # remove old figures if there are any
    try:
        maingui.tiffFig.traceFig.deleteLater()
        maingui.tiffFig.responseFig.deleteLater()
        maingui.roi_table.deleteLater()
        maingui.rmax_by_sfreq.deleteLater()
    except Exception:
        pass

    settings = maingui.settings
    #### get conditions from ED ####
    d = getInfoFromPath(settings.d['filename'])
    maingui.persistant = dict()
    maingui.persistant['experimenter_id'] = d['experimenter_id']
    maingui.persistant['date'] = d['datestr']
    maingui.persistant['mouse_id'] = d['mouse_id']
    maingui.persistant['filename'] = d['filename']
    if d is False:  #if there was an error loading the conditions file
        maingui.conditions = None
    else:
        maingui.conditions = getConditions(
            experimenter_id=d['experimenter_id'],
            date=d['datestr'],
            mouse_id=d['mouse_id'],
            filename=d['filename'])

    ###  try loading previous results from ED ###
    ids = [
        maingui.persistant['mouse_id'], maingui.persistant['experimenter_id'],
        maingui.persistant['date'], maingui.persistant['filename']
    ]
    db.config['database'] = 'ed'
    ans = db.execute(
        "SELECT data, id FROM analysis2p WHERE mouse_id=%s AND experimenter_id=%s AND date=%s and filename=%s",
        tuple(ids))
    db.config['database'] = 'glams'
    if len(ans) > 0:
        old_persistant = pickle.loads(ans[0][0])
        maingui.persistant = dict(maingui.persistant.items() +
                                  old_persistant.items())
        maingui.persistant['id'] = ans[0][1]
        print('Found persistent data on ED')
    else:
        maingui.persistant['id'] = None
        maingui.persistant = dict(maingui.persistant.items() + dict.fromkeys(
            ['motionVectors', 'ROIs', 'neuropil_radius', 'alpha']).items())
        print(
            'Persistent data on ED not found. This file has not been loaded into ceilingfaan before.'
        )
    maingui.statusBar().showMessage('Loading tiff')
    t = time.time()
    tif = tifffile.TIFFfile(settings.d['filename'])
    tiffstack = tif.asarray()
    tif.close()
    tiffstack = np.transpose(tiffstack, (0, 2, 1))
    maingui.statusBar().showMessage(
        'Tiff successfully loaded ({} s)'.format(time.time() - t))
    maingui.roi_table = roi_table.make_widget(maingui, show=True)

    maingui.rmax_by_sfreq = TuningCurveCanvas((536, 33, 446, 300))
    maingui.rmax_by_sfreq.show()
    maingui.stats = StatsWindow.from_geo(maingui, 680, 373, 230, 660)
    maingui.stats.show()

    # maingui.rmax_by_sfreq = RMaxBySpatialFrequencyFig()
    # maingui.rmax_by_sfreq.show()
    ## maingui.npplot = maingui.neuropil_mask.addPlot()
    ## maingui.npplot.hideAxis('left')
    ## maingui.npplot.hideAxis('bottom')

    ##########################################################
    # Injected by Hyungtae Kim <*****@*****.**>, in Nov 2014, Feb 2015
    # organize a menu to select a specific frequency based on conditions.
    #
    def on_tfreq_change(maingui, agroup):
        checked = agroup.checkedAction()
        checked_index = agroup.children().index(checked)
        maingui.tiffFig.temporal_frequency_changed(checked_index)

    def on_sfreq_change(maingui, agroup):
        checked = agroup.checkedAction()
        checked_index = agroup.children().index(checked)
        maingui.tiffFig.spatial_frequency_changed(checked_index)

    sf_agroup = maingui.sfreq_action_group
    sf_handler = partial(on_sfreq_change, maingui, sf_agroup)

    tf_agroup = maingui.tfreq_action_group
    tf_handler = partial(on_tfreq_change, maingui, tf_agroup)

    for action in sf_agroup.children():
        sf_agroup.removeAction(action)

    for action in tf_agroup.children():
        tf_agroup.removeAction(action)

    if maingui.conditions:
        maingui.sfreq_meta = SpatialFrequencyMeta(maingui.conditions)
        for index, text in enumerate(map(str,
                                         maingui.sfreq_meta.sfrequencies)):
            if_first = not index

            action = QAction(text, sf_agroup, checkable=True, checked=if_first)
            sf_agroup.addAction(action)
            maingui.sfreq_menu.addAction(action)
            action.triggered[()].connect(sf_handler)

        for index, text in enumerate(map(str,
                                         maingui.sfreq_meta.tfrequencies)):
            if_first = not index

            action = QAction(text, tf_agroup, checkable=True, checked=if_first)
            tf_agroup.addAction(action)
            maingui.tfreq_menu.addAction(action)
            action.triggered[()].connect(tf_handler)
    else:
        maingui.sfreq_meta = None

    channels = []
    if settings.d['nChannels'] == 1:
        channels.append(tiffstack)
        (mt, my, mx) = shape(channels[0])
        tiffstack = np.concatenate(
            (channels[0][..., np.newaxis], np.zeros((mt, my, mx, 2))),
            axis=3)  #this always assumes the only color is green
    elif settings.d['nChannels'] == 2:
        channels.append(tiffstack[0::2])
        channels.append(tiffstack[1::2])
        (mt, my, mx) = shape(channels[0])
        arr1 = channels[0][..., np.newaxis]
        arr2 = channels[1][..., np.newaxis]
        arr3 = np.zeros((mt, my, mx, 1))
        tiffstack = np.concatenate(
            (arr1, arr2, arr3),
            axis=3)  #this always assumes the only two colors are red and green
    elif settings.d['nChannels'] == 3:
        channels.append(tiffstack[0::3])
        channels.append(tiffstack[1::3])
        channels.append(tiffstack[2::3])
        tiffstack = np.concatenate(
            (channels[0][..., np.newaxis], channels[1][..., np.newaxis],
             channels[2][..., np.newaxis]),
            axis=3
        )  #this always assumes the only three colors are red,green, and blue in that order

    COI = channels[settings.d['channelOfInterest'] - 1]  #channel of interest
    (mt, my, mx) = shape(COI)

    if settings.d['motionCorrect']:  #True or False depending on user settings
        maingui.statusBar().showMessage('Performing motion correction...')
        t = time.time()
        if maingui.persistant['motionVectors'] is None:
            print 'motion vector not found, get drift !'
            maingui.persistant['motionVectors'] = getdrift3(COI)
        print 'driftcorrect...',
        tiffstack = driftcorrect(tiffstack,
                                 maingui.persistant['motionVectors'])
        print 'done !'
        maingui.statusBar().showMessage(
            'Finished motion correction ({} seconds)'.format(time.time() - t))
    else:
        maingui.statusBar().showMessage('Motion Correction is Off')
        maingui.persistant['motionVectors'] = None
    tiffFig = TiffFig(
        tiffstack, maingui
    )  # activates the movie display window and the response analysis windows
    # SPG 072414 writes out motion corrected file to matlab where we can save each channel as a tifstack. replace this with python
    # code once we figure out which python TIFF writer to use
    # if settings.d['saveMotionCorrectedTIFF']==True:
    #     # tiffstack.save(settings.d['filename']+'motionCorrected.tif')
    #     # tifffile.imsave(settings.d['filename']+'motionCorrected.tif',tiffstack, compress=6)
    #     matd=dict();
    #     matd['tiffStack']=tiffstack;
    #     scipy.io.savemat(settings.d['filename']+'motionCorrected.tif',matd);
    return tiffFig
Exemplo n.º 10
0
def getaName():
    lab_members = db.execute("SELECT name FROM lab_members")
    return lab_members[0][0]
Exemplo n.º 11
0
def main():
    mice=db.execute("SELECT name FROM mice")
    print(mice)
Exemplo n.º 12
0
def getaName():
    lab_members=db.execute("SELECT name FROM lab_members")
    return lab_members[0][0]
Exemplo n.º 13
0
def main():
    mice = db.execute("SELECT name FROM mice")
    print(mice)