Skip to content

Commit

Permalink
Merge pull request #14 from kadrlica/mlab
Browse files Browse the repository at this point in the history
Replacing mlab.rec_append_field
  • Loading branch information
kadrlica authored May 16, 2018
2 parents edadab4 + f080ad3 commit 6f09dd7
Show file tree
Hide file tree
Showing 7 changed files with 63 additions and 9 deletions.
3 changes: 1 addition & 2 deletions ugali/analysis/search.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ugali/observation/catalog.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,6 @@
import numpy as np
import fitsio
import copy
from matplotlib import mlab

import ugali.utils.projector

Expand All @@ -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:

Expand Down
3 changes: 1 addition & 2 deletions ugali/preprocess/pixelize.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion ugali/simulation/analyzer.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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):
"""
Expand Down
5 changes: 3 additions & 2 deletions ugali/simulation/simulator.py
Original file line number Diff line number Diff line change
Expand Up @@ -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])
Expand Down Expand Up @@ -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':
Expand Down
55 changes: 55 additions & 0 deletions ugali/utils/mlab.py
Original file line number Diff line number Diff line change
@@ -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()
2 changes: 1 addition & 1 deletion ugali/utils/stats.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down

0 comments on commit 6f09dd7

Please sign in to comment.