def test_plot_two_params(self): x = plot_drc_params(self.fit_params, fit_param='ic50', fit_param_compare='ec50', fit_param_sort='ec25') assert isinstance(plotly_to_dataframe(x), pd.DataFrame)
def test_plot_dip_params_aggregation(self): assert isinstance( plotly_to_dataframe( plot_drc_params( self.fit_params, aggregate_cell_lines={'tag': ['BT20', 'HCC1143']}, aggregate_drugs={'tag': ['abemaciclib', 'Panobinostat']}, fit_param='ic50')), pd.DataFrame)
def test_plot_plate_map(self): plate_data = self.dataset.plate('HTS007_149-28A', include_dip_rates=True) res = plot_plate_map(plate_data) assert isinstance(plotly_to_dataframe(res), pd.DataFrame)
def test_plot_ctrl_dip_by_plate(self): res = plot_ctrl_dip_by_plate(self.ctrl_dip_data) assert isinstance(plotly_to_dataframe(res), pd.DataFrame)
def test_plot_dip_params_single_param(self): assert isinstance( plotly_to_dataframe( plot_drc_params(self.fit_params, fit_param='ic50')), pd.DataFrame)
def test_plot_dip_single_curve(self): fit_data = self.fit_params.iloc[[0], :] assert isinstance(plotly_to_dataframe(plot_drc(fit_data)), pd.DataFrame)
def test_plot_dip(self): assert isinstance(plotly_to_dataframe(plot_drc(self.fit_params)), pd.DataFrame)
def test_plot_time_course(self): abe_bt20 = self.dataset.filter(drugs=['abemaciclib'], cell_lines=['BT20']) tc = plot_time_course(abe_bt20, show_dip_fit=True, log_yaxis=True) assert isinstance(plotly_to_dataframe(tc), pd.DataFrame)
def test_plot_viability_curves(self): assert isinstance(plotly_to_dataframe(plot_drc(self.viability_params)), pd.DataFrame)
def ajax_get_plot(request, file_type='json'): if file_type == 'csv': permission_required = 'download_data' else: permission_required = 'view_plots' try: plot_type = request.GET['plotType'] template = request.GET.get('theme', default_plotly_template) if template not in ALLOWED_TEMPLATES: return HttpResponse('Please select an allowed template', status=400) dataset_id = int(request.GET['datasetId']) dataset2_id = request.GET.get('dataset2Id', None) if dataset2_id == "": dataset2_id = None if dataset2_id is not None: dataset2_id = int(dataset2_id) cell_line_id = request.GET.getlist('c') drug_id = request.GET.getlist('d') cell_line_id = [int(cl) for cl in cell_line_id] drug_ids = [] for dr in drug_id: try: drug_ids.append(int(dr)) except ValueError: drug_ids.append([int(d) for d in dr.split(",")]) drug_id = drug_ids assay = request.GET.get('assayId') yaxis = request.GET.get('logTransform', 'None') except (KeyError, ValueError): raise Http404() try: dataset = HTSDataset.objects.get(pk=dataset_id) except HTSDataset.DoesNotExist: raise Http404() _assert_has_perm(request, dataset, permission_required) if not license_accepted(request, dataset): return HttpResponse(LICENSE_UNSIGNED.format(dataset.name), status=400) if plot_type == 'tc': if len(drug_id) != 1 or len(cell_line_id) != 1: return HttpResponse( 'Please select exactly one cell line and ' 'drug for time course plot', status=400) try: df_data = df_doses_assays_controls(dataset=dataset, drug_id=drug_id, cell_line_id=cell_line_id, assay=assay) except NoDataException: return HttpResponse( 'No data found for this request. This ' 'drug/cell line/assay combination may not ' 'exist.', status=400) if assay is None: assay = df_data.assays.index.get_level_values('assay')[0] overlay_dip_fit = request.GET.get('overlayDipFit', 'false') == 'true' if overlay_dip_fit and assay != df_data.dip_assay_name: return HttpResponse( 'Can only overlay DIP rate on cell ' 'proliferation assays', status=400) plot_fig = plot_time_course(df_data, log_yaxis=yaxis == 'log2', assay_name=assay, show_dip_fit=overlay_dip_fit, subtitle=dataset.name, template=template) elif plot_type in ('drc', 'drpar'): if all(isinstance(d, int) for d in drug_id): plot_fig = _dose_response_plot(request, dataset, dataset2_id, permission_required, drug_id, cell_line_id, plot_type, template) else: if dataset2_id is not None: return HttpResponse( 'Please select a single dataset at a time to view drug ' 'combination heat plots', status=400) plot_fig = _drug_combination_heatmap(request, dataset, drug_id, cell_line_id, template) if isinstance(plot_fig, HttpResponse): return plot_fig elif plot_type == 'qc': qc_view = request.GET.get('qcView', None) if qc_view == 'ctrldipbox': ctrl_dip_data = df_ctrl_dip_rates(dataset_id) if ctrl_dip_data is None: return HttpResponse( 'No control wells with DIP rates ' 'available were detected in this ' 'dataset.', status=400) plot_fig = plot_ctrl_dip_by_plate(ctrl_dip_data, template=template) elif qc_view == 'ctrlcellbox': # Try to fetch from cache cache_key = f'dataset_{dataset_id}_plot_ctrlcellbox' cur_plotver = 1 plot_cached = cache.get(cache_key) if plot_cached: plot_fig = plot_cached['plot_fig'] if plot_cached is None or plot_cached['dataset_last_modified'] < dataset.modified_date or \ plot_cached['plot_version'] < cur_plotver: # Create plot groupings = dataset_groupings(dataset) if not groupings['singleTimepoint']: return HttpResponse( 'This plot type is only available for ' 'single time-point datasets', status=400) try: df_data = df_control_wells(dataset_id=dataset, assay=assay) except NoDataException: return HttpResponse('No data found for this request.', status=400) if (df_data['value'] == 100.0).all(): return HttpResponse( 'The raw data for this dataset is given as relative viability, so no control ' 'wells are available', status=400) plot_fig = plot_ctrl_cell_counts_by_plate( df_data, subtitle=dataset.name, template=template) # Push to cache cache.set( cache_key, { 'dataset_last_modified': dataset.modified_date, 'plot_version': cur_plotver, 'plot_fig': plot_fig }) elif qc_view == 'dipplatemap': plate_id = request.GET.get('plateId', None) try: plate_id = int(plate_id) except ValueError: return HttpResponse('Integer plateId required', status=400) pl_data = ajax_load_plate(request, plate_id, return_as_platedata=True, use_names=True) plot_fig = plot_plate_map(pl_data, color_by='dip_rates', template=template) else: return HttpResponse('Unimplemented QC view: {}'.format(qc_view), status=400) else: return HttpResponse('Unimplemented plot type: %s' % plot_type, status=400) as_attachment = request.GET.get('download', '0') == '1' if file_type == 'json': j = json.dumps(plot_fig, cls=PlotlyJSONEncoder) response = HttpResponse(j, content_type='application/json') elif file_type == 'csv': response = HttpResponse(plotly_to_dataframe(plot_fig).to_csv(), content_type='text/csv') elif file_type == 'html': template = 'plotly_plot{}.html'.format( '_standalone' if as_attachment else '') context = { 'data': json.dumps(plot_fig, cls=PlotlyJSONEncoder), 'page_title': strip_tags(plot_fig['layout']['title']['text']) } if as_attachment: context['plotlyjs'] = get_plotlyjs() response = render(request, template, context) else: return HttpResponse('Unknown file type: %s' % file_type, status=400) if as_attachment: try: title = plot_fig['layout']['title']['text'] except KeyError: title = 'Plot' response['Content-Disposition'] = \ 'attachment; filename="{}.{}"'.format(strip_tags(title), file_type) return response