def get_context_data(self, **kwargs): # Call the base implementation first to get the context context = super(CompoundListView, self).get_context_data(**kwargs) # add data from the DataFrame to the context comp_tracer_list_df = qs2df.get_compound_synonym_list_df() # convert DataFrame to a list of dictionary data = qs2df.df_to_list_of_dict(comp_tracer_list_df) context["df"] = data return context
def get_context_data(self, **kwargs): # Call the base implementation first to get the context context = super(StudyListView, self).get_context_data(**kwargs) # add data from the DataFrame to the context stud_list_stats_df = qs2df.get_study_list_stats_df() # convert DataFrame to a list of dictionary data = qs2df.df_to_list_of_dict(stud_list_stats_df) context["df"] = data return context
def get_context_data(self, **kwargs): # Call the base implementation first to get the context context = super(SampleListView, self).get_context_data(**kwargs) # add data from the DataFrame to the context all_anim_msrun_df = qs2df.get_animal_msrun_all_df() # convert DataFrame to a list of dictionary data = qs2df.df_to_list_of_dict(all_anim_msrun_df) context["df"] = data return context
def study_summary(request): """ function-based view for studies based summary data, including selected data fileds for animal, tissue, sample, and MSRun get DataFrame for summary data, then convert to JSON format """ all_stud_msrun_df = qs2df.get_study_msrun_all_df() # convert DataFrame to a list of dictionary data = qs2df.df_to_list_of_dict(all_stud_msrun_df) context = {"df": data} return render(request, "DataRepo/study_summary.html", context)
def get_context_data(self, **kwargs): # Call the base implementation first to get the context context = super(StudyDetailView, self).get_context_data(**kwargs) pk = self.kwargs.get("pk") per_stud_msrun_df = qs2df().get_per_study_msrun_df(pk) per_stud_stat_df = qs2df().get_per_study_stat_df(pk) # convert DataFrame to a list of dictionary data = qs2df.df_to_list_of_dict(per_stud_msrun_df) stats_data = qs2df.df_to_list_of_dict(per_stud_stat_df) context["df"] = data context["stats_df"] = stats_data return context
def get_context_data(self, **kwargs): # Call the base implementation first to get the context context = super(CompoundDetailView, self).get_context_data(**kwargs) # add data from the DataFrame to the context anim_list_stats_df = qs2df.get_animal_list_stats_df() pk = self.kwargs.get("pk") per_tracer_anim_list_stats_df = anim_list_stats_df[ anim_list_stats_df["tracer_compound_id"] == pk] # convert DataFrame to a list of dictionary tracer_data = qs2df.df_to_list_of_dict(per_tracer_anim_list_stats_df) context["tracer_df"] = tracer_data context["measured"] = ( PeakGroup.objects.filter(compounds__id__exact=pk).count() > 0) return context
def test_study_list_stat_df(self): """ get data from the data frame for selected study with selected columns, then convert the data to dictionary to compare with the example data. """ example_study_dict = self.get_example_study_dict() study_list_stats_df = qs2df.get_study_list_stats_df() stud1_list_stats_df = study_list_stats_df[ study_list_stats_df["study"] == "obob_fasted" ] selected_columns = list(example_study_dict.keys()) stud1_list_stats_dict = stud1_list_stats_df[selected_columns].loc[0].to_dict() self.assertEqual(stud1_list_stats_dict, example_study_dict)
def test_animal_list_stat_df(self): """ get data from the data frame for selected animal with selected columns, then convert the data to dictionary to compare with the example data. test studies as an unordered list """ example_animal_dict = self.get_example_animal_dict() example_studies = self.get_example_study_list() anim_list_stats_df = qs2df.get_animal_list_stats_df() anim1_list_stats_df = anim_list_stats_df[anim_list_stats_df["animal"] == "971"] selected_columns = list(example_animal_dict.keys()) anim1_list_stats_dict = anim1_list_stats_df[selected_columns].iloc[0].to_dict() studies = anim1_list_stats_df["studies"].iloc[0].tolist() self.assertEqual(anim1_list_stats_dict, example_animal_dict) self.assertEqual(len(studies), len(example_studies)) self.assertEqual(any(studies), any(example_studies))
def test_comp_tracer_list_df(self): """ get data from the data frame for selected compound with selected columns, then convert the data to dictionary to compare with the example data. """ example_compound_dict = self.get_example_compound_dict() comp_tracer_list_df = qs2df.get_compound_synonym_list_df() comp1_tracer_list_df = comp_tracer_list_df[ comp_tracer_list_df["compound_name"] == "lysine" ] selected_columns = list(example_compound_dict.keys()) comp1_tracer_dict = comp1_tracer_list_df[selected_columns].iloc[0].to_dict() self.assertEqual(comp1_tracer_dict, example_compound_dict) # check synonym values expected_comp_symnonym_list = ["Lysine", "lys", "lysine"] comp_symnonym_in_df = comp1_tracer_list_df.iloc[0].synonyms.tolist() self.assertEqual(len(comp_symnonym_in_df), len(expected_comp_symnonym_list)) self.assertEqual(any(comp_symnonym_in_df), any(comp_symnonym_in_df))
def test_animal_sample_msrun_df(self): """ get data from the data frame for selected sample with selected columns, then convert the data to dictionary to compare with the example data. test studies as an unordered list """ # get data for examples example_sample1_dict = self.get_example_sample1_dict() example_sample2_dict = self.get_example_sample2_dict() example_studies = self.get_example_study_list() anim_msrun_df = qs2df.get_animal_msrun_all_df() # sample1 test sam1_msrun_df = anim_msrun_df[anim_msrun_df["sample"] == "BAT-xz971"] sam1_columns = list(example_sample1_dict.keys()) sam1_msrun_dict = sam1_msrun_df[sam1_columns].iloc[0].to_dict() sam1_studies = sam1_msrun_df["studies"].iloc[0].tolist() self.assertEqual(sam1_msrun_dict, example_sample1_dict) self.assertEqual(len(sam1_studies), len(example_studies)) self.assertEqual(any(sam1_studies), any(example_studies)) # sample2 test sam2_msrun_df = anim_msrun_df[anim_msrun_df["sample"] == "Br-t1-no-tracer-age"] sam2_columns = list(example_sample2_dict.keys()) sam2_msrun_sel_dict = sam2_msrun_df[sam2_columns].iloc[0].to_dict() sam2_msrun_all_dict = sam2_msrun_df.iloc[0].to_dict() self.assertEqual(sam2_msrun_sel_dict, example_sample2_dict) # test values for age and sample_time_collected # expected values expected_sam2_age_week = 6.0 expected_sam2_time_collected_mins = 150.0 # verify the values from the Dataframe sam2_age_to_week = (sam2_msrun_all_dict["age"]).days // 7 # age: timedelta64[ns] type in DataFrame vs. isoformat in json output sam2_msrun_df_json = sam2_msrun_df.to_json( orient="records", date_format="iso", date_unit="ns" ) sam2_msrun_df_json_data = json.loads(sam2_msrun_df_json) sam2_age_in_json_data = sam2_msrun_df_json_data[0]["age"] sam2_age_in_json_data_to_weeks = ( dateparse.parse_duration(sam2_age_in_json_data).days // 7 ) self.assertEqual(sam2_age_to_week, expected_sam2_age_week) self.assertEqual(sam2_age_in_json_data_to_weeks, expected_sam2_age_week) # sample_time_collected: timedelta64[ns] type in DataFrame vs. isoformat in json output sam2_time_collected_to_mins = ( sam2_msrun_all_dict["sample_time_collected"] ).seconds // 60 sam2_time_collected_in_json_data = sam2_msrun_df_json_data[0][ "sample_time_collected" ] sam2_time_collected_in_json_data_to_mins = ( dateparse.parse_duration(sam2_time_collected_in_json_data).seconds // 60 ) self.assertEqual(sam2_time_collected_to_mins, expected_sam2_time_collected_mins) self.assertEqual( sam2_time_collected_in_json_data_to_mins, expected_sam2_time_collected_mins ) # sample2 has no tracer and MSRun data self.assertTrue(sam2_msrun_all_dict["tracer"] is pd.NA) self.assertTrue(sam2_msrun_all_dict["msrun_id"] is pd.NA) self.assertTrue(sam2_msrun_all_dict["msrun_owner"] is pd.NA)