Exemplo n.º 1
0
    def calculate_confusion_matrices_per_classification(self):
        """Parameters
        ----------

        Returns
        -------
        A dict with keys of workflow IDs and values list
        of golden sets associated with that workflow
        """
        # Load classifications, and golden images from DB
        # Make sure choice is a valid index
        # Make sure to evaluate only logged in users
        # Ignore NONEOFTHEABOVE classificatios when constructing confusion
        # matrix
        # Make sure to the subject classified was a golden image
        query = 'classificationsdev WHERE \"annotations_value_choiceINT\" != \
            -1 AND \"links_user\" != 0 AND \
            \"annotations_value_choiceINT\" != 12 AND \
            CAST(links_subjects AS FLOAT) IN \
            (SELECT \"links_subjects\" FROM goldenimages)'

        columns = [
            'id', 'links_user', 'links_subjects', 'links_workflow',
            'annotations_value_choiceINT'
        ]
        classifications = EventTable.fetch('gravityspy',
                                           query,
                                           columns=columns)

        classifications = classifications.to_pandas()
        classifications = classifications.sort_values('id')
        golden_images = EventTable.fetch('gravityspy', 'goldenimages')
        golden_images_df = golden_images.to_pandas()

        # From answers Dict determine number of classes
        numClasses = len(self.get_answers(workflow=7766).values()[0])

        # merge the golden image DF with th classification (this merge is on
        # links_subject (i.e. the zooID of the image classified)
        image_and_classification = classifications.merge(golden_images_df,
                                                         on=['links_subjects'])

        # groupby users to get there gold classifications
        tmp = image_and_classification.groupby('links_user')[[
            'annotations_value_choiceINT', 'GoldLabel', 'id'
        ]]
        user_confusion_matrices = {}
        for key, item in tmp:
            user_confusion_matrices[key] = {}
            userlabels = tmp.get_group(key)
            rows = []
            cols = []
            entry = []
            for ilabel in userlabels.sort_values('id').itertuples():
                rows.append(ilabel[2])
                cols.append(ilabel[1])
                entry.append(1)
                user_confusion_matrices[key][ilabel[3]] = coo_matrix(
                    (entry, (rows, cols)), shape=(numClasses, numClasses))
        return user_confusion_matrices
Exemplo n.º 2
0
    def clean(self):
        cleaned_data = super(LIGOSearchForm, self).clean()
        zooid = cleaned_data.get('zooid')
        imageid = cleaned_data.get('imageid')
        gpstime = cleaned_data.get('gpstime')
        ifos = cleaned_data.get('ifo')
        database = cleaned_data.get('database')

        if (zooid and imageid and gpstime) or (zooid and imageid) or \
               (zooid and gpstime) or (gpstime and imageid):
            raise forms.ValidationError("Please fill out "
                                        "only one of the zooid "
                                        "or gravityspy id fields"
                                        )

        elif (not zooid) and (not imageid) and (not gpstime):
            raise forms.ValidationError("Please fill out "
                                        "one but not both of the zooid "
                                        "and gravityspy id fields"
                                        )

        if zooid and not imageid and not gpstime:
            if EventTable.fetch('gravityspy', '{0} WHERE links_subjects = {1}'.format(database, zooid), columns=['links_subjects'], host='gravityspyplus.ciera.northwestern.edu').to_pandas().empty:
                raise forms.ValidationError("zooid does not exist"
                                        )

        if imageid and not zooid and not gpstime:
            if EventTable.fetch('gravityspy', '{0} WHERE \"gravityspy_id\" = \'{1}\''.format(database, imageid), columns=['gravityspy_id'], host='gravityspyplus.ciera.northwestern.edu').to_pandas().empty:
                raise forms.ValidationError("uniqueid does not exist"
                                        )
Exemplo n.º 3
0
    def clean(self):
        cleaned_data = super(LIGOSearchForm, self).clean()
        zooid = cleaned_data.get('zooid')
        imageid = cleaned_data.get('imageid')
        gpstime = cleaned_data.get('gpstime')

        if (zooid and imageid and gpstime) or (zooid and imageid) or \
               (zooid and gpstime) or (gpstime and imageid):
            raise forms.ValidationError("Please fill out "
                                        "only one of the zooid "
                                        "or gravityspy id fields"
                                        )

        elif (not zooid) and (not imageid) and (not gpstime):
            raise forms.ValidationError("Please fill out "
                                        "one but not both of the zooid "
                                        "and gravityspy id fields"
                                        )

        if zooid and not imageid and not gpstime:
            if EventTable.fetch('gravityspy', 'similarityindex WHERE links_subjects = {0}'.format(zooid), columns=['links_subjects']).to_pandas().empty:
                raise forms.ValidationError("zooid does not exist"
                                        )

        if imageid and not zooid and not gpstime:
            if EventTable.fetch('gravityspy', 'similarityindex WHERE \"uniqueID\" = \'{0}\''.format(imageid), columns=['uniqueID']).to_pandas().empty:
                raise forms.ValidationError("uniqueid does not exist"
                                        )
Exemplo n.º 4
0
    def check_level_by_classification(self):
        # Obtain workflow order
        order = self.project_info['configuration_workflow_order']
        workflows = [int(str(iWorkflow)) for iWorkflow in order]
        levelWorkflowDict = dict(enumerate(workflows))
        workflowLevelDict = dict(
            (v, k + 1) for k, v in levelWorkflowDict.iteritems())

        query = 'classificationsdev GROUP BY links_user, links_workflow'
        userlevels = EventTable.fetch('gravityspy',
                                      query,
                                      columns=['links_user', 'links_workflow'])

        userlevels = userlevels.to_pandas()
        userlevels['Level'] = userlevels.links_workflow.apply(
            lambda x: workflowLevelDict[x])

        init_user_levels = userlevels.groupby('links_user').Level.max()

        init_user_levels_dict = {
            'userID': init_user_levels.index.tolist(),
            'workflowInit': init_user_levels.tolist()
        }

        userStatusInit = pd.DataFrame(init_user_levels_dict)
        self.userStatusInit = userStatusInit
        return userStatusInit
Exemplo n.º 5
0
def dategraph(request):
    # if this is a POST request we need to process the form data
    if request.method == 'GET':

        # create a form instance and populate it with data from the request:
        form = SearchForm(request.GET)
        # check whether it's valid:
        if form.is_valid():
            username = str(form.cleaned_data['username'])
            collection_display_name = str(
                form.cleaned_data['collection_display_name'])

            subjects_in_collection, tmp = retrieve_subjects_from_collection(
                username, collection_display_name)
            subjects_in_collection = [
                str(isubject) for isubject in subjects_in_collection
            ]
            SI_glitches = EventTable.fetch(
                'gravityspy',
                'glitches WHERE CAST(links_subjects AS FLOAT) IN ({0})'.format(
                    str(",".join(subjects_in_collection))),
                host='gravityspyplus.ciera.northwestern.edu').to_pandas()
            fig = obtain_figure(SI_glitches)
            canvas = FigureCanvas(fig)
            buf = io.BytesIO()
            canvas.print_png(buf)
            response = HttpResponse(buf.getvalue(), content_type='image/png')
            fig.clear()
            return response
Exemplo n.º 6
0
def collectioninfo(request):
    # if this is a POST request we need to process the form data
    if request.method == 'GET':

        # create a form instance and populate it with data from the request:
        form = SearchForm(request.GET)
        # check whether it's valid:
        if form.is_valid():
            username = str(form.cleaned_data['username'])
            collection_display_name = str(
                form.cleaned_data['collection_display_name'])

            subjects_in_collection, tmp = retrieve_subjects_from_collection(
                username, collection_display_name)
            subjects_in_collection = [
                str(isubject) for isubject in subjects_in_collection
            ]
            SI_glitches = EventTable.fetch(
                'gravityspy',
                'glitches WHERE CAST(links_subjects AS FLOAT) IN ({0})'.format(
                    str(",".join(subjects_in_collection))),
                host='gravityspyplus.ciera.northwestern.edu').to_pandas()
            dategraph_url = request.get_full_path()[::-1].replace(
                'collection-info'[::-1], 'dategraph'[::-1], 1)[::-1]

            return render(
                request, 'collection_results.html', {
                    'results': SI_glitches.to_dict(orient='records'),
                    'dategraph_url': dategraph_url
                })
        else:
            return render(request, 'collectioninfo.html', {'form': form})
Exemplo n.º 7
0
def retrain_model(request):
    # if this is a POST request we need to process the form data
    if request.method == 'POST':

        # create a form instance and populate it with data from the request:
        form = NewClassForm(request.POST)
        # check whether it's valid:
        if form.is_valid():
            collection_owner = str(form.cleaned_data['collection_owner'])
            collection_name = str(form.cleaned_data['collection_name'])
            new_class_name = str(form.cleaned_data['new_class_name'])

            # First determine the subjects attempting to be added to the training set
            subjects_in_collection, tmp = retrieve_subjects_from_collection(collection_owner, collection_name)
            subjects_in_collection = [str(isubject) for isubject in subjects_in_collection]

            new_subjects = list(EventTable.fetch('gravityspy',
                                                 'glitches WHERE CAST(links_subjects AS FLOAT) IN ({0})'.format(str(",".join(subjects_in_collection))),
                                                  columns=["gravityspy_id"], host='gravityspyplus.ciera.northwestern.edu')['gravityspy_id'])

            requested_model, created = NewClass.objects.get_or_create(collection_owner=collection_owner,
                                                                      collection_name=collection_name,
                                                                      new_class_name=new_class_name,
                                                                      new_subjects=new_subjects,
                                                                      user=request.user)
            requested_model.save()

            return render(request, 'temp.html') 
        else:
            return render(request, 'retrain-model-form.html', {'form': form})
Exemplo n.º 8
0
def collectioninfo(request):
    # if this is a POST request we need to process the form data
    if request.method == 'GET':

        # create a form instance and populate it with data from the request:
        form = SearchForm(request.GET)
        # check whether it's valid:
        if form.is_valid():
            username = str(form.cleaned_data['username'])
            collection_display_name = str(form.cleaned_data['collection_display_name'])

            subjects_in_collection = retrieve_subjects_from_collection(username, collection_display_name)
            subjects_in_collection = [str(isubject) for isubject in subjects_in_collection]
            SI_glitches = EventTable.fetch('gravityspy', 'glitches WHERE CAST(links_subjects AS FLOAT) IN ({0})'.format(str(",".join(subjects_in_collection)))).to_pandas() 
            
                GlitchValues = SI_glitches.values
                GlitchGPS = (GlitchValues[:,4]).tolist()
                newestGPS = (max(GlitchGPS))
                oldestGPS = (min(GlitchGPS))
                print (newestGPS)
                print (oldestGPS)

                #converting newest and oldest to dates
                newest = timeconvert.gps2ppl(newestGPS)
                oldest = timeconvert.gps2ppl(oldestGPS)
                print (newest)
                print (oldest)

                #number of weeks
                def nofweeks(d1,d2):
                    d1 = datetime.datetime.strptime(d1, "%Y-%m-%d %H:%M")
                    d2 = datetime.datetime.strptime(d2, "%Y-%m-%d %H:%M")
                    hop = ((abs((d2 - d1).days))/7)
                    if hop > 1:
                        return hop
                    elif hop <= 1:
                        return 1
                print (nofweeks(newest, oldest))

                #checking and changing xlocs, xlabels
                plt.hist(GlitchGPS, bins = (nofweeks(newest, oldest)))
                xlocs, xlabels = plt.xticks()
                print (xlocs)
    
                GPSlabels = list()
                for x in range(len(xlocs)):
                    GPSlabels.append(timeconvert.gps2ppl(xlocs[x]))
                plt.xticks(xlocs, GPSlabels, rotation = 45)
                plt.title("Distribution of Glitches \n Each Bin Represents One Week")
                plt.xlabel("Time")
                plt.ylabel("Number of Glitches per Week")
                plt.tight_layout()
                plt.hist(GlitchGPS, bins = (nofweeks(newest, oldest)))
                xlocs, xlabels = plt.xticks()
                print (xlocs)

                plt.savefig("final.png")
            
            return render(request, 'searchresults.html', {'results': SI_glitches.to_dict(orient='records')})
Exemplo n.º 9
0
def searchDB(form):

    # process the data in form.cleaned_data as required
    # process the data in form.cleaned_data as required
    glitchclass = str(form.cleaned_data['glitchclass'])

    SI_glitches = EventTable.fetch('gravityspy', 'trainingset WHERE \"Label\" = \'{0}\''.format(glitchclass), columns = ['gravityspy_id', 'Filename1', 'Filename2', 'Filename3', 'Filename4', 'ifo', 'snr', 'peak_frequency', 'Label']).to_pandas()

    SI_glitches['url1'] = SI_glitches[['ifo', 'Filename1']].apply(makelink, axis=1)
    SI_glitches['url2'] = SI_glitches[['ifo', 'Filename2']].apply(makelink, axis=1)
    SI_glitches['url3'] = SI_glitches[['ifo', 'Filename3']].apply(makelink, axis=1)
    SI_glitches['url4'] = SI_glitches[['ifo', 'Filename4']].apply(makelink, axis=1)

    return SI_glitches
def training_set_raw_data(filename,
                          format,
                          duration=8,
                          sample_frequency=4096,
                          verbose=False,
                          **kwargs):
    """Obtain the raw timeseries for the whole training set

    Parameters:

        filename (str):

        format (str):

        duration (int, optional):

        sample_frequency (int, optional):

        verbose (bool, optional):

    Returns:

        A file containing the raw timeseries data of the training set
    """
    logger = log.Logger('Gravity Spy: Obtaining TimeSeries'
                        ' Data For Trainingset')
    trainingset_table = EventTable.fetch(
        'gravityspy',
        'trainingsetv1d1',
        columns=['event_time', 'ifo', 'true_label'])
    for ifo, gps, label in zip(trainingset_table['ifo'],
                               trainingset_table['event_time'],
                               trainingset_table['true_label']):
        logger.info('Obtaining sample {0} with gps {1} from '
                    '{2}'.format(label, gps, ifo))
        data = fetch_data(ifo,
                          gps,
                          duration=duration,
                          sample_frequency=sample_frequency,
                          verbose=verbose,
                          **kwargs)
        logger.info('Writing Sample To File..')
        data.write(filename,
                   format=format,
                   append=True,
                   path='/data/{0}/{1}/'.format(label, gps))
Exemplo n.º 11
0
def histogram(request):
    # if this is a POST request we need to process the form data
    if request.method == 'GET':

        # create a form instance and populate it with data from the request:
        form = PosteriorForm(request.GET)
        # check whether it's valid:
        if form.is_valid():
            graceid = form.cleaned_data['graceid']
            param1 = form.cleaned_data['param1']
            param2 = form.cleaned_data['param2']
            param1_min = form.cleaned_data['param1_min']
            param1_max = form.cleaned_data['param1_max']
            param2_min = form.cleaned_data['param2_min']
            param2_max = form.cleaned_data['param2_max']
            client = GraceDb("https://gracedb-playground.ligo.org/api/")
            #event = client.event(graceid)
            #filename = client.files(graceid, 'event.log')
            ps = EventTable.fetch(
                'gravityspy',
                '\"{0}\"'.format(graceid),
                selection=[
                    '{0}<{1}<{2}'.format(param1_min, param1, param1_max),
                    '{0}<{1}<{2}'.format(param2_min, param2, param2_max)
                ],
                columns=[param1, param2])
            ps = ps.to_pandas().iloc[0:1000]

            with seaborn.axes_style('white'):
                plot = seaborn.jointplot(param1, param2, ps, kind='kde')

            fig = plot.fig
            canvas = FigureCanvas(fig)

            import io
            buf = io.BytesIO()
            canvas.print_png(buf)
            response = HttpResponse(buf.getvalue(), content_type='image/png')
            fig.clear()
            return response
Exemplo n.º 12
0
    def clean(self):
        cleaned_data = super(SearchForm, self).clean()
        zooid = cleaned_data.get('zooid')
        imageid = cleaned_data.get('imageid')
        ifos = str(cleaned_data.get('ifo'))
        database = cleaned_data.get('database')
        era = cleaned_data.get('era')
 
        if zooid and imageid:
            raise forms.ValidationError("Please fill out "
                                        "only one of the zooid "
                                        "or gravityspy id fields"
                                        )

        elif (not zooid) and (not imageid):
            raise forms.ValidationError("Please fill out "
                                        "one but not both of the zooid "
                                        "and gravityspy id fields"
                                        )

        if zooid and not imageid:
            if not EventTable.fetch('gravityspy', 'nonanalysisreadyids WHERE links_subjects = {0}'.format(zooid), host='gravityspyplus.ciera.northwestern.edu').to_pandas().empty:
                raise forms.ValidationError("This zooID is one of a handful of glitches that were mistakenly uploaded, despite being glitches"
                                            "occuring while the detector was not in a state to be taking quality data "
                                            "(i.e. people may have been working on the instrument at the time."
                                        )

            if EventTable.fetch('gravityspy', '{0} WHERE links_subjects = {1}'.format(database, zooid), columns=['links_subjects'], host='gravityspyplus.ciera.northwestern.edu').to_pandas().empty:
                    raise forms.ValidationError("zooid does not exist")

            elif EventTable.fetch('gravityspy', '{0} WHERE links_subjects = {1} AND ifo IN ({2})'.format(database, zooid, ifos), columns=['links_subjects'], host='gravityspyplus.ciera.northwestern.edu').to_pandas().empty:
                raise forms.ValidationError("This image is not from one of the interferometers you selected"
                                        )

        if imageid and not zooid:
            if not EventTable.fetch('gravityspy', 'nonanalysisreadyids WHERE \"gravityspy_id\" = \'{0}\''.format(imageid), host='gravityspyplus.ciera.northwestern.edu').to_pandas().empty:
                raise forms.ValidationError("This gravityspy_id is one of a handful of glitches that were mistakenly uploaded, despite being glitches"
                                            "occuring while the detector was not in a state to be taking quality data "
                                            "(i.e. people may have been working on the instrument at the time."
                                            )

            if EventTable.fetch('gravityspy', '{0} WHERE \"gravityspy_id\" = \'{1}\''.format(database, imageid), columns=['gravityspy_id'], host='gravityspyplus.ciera.northwestern.edu').to_pandas().empty:
                raise forms.ValidationError("uniqueid does not exist")

            elif EventTable.fetch('gravityspy', '{0} WHERE \"gravityspy_id\" = \'{1}\' AND ifo IN ({2})'.format(database, imageid, ifos), columns=['gravityspy_id'], host='gravityspyplus.ciera.northwestern.edu').to_pandas().empty:
                raise forms.ValidationError("This image is not from one of the interferometers you selected"
                                            )
Exemplo n.º 13
0
def posteriors(request):
    # if this is a POST request we need to process the form data
    if request.method == 'GET':

        # create a form instance and populate it with data from the request:
        form = PosteriorForm(request.GET)
        # check whether it's valid:
        if form.is_valid():
            graceid = form.cleaned_data['graceid']
            param1 = form.cleaned_data['param1']
            param2 = form.cleaned_data['param2']
            param1_min = form.cleaned_data['param1_min']
            param1_max = form.cleaned_data['param1_max']
            param2_min = form.cleaned_data['param2_min']
            param2_max = form.cleaned_data['param2_max']
            client = GraceDb("https://gracedb-playground.ligo.org/api/")
            #event = client.event(graceid)
            #filename = client.files(graceid, 'event.log')
            ps = EventTable.fetch(
                'gravityspy',
                '\"{0}\"'.format(graceid),
                selection=[
                    '{0}<{1}<{2}'.format(param1_min, param1, param1_max),
                    '{0}<{1}<{2}'.format(param2_min, param2, param2_max)
                ],
                columns=[param1, param2])
            ps = ps.to_pandas().iloc[0:1000]
            old = 'posteriors'
            new = 'histogram'
            histogramurl = (request.get_full_path()[::-1].replace(
                old[::-1], new[::-1], 1))[::-1]

            return render(
                request, 'gracedb.html', {
                    'results': ps.iloc[0:1000].to_dict(orient='records'),
                    'histogramurl': histogramurl
                })
        else:
            return render(request, 'form.html', {'form': form})
Exemplo n.º 14
0
def get_imageid_json(name=''):
    return EventTable.fetch('gravityspy', 'similarity_index_o3 WHERE \"gravityspy_id\" ~ \'{0}\' LIMIT 20'.format(name), columns=["gravityspy_id"], host='gravityspyplus.ciera.northwestern.edu').to_pandas().rename(columns={'gravityspy_id': 'value'}).to_json(orient='records')
Exemplo n.º 15
0
import matplotlib.pyplot as plt
plt.switch_backend('agg')
from gwpy.table import EventTable
blips_O2_L1 = EventTable.fetch('gravityspy',
                               'glitches',
                               selection=[
                                   '"Label" = "Blip"',
                                   '1200000000 > "peakGPS" > 1137250000',
                                   '"Blip" > 0.95', 'ifo=L1'
                               ])
koi_O2_L1 = EventTable.fetch('gravityspy',
                             'glitches',
                             selection=[
                                 '"Label" = "Koi_Fish"',
                                 '1200000000 > "peakGPS" > 1137250000',
                                 '"Koi_Fish" > 0.95', 'ifo=L1'
                             ])
aftercomiss_koi_l1 = koi_O2_L1[koi_O2_L1['peakGPS'] > 1178841618]
beforecomiss_koi_l1 = koi_O2_L1[koi_O2_L1['peakGPS'] < 1178841618]
beforecomiss_blips_l1 = blips_O2_L1[blips_O2_L1['peakGPS'] < 1178841618]
aftercomiss_blips_l1 = blips_O2_L1[blips_O2_L1['peakGPS'] > 1178841618]
plot = aftercomiss_blips_l1.hist('snr',
                                 logbins=True,
                                 bins=50,
                                 histtype='stepfilled',
                                 label='After Commissioning')
ax = plot.gca()
ax.hist(aftercomiss_koi_l1['snr'],
        logbins=True,
        bins=50,
        histtype='stepfilled',
Exemplo n.º 16
0
def get_gpstimes_json(name=''):
    return EventTable.fetch('gravityspy', 'similarity_index_o3 WHERE CAST(\"event_time\" AS TEXT) ~ \'{0}\' LIMIT 20'.format(name), columns=["event_time"], host='gravityspyplus.ciera.northwestern.edu').to_pandas().astype(str).rename(columns={'event_time': 'value'}).to_json(orient='records')
Exemplo n.º 17
0
def similarity_search(form):

    # process the data in form.cleaned_data as required
    if form.cleaned_data['imageid']:
        uniqueID = str(form.cleaned_data['imageid'])
    else:
        uniqueID = form.cleaned_data['imageid']
    if form.cleaned_data['zooid']:
        zooID = float(str(form.cleaned_data['zooid']))
    else:
        zooID = form.cleaned_data['zooid']
    if not zooID and not uniqueID:
        gpstime = float(str(form.cleaned_data['gpstime']))
    else:
        gpstime = False

    howmany = int(form.cleaned_data['howmany'])

    engine = create_engine(
        'postgresql://{0}:{1}@gravityspy.ciera.northwestern.edu:5432/gravityspy'
        .format(os.environ['GRAVITYSPY_DATABASE_USER'],
                os.environ['GRAVITYSPY_DATABASE_PASSWD']))
    if zooID:
        PostGresEuclideanDistanceQuery = 'WITH searcharray AS (SELECT cube(array["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "140", "141", "142", "143", "144", "145", "146", "147", "148", "149", "150", "151", "152", "153", "154", "155", "156", "157", "158", "159", "160", "161", "162", "163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189", "190", "191", "192", "193", "194", "195", "196", "197", "198", "199"]) FROM similarityindex WHERE "links_subjects" = \'{0}\'), t AS (SELECT "uniqueID", links_subjects, cube(array["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "140", "141", "142", "143", "144", "145", "146", "147", "148", "149", "150", "151", "152", "153", "154", "155", "156", "157", "158", "159", "160", "161", "162", "163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189", "190", "191", "192", "193", "194", "195", "196", "197", "198", "199"]) FROM similarityindex) SELECT "uniqueID", "links_subjects" FROM t order by (SELECT * FROM searcharray) <-> cube asc limit {1};'.format(
            zooID, howmany)
    elif uniqueID:
        PostGresEuclideanDistanceQuery = 'WITH searcharray AS (SELECT cube(array["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "140", "141", "142", "143", "144", "145", "146", "147", "148", "149", "150", "151", "152", "153", "154", "155", "156", "157", "158", "159", "160", "161", "162", "163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189", "190", "191", "192", "193", "194", "195", "196", "197", "198", "199"]) FROM similarityindex WHERE "uniqueID" = \'{0}\'), t AS (SELECT "uniqueID", links_subjects, cube(array["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "140", "141", "142", "143", "144", "145", "146", "147", "148", "149", "150", "151", "152", "153", "154", "155", "156", "157", "158", "159", "160", "161", "162", "163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189", "190", "191", "192", "193", "194", "195", "196", "197", "198", "199"]) FROM similarityindex) SELECT "uniqueID", "links_subjects" FROM t order by (SELECT * FROM searcharray) <-> cube asc limit {1};'.format(
            uniqueID, howmany)
    elif gpstime:
        PostGresEuclideanDistanceQuery = 'WITH searcharray AS (SELECT cube(array["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "140", "141", "142", "143", "144", "145", "146", "147", "148", "149", "150", "151", "152", "153", "154", "155", "156", "157", "158", "159", "160", "161", "162", "163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189", "190", "191", "192", "193", "194", "195", "196", "197", "198", "199"]) FROM similarityindex WHERE "peakGPS" = \'{0}\'), t AS (SELECT "uniqueID", links_subjects, cube(array["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "140", "141", "142", "143", "144", "145", "146", "147", "148", "149", "150", "151", "152", "153", "154", "155", "156", "157", "158", "159", "160", "161", "162", "163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189", "190", "191", "192", "193", "194", "195", "196", "197", "198", "199"]) FROM similarityindex) SELECT "uniqueID", "links_subjects" FROM t order by (SELECT * FROM searcharray) <-> cube asc limit {1};'.format(
            repr(gpstime), howmany)

    # Query Similarity Index to give you the most similar IDs
    SI = pd.read_sql(PostGresEuclideanDistanceQuery, engine)

    glitches = EventTable.fetch(
        'gravityspy',
        'glitches WHERE "uniqueID" IN (\'{0}\')'.format(
            str("','".join(list(SI['uniqueID'])))),
        columns=[
            'uniqueID', 'imgUrl1', 'imgUrl2', 'imgUrl3', 'imgUrl4', 'ifo',
            'links_subjects', 'snr', 'peak_frequency', 'Label', 'peakGPS',
            'Filename1', 'Filename2', 'Filename3', 'Filename4'
        ])

    # Convert from astropy to pandas for easy manipulation
    SI_glitches = glitches.to_pandas()

    SI_glitches.loc[SI_glitches.imgUrl1 == '?',
                    'imgUrl1'] = SI_glitches.loc[SI_glitches.imgUrl1 == '?',
                                                 ['ifo', 'Filename1']].apply(
                                                     makelink, axis=1)
    SI_glitches.loc[SI_glitches.imgUrl2 == '?',
                    'imgUrl2'] = SI_glitches.loc[SI_glitches.imgUrl2 == '?',
                                                 ['ifo', 'Filename2']].apply(
                                                     makelink, axis=1)
    SI_glitches.loc[SI_glitches.imgUrl3 == '?',
                    'imgUrl3'] = SI_glitches.loc[SI_glitches.imgUrl3 == '?',
                                                 ['ifo', 'Filename3']].apply(
                                                     makelink, axis=1)
    SI_glitches.loc[SI_glitches.imgUrl4 == '?',
                    'imgUrl4'] = SI_glitches.loc[SI_glitches.imgUrl4 == '?',
                                                 ['ifo', 'Filename4']].apply(
                                                     makelink, axis=1)

    if zooID:
        SI_glitches['searchedID'] = SI_glitches.loc[
            SI_glitches.links_subjects == zooID, 'uniqueID'].iloc[0]
    elif gpstime:
        SI_glitches['searchedID'] = SI_glitches.loc[
            SI_glitches.peakGPS == gpstime, 'uniqueID'].iloc[0]
    else:
        SI_glitches['searchedID'] = uniqueID

    if uniqueID:
        SI_glitches['searchedzooID'] = SI_glitches.loc[
            SI_glitches.uniqueID == uniqueID, 'links_subjects'].iloc[0]
    elif gpstime:
        SI_glitches['searchedzooID'] = SI_glitches.loc[
            SI_glitches.peakGPS == gpstime, 'links_subjects'].iloc[0]
    else:
        SI_glitches['searchedzooID'] = zooID

    return SI_glitches
Exemplo n.º 18
0
    def calculate_confusion_matrices(self):
        """Parameters
        ----------

        Returns
        -------
        A dict with keys of workflow IDs and values list
        of golden sets associated with that workflow
        """
        # Load classifications, and golden images from DB
        # Make sure choice is a valid index
        # Make sure to evaluate only logged in users
        # Ignore NONEOFTHEABOVE classificatios when constructing confusion
        # matrix
        # Make sure to the subject classified was a golden image
        query = 'classificationsdev WHERE \"annotations_value_choiceINT\" != \
            -1 AND \"links_user\" != 0 AND \
            \"annotations_value_choiceINT\" != 12 AND \
            CAST(links_subjects AS FLOAT) IN \
            (SELECT \"links_subjects\" FROM goldenimages)'

        columns = [
            'id', 'links_user', 'links_subjects', 'links_workflow',
            'annotations_value_choiceINT'
        ]
        classifications = EventTable.fetch('gravityspy',
                                           query,
                                           columns=columns)

        classifications = classifications.to_pandas()
        classifications = classifications.sort_values('id')
        golden_images = EventTable.fetch('gravityspy', 'goldenimages')
        golden_images_df = golden_images.to_pandas()

        # From answers Dict determine number of classes
        numClasses = len(self.get_answers(workflow=7766).values()[0])

        # merge the golden image DF with th classification (this merge is on
        # links_subject (i.e. the zooID of the image classified)
        image_and_classification = classifications.merge(golden_images_df,
                                                         on=['links_subjects'])

        # This is where the power of pandas comes in...on the fly in very quick
        # order we can fill all users confusion matrices
        # by smartly chosen groupby
        test = image_and_classification.groupby(
            ['links_user', 'annotations_value_choiceINT', 'GoldLabel'])
        test = test.count().links_subjects.to_frame().reset_index()

        # Create "Sparse Matrices" and perform a normalization task on them.
        # Afterwards determine if the users diagonal
        # is above the threshold set above
        confusion_matrices = pd.DataFrame()
        for iUser in test.groupby('links_user'):
            columns = iUser[1].annotations_value_choiceINT
            rows = iUser[1]['GoldLabel']
            entry = iUser[1]['links_subjects']
            tmp = coo_matrix((entry, (rows, columns)),
                             shape=(numClasses, numClasses))
            conf_divided, a1, a2, a3 = \
                np.linalg.lstsq(np.diagflat(tmp.sum(axis=1)),
                                            tmp.todense())

            conf_dict = {
                'userID': iUser[0],
                'conf_matrix': [conf_divided],
                'alpha': [np.diag(conf_divided)]
            }

            confusion_matrices = \
                confusion_matrices.append(pd.DataFrame(
                                                       conf_dict,
                                                       index=[iUser[0]]))

        self.confusion_matrices = confusion_matrices
        return confusion_matrices
Exemplo n.º 19
0
def similarity_search(form):

    # process the data in form.cleaned_data as required
    if form.cleaned_data['imageid']:
        gravityspy_id = str(form.cleaned_data['imageid'])
    else:
        gravityspy_id = form.cleaned_data['imageid']
    if form.cleaned_data['zooid']:
        zooID = float(str(form.cleaned_data['zooid']))
    else:
        zooID = form.cleaned_data['zooid']
    if not zooID and not gravityspy_id:
        gpstime = float(str(form.cleaned_data['gpstime']))
    else:
        gpstime = False

    howmany = int(form.cleaned_data['howmany'])
    database = str(form.cleaned_data['database'])
    ifos = str(form.cleaned_data['ifo'])
    era = str(form.cleaned_data['era'])

    engine = create_engine(
        'postgresql://{0}:{1}@gravityspyplus.ciera.northwestern.edu:5432/gravityspy'
        .format(os.environ['GRAVITYSPYPLUS_DATABASE_USER'],
                os.environ['GRAVITYSPY_DATABASE_PASSWD']))
    if zooID:
        PostGresEuclideanDistanceQuery = 'WITH searcharray AS (SELECT cube(array["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "140", "141", "142", "143", "144", "145", "146", "147", "148", "149", "150", "151", "152", "153", "154", "155", "156", "157", "158", "159", "160", "161", "162", "163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189", "190", "191", "192", "193", "194", "195", "196", "197", "198", "199"]) FROM {2} WHERE "links_subjects" = \'{0}\'), t AS (SELECT "gravityspy_id", links_subjects, cube(array["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "140", "141", "142", "143", "144", "145", "146", "147", "148", "149", "150", "151", "152", "153", "154", "155", "156", "157", "158", "159", "160", "161", "162", "163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189", "190", "191", "192", "193", "194", "195", "196", "197", "198", "199"]) FROM {2} WHERE ifo IN ({3}) AND {4}) SELECT * FROM t order by (SELECT * FROM searcharray) <-> cube asc limit {1};'.format(
            int(zooID), howmany, database, ifos, era)
    elif gravityspy_id:
        PostGresEuclideanDistanceQuery = 'WITH searcharray AS (SELECT cube(array["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "140", "141", "142", "143", "144", "145", "146", "147", "148", "149", "150", "151", "152", "153", "154", "155", "156", "157", "158", "159", "160", "161", "162", "163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189", "190", "191", "192", "193", "194", "195", "196", "197", "198", "199"]) FROM {2} WHERE "gravityspy_id" = \'{0}\'), t AS (SELECT "gravityspy_id", links_subjects, cube(array["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "140", "141", "142", "143", "144", "145", "146", "147", "148", "149", "150", "151", "152", "153", "154", "155", "156", "157", "158", "159", "160", "161", "162", "163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189", "190", "191", "192", "193", "194", "195", "196", "197", "198", "199"]) FROM {2} WHERE ifo IN ({3}) AND {4}) SELECT * FROM t order by (SELECT * FROM searcharray) <-> cube asc limit {1};'.format(
            gravityspy_id, howmany, database, ifos, era)
    elif gpstime:
        PostGresEuclideanDistanceQuery = 'WITH searcharray AS (SELECT cube(array["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "140", "141", "142", "143", "144", "145", "146", "147", "148", "149", "150", "151", "152", "153", "154", "155", "156", "157", "158", "159", "160", "161", "162", "163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189", "190", "191", "192", "193", "194", "195", "196", "197", "198", "199"]) FROM {2} WHERE "event_time" = \'{0}\'), t AS (SELECT "gravityspy_id", links_subjects, cube(array["0", "1", "2", "3", "4", "5", "6", "7", "8", "9", "10", "11", "12", "13", "14", "15", "16", "17", "18", "19", "20", "21", "22", "23", "24", "25", "26", "27", "28", "29", "30", "31", "32", "33", "34", "35", "36", "37", "38", "39", "40", "41", "42", "43", "44", "45", "46", "47", "48", "49", "50", "51", "52", "53", "54", "55", "56", "57", "58", "59", "60", "61", "62", "63", "64", "65", "66", "67", "68", "69", "70", "71", "72", "73", "74", "75", "76", "77", "78", "79", "80", "81", "82", "83", "84", "85", "86", "87", "88", "89", "90", "91", "92", "93", "94", "95", "96", "97", "98", "99", "100", "101", "102", "103", "104", "105", "106", "107", "108", "109", "110", "111", "112", "113", "114", "115", "116", "117", "118", "119", "120", "121", "122", "123", "124", "125", "126", "127", "128", "129", "130", "131", "132", "133", "134", "135", "136", "137", "138", "139", "140", "141", "142", "143", "144", "145", "146", "147", "148", "149", "150", "151", "152", "153", "154", "155", "156", "157", "158", "159", "160", "161", "162", "163", "164", "165", "166", "167", "168", "169", "170", "171", "172", "173", "174", "175", "176", "177", "178", "179", "180", "181", "182", "183", "184", "185", "186", "187", "188", "189", "190", "191", "192", "193", "194", "195", "196", "197", "198", "199"]) FROM {2} WHERE ifo IN ({3}) AND {4}) SELECT * FROM t order by (SELECT * FROM searcharray) <-> cube asc limit {1};'.format(
            repr(gpstime), howmany, database, ifos, era)

    # Query Similarity Index to give you the most similar IDs
    SI = pd.read_sql(PostGresEuclideanDistanceQuery, engine)
    features = SI['cube'].apply(extract_feature_array)
    features = numpy.vstack(features.values)

    glitches = EventTable.fetch(
        'gravityspy',
        'glitches WHERE "gravityspy_id" IN (\'{0}\')'.format(
            str("','".join(list(SI['gravityspy_id'])))),
        columns=[
            'gravityspy_id', 'url1', 'url2', 'url3', 'url4', 'ifo',
            'links_subjects', 'snr', 'peak_frequency', 'duration',
            'central_freq', 'bandwidth', 'ml_label', 'event_time', 'Filename1',
            'Filename2', 'Filename3', 'Filename4', 'peak_time'
        ],
        host='gravityspyplus.ciera.northwestern.edu')

    # Convert from astropy to pandas for easy manipulation
    SI_glitches = glitches.to_pandas()

    SI_glitches.loc[SI_glitches.url1 == '?',
                    'url1'] = SI_glitches.loc[SI_glitches.url1 == '?',
                                              ['ifo', 'Filename1']].apply(
                                                  makelink, axis=1)
    SI_glitches.loc[SI_glitches.url2 == '?',
                    'url2'] = SI_glitches.loc[SI_glitches.url2 == '?',
                                              ['ifo', 'Filename2']].apply(
                                                  makelink, axis=1)
    SI_glitches.loc[SI_glitches.url3 == '?',
                    'url3'] = SI_glitches.loc[SI_glitches.url3 == '?',
                                              ['ifo', 'Filename3']].apply(
                                                  makelink, axis=1)
    SI_glitches.loc[SI_glitches.url4 == '?',
                    'url4'] = SI_glitches.loc[SI_glitches.url4 == '?',
                                              ['ifo', 'Filename4']].apply(
                                                  makelink, axis=1)

    if zooID:
        SI_glitches['searchedID'] = SI_glitches.loc[
            SI_glitches.links_subjects == zooID, 'gravityspy_id'].iloc[0]
    elif gpstime:
        SI_glitches['searchedID'] = SI_glitches.loc[
            SI_glitches.event_time == gpstime, 'gravityspy_id'].iloc[0]
    else:
        SI_glitches['searchedID'] = gravityspy_id

    if gravityspy_id:
        SI_glitches['searchedzooID'] = SI_glitches.loc[
            SI_glitches.gravityspy_id == gravityspy_id,
            'links_subjects'].iloc[0]
    elif gpstime:
        SI_glitches['searchedzooID'] = SI_glitches.loc[
            SI_glitches.event_time == gpstime, 'links_subjects'].iloc[0]
    else:
        SI_glitches['searchedzooID'] = zooID

    if zooID:
        searched_image = features[numpy.argwhere(
            SI['links_subjects'].values == zooID)][0][0]
    else:
        searched_image = features[numpy.argwhere(
            SI['gravityspy_id'].values == gravityspy_id)][0][0]

    distances = []
    for feature in features:
        distances.append(1 - distance.cosine(searched_image, feature))

    tmp = pd.DataFrame({
        'distances': distances,
        'gravityspy_id': SI['gravityspy_id'].values
    })
    SI_glitches = SI_glitches.merge(tmp)

    return SI_glitches
Exemplo n.º 20
0
def get_gpstimes_json(name=''):
    return EventTable.fetch('gravityspy', 'similarityindex WHERE CAST(\"peakGPS\" AS TEXT) ~ \'{0}\' LIMIT 20'.format(name), columns=["peakGPS"]).to_pandas().astype(str).rename(columns={'peakGPS': 'value'}).to_json(orient='records')
Exemplo n.º 21
0
def get_imageid_json(name=''):
    return EventTable.fetch('gravityspy', 'similarityindex WHERE \"uniqueID\" ~ \'{0}\' LIMIT 20'.format(name), columns=["uniqueID"]).to_pandas().rename(columns={'uniqueID': 'value'}).to_json(orient='records')
Exemplo n.º 22
0
def get_zooid_json(name=''):
    return EventTable.fetch('gravityspy', 'similarity_index_o3 WHERE CAST(links_subjects AS TEXT) ~ \'{0}\' LIMIT 20'.format(name), columns=["links_subjects"], host='gravityspyplus.ciera.northwestern.edu').to_pandas().astype(str).rename(columns={'links_subjects': 'value'}).to_json(orient='records')
Exemplo n.º 23
0
def index(request):
    if request.user.is_authenticated:
        if request.user.id == 3:
            other_id = 37
        elif request.user.id == 37:
            other_id = 3
        else:
            other_id = -1

        image_to_be_displayed = EventTable.fetch(
            'gravityspy',
            'retired_images_for_testing WHERE \"gravityspy_id\" NOT IN (SELECT \"gravityspy_id\" FROM label_label WHERE user_id IN (40, {0}, {1})) ORDER BY RANDOM() LIMIT 1'
            .format(other_id, request.user.id),
            columns=[
                'url1', 'url2', 'url3', 'url4', 'gravityspy_id', 'ml_label'
            ],
            db='gravityspytools',
            passwd=os.getenv('GRAVITYSPYTOOLS_DATABASE_PASSWD'),
            user=os.getenv('GRAVITYSPYTOOLS_DATABASE_USER'),
            host='gravityspyplus.ciera.northwestern.edu')
        url1 = image_to_be_displayed['url1']
        url2 = image_to_be_displayed['url2']
        url3 = image_to_be_displayed['url3']
        url4 = image_to_be_displayed['url4']
        gravityspy_id = list(image_to_be_displayed['gravityspy_id'])[0]
        retired_label = str(list(image_to_be_displayed['ml_label'])[0])
        retired_label = convert_string_labels[retired_label]

        # Check if this image has already been seen by this user
        try:
            does_exist = Label.objects.get(gravityspy_id=gravityspy_id,
                                           user=request.user)
        except:
            does_exist = False

        # if it has redirect them to the page for a fresh image
        # until it is a new label for this user
        if does_exist:
            return redirect('/label/')
        else:
            if request.method == 'POST':
                form = LabelForm(request.POST)
                if form.is_valid():
                    label = str(form.cleaned_data['label'])
                    agreed = str(form.cleaned_data['agreed'])
                    gravityspy_id = str(form.cleaned_data['gravityspy_id'])
                    classification, created = Label.objects.get_or_create(
                        label=label,
                        agreed=agreed,
                        gravityspy_id=gravityspy_id,
                        user=request.user)
                    classification.save()
                    return redirect('/label/')
            else:
                form = LabelForm(
                    initial={
                        'label': retired_label,
                        'agreed': 'AGREE',
                        'gravityspy_id': gravityspy_id
                    })

        return render(
            request, 'home.html', {
                'form': form,
                'url1': list(url1)[0],
                'url2': list(url2)[0],
                'url3': list(url3)[0],
                'url4': list(url4)[0],
            })
    else:
        return redirect(make_authorization_url())