def analyse(api_key, endpoint, doi, project_id, result_id, project_short_name, path, throttle, **kwargs): """Analyse Convert-a-Card results.""" e = enki.Enki(api_key, endpoint, project_short_name, all=1) result = enki.pbclient.find_results(project_id, id=result_id, limit=1, all=1)[0] df = helpers.get_task_run_df(e, result.task_id) df = df.loc[:, df.columns.isin(VALID_KEYS)] df = helpers.drop_empty_rows(df) n_task_runs = len(df.index) # Initialise the result defaults = {k: "" for k in df.keys()} result.info = helpers.init_result_info(doi, path, defaults) has_answers = not df.empty has_matches = helpers.has_n_matches(df, n_task_runs, MATCH_PERCENTAGE) # Matching answers if has_answers and has_matches: for k in df.keys(): result.info[k] = df[k].value_counts().idxmax() # Varied answers elif has_answers: result.info['analysis_complete'] = False enki.pbclient.update_result(result) time.sleep(throttle)
def analyse_selections(api_key, endpoint, project_id, result_id, path, doi, project_short_name, throttle, **kwargs): """Analyse In the Spotlight selection results.""" e = enki.Enki(api_key, endpoint, project_short_name, all=1) result = enki.pbclient.find_results(project_id, id=result_id, limit=1, all=1)[0] df = helpers.get_task_run_df(e, result.task_id) # Flatten annotations into a single list anno_list = df['info'].tolist() anno_list = list(itertools.chain.from_iterable(anno_list)) defaults = {'annotations': []} result.info = helpers.init_result_info(doi, path, defaults) clusters = [] comments = [] # Cluster similar regions for anno in anno_list: if anno['motivation'] == 'commenting': comments.append(anno) continue elif anno['motivation'] == 'tagging': r1 = get_rect_from_selection(anno) matched = False for cluster in clusters: r2 = get_rect_from_selection(cluster) overlap_ratio = get_overlap_ratio(r1, r2) if overlap_ratio > MERGE_RATIO: matched = True r3 = merge_rects(r1, r2) update_selector(cluster, r3) if not matched: update_selector(anno, r1) # still update to round rect params clusters.append(anno) else: # pragma: no cover raise ValueError('Unhandled motivation') result.info['annotations'] = clusters + comments enki.pbclient.update_result(result) time.sleep(throttle)
def test_path_added_to_result_info(self): path = '/example' info = helpers.init_result_info(None, path) assert info['analysis_path'] == path
def test_analysis_complete_true_added_to_result_info(self): info = helpers.init_result_info(None, None) assert info['analysis_complete']
def test_defaults_added_to_result_info(self): defaults = {'some_key': 'some_value'} info = helpers.init_result_info(None, None, defaults) assert info['some_key'] == 'some_value'
def test_doi_added_to_result_info(self): doi = '10.5281/zenodo.890858' info = helpers.init_result_info(doi, None) assert info['analysis_doi'] == doi