From f080ad366f55da361b257ced64e603a1139703f7 Mon Sep 17 00:00:00 2001 From: Alex Drlica-Wagner Date: Tue, 15 May 2018 19:55:58 -0500 Subject: [PATCH] Replacing mlab.rec_append_field --- ugali/analysis/search.py | 3 +- ugali/observation/catalog.py | 2 +- ugali/preprocess/pixelize.py | 3 +- ugali/simulation/analyzer.py | 2 +- ugali/simulation/simulator.py | 5 ++-- ugali/utils/mlab.py | 55 +++++++++++++++++++++++++++++++++++ ugali/utils/stats.py | 2 +- 7 files changed, 63 insertions(+), 9 deletions(-) create mode 100644 ugali/utils/mlab.py diff --git a/ugali/analysis/search.py b/ugali/analysis/search.py index 1eb5756..fd962ac 100644 --- a/ugali/analysis/search.py +++ b/ugali/analysis/search.py @@ -14,13 +14,12 @@ import numpy import numpy.lib.recfunctions as recfuncs import scipy.ndimage as ndimage -from matplotlib import mlab from ugali.utils.shell import mkdir, which from ugali.utils.logger import logger from ugali.utils.binning import reverseHistogram from ugali.utils.projector import Projector,gal2cel,cel2gal,dec2hms,dec2dms,mod2dist -from ugali.utils import healpix +from ugali.utils import healpix, mlab from ugali.utils.healpix import pix2ang, ang2pix from ugali.candidate.associate import SourceCatalog, catalogFactory from ugali.utils.config import Config diff --git a/ugali/observation/catalog.py b/ugali/observation/catalog.py index a3425f3..33c57e9 100644 --- a/ugali/observation/catalog.py +++ b/ugali/observation/catalog.py @@ -4,7 +4,6 @@ import numpy as np import fitsio import copy -from matplotlib import mlab import ugali.utils.projector @@ -13,6 +12,7 @@ from ugali.utils.healpix import ang2pix,superpixel from ugali.utils.logger import logger from ugali.utils.fileio import load_infiles +from ugali.utils import mlab class Catalog: diff --git a/ugali/preprocess/pixelize.py b/ugali/preprocess/pixelize.py index 237ae58..320b258 100644 --- a/ugali/preprocess/pixelize.py +++ b/ugali/preprocess/pixelize.py @@ -14,12 +14,11 @@ import numpy as np import numpy.lib.recfunctions as recfuncs import healpy as hp -from matplotlib import mlab #import ugali.utils.binning #import ugali.utils.skymap from ugali.utils.projector import cel2gal, gal2cel -from ugali.utils import healpix +from ugali.utils import healpix, mlab from ugali.utils.healpix import ang2pix, pix2ang, superpixel from ugali.utils.shell import mkdir diff --git a/ugali/simulation/analyzer.py b/ugali/simulation/analyzer.py index db4dfbb..c2a92a4 100755 --- a/ugali/simulation/analyzer.py +++ b/ugali/simulation/analyzer.py @@ -12,7 +12,6 @@ import healpy import numpy.lib.recfunctions as recfuncs import fitsio -from matplotlib import mlab import ugali.observation.catalog import ugali.observation.mask @@ -25,6 +24,7 @@ from ugali.utils.healpix import ang2pix, pix2ang from ugali.utils.logger import logger from ugali.utils.config import Config +from ugali.utils import mlab class Analyzer(object): """ diff --git a/ugali/simulation/simulator.py b/ugali/simulation/simulator.py index d5cc366..71d0da7 100755 --- a/ugali/simulation/simulator.py +++ b/ugali/simulation/simulator.py @@ -389,7 +389,7 @@ def toy_background(self,mc_source_id=2,seed=None): mc_source_id = mc_source_id * np.ones(len(mag_1)) select = (mag_lim_1>mag_1)&(mag_lim_2>mag_2) - + hdu = ugali.observation.catalog.makeHDU(self.config,mag_1[select],mag_err_1[select], mag_2[select],mag_err_2[select], lon[select],lat[select],mc_source_id[select]) @@ -629,7 +629,8 @@ def makeHDU(self, mag_1, mag_err_1, mag_2, mag_err_2, lon, lat, mc_source_id): """ Create a catalog fits file object based on input data. - ADW: This should be combined with the write_membership function of loglike. + ADW: This should be combined with the write_membership + function of loglike. """ if self.config['catalog']['coordsys'].lower() == 'cel' \ and self.config['coords']['coordsys'].lower() == 'gal': diff --git a/ugali/utils/mlab.py b/ugali/utils/mlab.py new file mode 100644 index 0000000..e4c912b --- /dev/null +++ b/ugali/utils/mlab.py @@ -0,0 +1,55 @@ +#!/usr/bin/env python +""" +Transplanted from matplotlib.mlab +""" + +import six +import numpy as np + +def iterable(obj): + """return true if *obj* is iterable""" + try: + iter(obj) + except TypeError: + return False + return True + +def rec_append_fields(rec, names, arrs, dtypes=None): + """ + Return a new record array with field names populated with data + from arrays in *arrs*. If appending a single field, then *names*, + *arrs* and *dtypes* do not have to be lists. They can just be the + values themselves. + """ + if (not isinstance(names, six.string_types) and iterable(names) + and len(names) and isinstance(names[0], six.string_types)): + if len(names) != len(arrs): + raise ValueError("number of arrays do not match number of names") + else: # we have only 1 name and 1 array + names = [names] + arrs = [arrs] + arrs = list(map(np.asarray, arrs)) + if dtypes is None: + dtypes = [a.dtype for a in arrs] + elif not iterable(dtypes): + dtypes = [dtypes] + if len(arrs) != len(dtypes): + if len(dtypes) == 1: + dtypes = dtypes * len(arrs) + else: + raise ValueError("dtypes must be None, a single dtype or a list") + old_dtypes = rec.dtype.descr + if six.PY2: + old_dtypes = [(name.encode('utf-8'), dt) for name, dt in old_dtypes] + newdtype = np.dtype(old_dtypes + list(zip(names, dtypes))) + newrec = np.recarray(rec.shape, dtype=newdtype) + for field in rec.dtype.fields: + newrec[field] = rec[field] + for name, arr in zip(names, arrs): + newrec[name] = arr + return newrec + +if __name__ == "__main__": + import argparse + parser = argparse.ArgumentParser(description=__doc__) + args = parser.parse_args() diff --git a/ugali/utils/stats.py b/ugali/utils/stats.py index 857a0f9..5930e01 100644 --- a/ugali/utils/stats.py +++ b/ugali/utils/stats.py @@ -7,12 +7,12 @@ import numpy as np import numpy.lib.recfunctions as recfuncs -from matplotlib import mlab import scipy.special import scipy.stats # These should probably live in this file from ugali.utils.bayesian_efficiency import bayesianInterval, binomialInterval +from ugali.utils import mlab _alpha = 0.32 _nbins = 300