def test_hist_addition(): bins = [-3, -2, -1, 0, 1, 2, 3] bounds = IntervalSet.from_list_of_edges(bins) contents = np.ones(len(bins) - 1) hh1 = Histogram(bounds, contents, is_poisson=True) hh2 = hh1+hh1 assert np.all(hh2.contents == 2*hh1.contents) hh3 = Histogram(bounds, contents, errors=contents) hh4 = hh3 + hh3 with pytest.raises(AssertionError): hh3 + hh1 hh5 = Histogram(bounds, contents, errors=contents, sys_errors=contents) hh6 = hh5 +hh5 hh7 = hh5 + hh3 hh8 = hh3 + hh5
def from_numpy_histogram(cls,hist,errors=None,sys_errors=None,is_poisson=False,**kwargs): """ create a Histogram from a numpy histogram. Example: r = np.random.randn(1000) np_hist = np.histogram(r) hist = Histogram.from_numpy_histogram(np_hist) :param hist: a np.histogram tuple :param errors: list of errors for each bin in the numpy histogram :param sys_errors: list of systematic errors for each bin in the numpy histogram :param is_poisson: if the data is Poisson distributed or not :param kwargs: any kwargs to pass along :return: a Histogram object """ # extract the contents and the bin edges contents = hist[0] #type: np.ndarray edges = hist[1] #type: np.ndarray # convert the edges to an interval set bounds = IntervalSet.from_list_of_edges(edges) #type: IntervalSet # return a histogram return cls(list_of_intervals=bounds, contents=contents, errors=errors, sys_errors=sys_errors, is_poisson=is_poisson, **kwargs)
def _calculate_fwhm(self): """ calculate the FWHM of the filters :return: """ wmin = [] wmax = [] # go through each filter # and find the non-gaussian FWHM bounds for filter in self._filters: response = filter.response max_response = response.max() idx_max = response.argmax() half_max = 0.5 * max_response idx1 = abs(response[:idx_max] - half_max).argmin() idx2 = abs(response[idx_max:] - half_max).argmin() + idx_max # have to grab the private member here # bc the library does not expose it! w1 = filter._wavelength[idx1] w2 = filter._wavelength[idx2] wmin.append(w1) wmax.append(w2) self._wavebounds = IntervalSet.from_starts_and_stops(wmin, wmax)
def test_hist_addition(): bins = [-3, -2, -1, 0, 1, 2, 3] bounds = IntervalSet.from_list_of_edges(bins) contents = np.ones(len(bins) - 1) hh1 = Histogram(bounds, contents, is_poisson=True) hh2 = hh1 + hh1 assert np.all(hh2.contents == 2 * hh1.contents) hh3 = Histogram(bounds, contents, errors=contents) hh4 = hh3 + hh3 with pytest.raises(AssertionError): hh3 + hh1 hh5 = Histogram(bounds, contents, errors=contents, sys_errors=contents) hh6 = hh5 + hh5 hh7 = hh5 + hh3 hh8 = hh3 + hh5
def test_hist_constructor(): with within_directory(__this_dir__): bins=[-3,-2,-1,0,1,2,3] bounds = IntervalSet.from_list_of_edges(bins) contents = np.ones(len(bins) - 1) hh1 = Histogram(bounds, contents, is_poisson=True) assert hh1.is_poisson == True assert len(hh1) == len(bins)-1 hh1.display() # rnum = np.loadtxt('test_hist_data.txt') # # # #rnum = np.random.randn(1000) # hrnum = np.histogram(rnum, bins=bins, normed=False) # hh2 = Histogram.from_numpy_histogram(hrnum, is_poisson=True) # hh3 = Histogram.from_entries(bounds,rnum) # # assert hh2==hh3 hh4 = Histogram(bounds, contents, errors=contents) assert hh4.is_poisson == False with pytest.raises(AssertionError): hh4 = Histogram(bounds, contents, errors=contents,is_poisson=True)
def _calculate_fwhm(self): """ calculate the FWHM of the filters :return: """ wmin = [] wmax = [] # go through each filter # and find the non-gaussian FWHM bounds for filter in self._filters: response = filter.response max_response = response.max() idx_max = response.argmax() half_max = 0.5 * max_response idx1 = abs(response[:idx_max] - half_max).argmin() idx2 = abs(response[idx_max:] - half_max).argmin() + idx_max # have to grab the private member here # bc the library does not expose it! w1 = filter._wavelength[idx1] w2 = filter._wavelength[idx2] wmin.append(w1) wmax.append(w2) self._wavebounds = IntervalSet.from_starts_and_stops(wmin,wmax)
def test_hist_constructor(): with within_directory(__this_dir__): bins = [-3, -2, -1, 0, 1, 2, 3] bounds = IntervalSet.from_list_of_edges(bins) contents = np.ones(len(bins) - 1) hh1 = Histogram(bounds, contents, is_poisson=True) assert hh1.is_poisson == True assert len(hh1) == len(bins) - 1 hh1.display() # rnum = np.loadtxt('test_hist_data.txt') # # # #rnum = np.random.randn(1000) # hrnum = np.histogram(rnum, bins=bins, normed=False) # hh2 = Histogram.from_numpy_histogram(hrnum, is_poisson=True) # hh3 = Histogram.from_entries(bounds,rnum) # # assert hh2==hh3 hh4 = Histogram(bounds, contents, errors=contents) assert hh4.is_poisson == False with pytest.raises(AssertionError): hh4 = Histogram(bounds, contents, errors=contents, is_poisson=True)