def join_histograms_from_files(histofiles, group_name = 'HIST', join_file = None, write_mode = 'w'): """Joins the histograms of a given list of histogram files. If possible, histograms with the same name will be added. Parameters ---------- histofiles : sequence List of strings with the filenames to be summed. group_name : string, optional Name of the histogram group in the file. join_file : string, optional If passed, saves the resulting HistoManager to this path. """ if not histofiles: raise ValueError("List of files is empty") final_histogram_manager = get_histograms_from_file(histofiles[0], group_name) for file in histofiles[1:]: added_histograms = get_histograms_from_file(file, group_name) final_histogram_manager = join_histo_managers (final_histogram_manager, added_histograms) if join_file is not None: save_histomanager_to_file(final_histogram_manager, join_file, mode=write_mode, group=group_name) return final_histogram_manager
def test_get_histograms_from_file(output_tmpdir, histogram_list): args, list_of_histograms = histogram_list histogram_manager1 = HistoManager(list_of_histograms) file_out = os.path.join(output_tmpdir, 'test_save_histogram_manager.h5') save_histomanager_to_file(histogram_manager1, file_out) histogram_manager2 = get_histograms_from_file(file_out) assert len(histogram_manager1.histos) == len(histogram_manager2.histos) for histoname in histogram_manager1.histos: assert_histogram_equality(histogram_manager1[histoname], histogram_manager2[histoname])
def test_save_histomanager_to_file_raises_ValueError(output_tmpdir, histogram_list, group, write_mode): args, list_of_histograms = histogram_list histogram_manager = HistoManager(list_of_histograms) file_out = os.path.join(output_tmpdir, 'test_save_histogram_manager.h5') with raises(ValueError): save_histomanager_to_file(histogram_manager, file_out, mode=write_mode, group=group)
def test_save_histomanager_to_file_append_mode(output_tmpdir, histogram_list, group): assume(not iskeyword(group)) args, list_of_histograms = histogram_list histogram_manager = HistoManager(list_of_histograms[:1]) file_out = os.path.join(output_tmpdir, 'test_save_histogram_manager.h5') save_histomanager_to_file(histogram_manager, file_out, mode='w', group=group) histogram_manager = HistoManager(list_of_histograms[1:]) save_histomanager_to_file(histogram_manager, file_out, mode='a', group=group) with tb.open_file(file_out, "r") as h5in: file_group = getattr(h5in.root, group) for histogram in list_of_histograms: histoname = histogram.title saved_labels = [ str(label)[2:-1].replace('\\\\', '\\') for label in getattr(file_group, histoname + "_labels")[:] ] saved_scales = [ str(label)[2:-1].replace('\\\\', '\\') for label in getattr(file_group, histoname + "_scales")[:] ] assert histoname in file_group assert len(histogram.bins) == len( getattr(file_group, histoname + "_bins")[:]) assert np.all( a == b for a, b in zip(histogram.bins, getattr(file_group, histoname + "_bins")[:])) assert np.allclose(histogram.data, getattr(file_group, histoname)[:]) assert np.allclose(histogram.errors, getattr(file_group, histoname + "_errors")[:]) assert np.allclose(histogram.out_range, getattr(file_group, histoname + "_outRange")[:]) assert histogram.labels == saved_labels assert histogram.scale == saved_scales
def test_join_histograms_from_file_and_write(output_tmpdir, histogram_list): _, list_of_histograms = histogram_list histogram_manager = HistoManager(list_of_histograms) file_out_test = os.path.join(output_tmpdir, 'test_save_histogram_manager_1.h5') save_histomanager_to_file(histogram_manager, file_out_test) file_out = os.path.join(output_tmpdir, 'test_join_histograms.h5') _ = histf.join_histograms_from_files([file_out_test, file_out_test], join_file=file_out) joined_histogram_manager1 = histf.get_histograms_from_file(file_out) joined_histogram_manager2 = histf.join_histo_managers( histogram_manager, histogram_manager) assert len(joined_histogram_manager1.histos) == len( joined_histogram_manager2.histos) for histoname in joined_histogram_manager1.histos: assert_histogram_equality(joined_histogram_manager1[histoname], joined_histogram_manager2[histoname])
def olivia(conf): files_in = os.path.expandvars(conf.files_in) file_out = os.path.expandvars(conf.file_out) detector_db = conf.detector_db run_number = int(conf.run_number) histo_config = os.path.expandvars(conf.histo_config) try: data_type = InputDataType[conf.data_type] except KeyError: print(f'Error: Data type {conf.data_type} is not recognized.') raise with open(histo_config) as config_file: config_dict = json.load(config_file) if data_type == InputDataType.rwf: histo_manager = monf.fill_rwf_histos(files_in, config_dict) elif data_type == InputDataType.pmaps: histo_manager = monf.fill_pmap_histos(files_in, detector_db, run_number, config_dict) save_histomanager_to_file(histo_manager, file_out)
def test_join_histograms_from_file(output_tmpdir, histogram_list1, histogram_list2): _, list_of_histograms1 = histogram_list1 _, list_of_histograms2 = histogram_list2 histogram_manager1 = HistoManager(list_of_histograms1) histogram_manager2 = HistoManager(list_of_histograms2) file_out1 = os.path.join(output_tmpdir, 'test_save_histogram_manager_1.h5') file_out2 = os.path.join(output_tmpdir, 'test_save_histogram_manager_2.h5') save_histomanager_to_file(histogram_manager1, file_out1) save_histomanager_to_file(histogram_manager2, file_out2) joined_histogram_manager1 = histf.join_histograms_from_files( [file_out1, file_out2]) joined_histogram_manager2 = histf.join_histo_managers( histogram_manager1, histogram_manager2) assert len(joined_histogram_manager1.histos) == len( joined_histogram_manager2.histos) for histoname in joined_histogram_manager1.histos: assert_histogram_equality(joined_histogram_manager1[histoname], joined_histogram_manager2[histoname])