Skip to content

Commit

Permalink
new structure to allow for contamination in mocks
Browse files Browse the repository at this point in the history
  • Loading branch information
forero committed Nov 25, 2016
1 parent 2fa851e commit 96a23a9
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 13 deletions.
18 changes: 11 additions & 7 deletions py/desitarget/mock/build.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,13 +277,14 @@ def targets_truth(params, output_dir):
# runs target selection on every mock
for source_name in sorted(source_defs.keys()):
target_name = params['sources'][source_name]['target_name'] #Target names
truth_name = params['sources'][source_name]['truth_name'] #name for the truth file
source_selection = params['sources'][source_name]['selection'] # criteria to make target selection
source_dict = params['sources'][source_name] # dictionary with sources info
source_data = source_data_all[source_name] # data

print('target_name {} : type: {} select: {}'.format(target_name, source_name, source_selection))
selection_function = source_selection + '_select'
result = getattr(mockselect, selection_function.lower())(source_data, target_name, source_name, brick_info = brick_info,
result = getattr(mockselect, selection_function.lower())(source_data, source_name, target_name, truth_name, brick_info = brick_info,
density_fluctuations = params['density_fluctuations'],
**source_dict)
target_mask_all[source_name] = result
Expand All @@ -304,6 +305,7 @@ def targets_truth(params, output_dir):
print('Collects information across mock files')
for source_name in sorted(source_defs.keys()):
target_name = params['sources'][source_name]['target_name']
truth_name = params['sources'][source_name]['truth_name']
source_data = source_data_all[source_name]
target_mask = target_mask_all[source_name]

Expand All @@ -323,8 +325,9 @@ def targets_truth(params, output_dir):

# define names that go into Truth
n = len(source_data['RA'][ii])
if target_name not in ['STD_FSTAR', 'SKY']:
if source_name not in ['STD_FSTAR', 'SKY']:
true_type_map = {
'STD_FSTAR': 'STAR',
'ELG': 'GALAXY',
'LRG': 'GALAXY',
'BGS': 'GALAXY',
Expand All @@ -337,7 +340,7 @@ def targets_truth(params, output_dir):
source_type = np.zeros(n, dtype='S10')
source_type[:] = target_name
true_type = np.zeros(n, dtype='S10')
true_type[:] = true_type_map[target_name]
true_type[:] = true_type_map[truth_name]


#define obsconditions
Expand All @@ -354,21 +357,21 @@ def targets_truth(params, output_dir):
source_obsconditions[:] = obsconditions.DARK|obsconditions.GRAY|obsconditions.BRIGHT

#append to the arrays that will go into Targets
if target_name in ['STD_FSTAR']:
if source_name in ['STD_FSTAR']:
ra_stars = source_data['RA'][ii].copy()
dec_stars = source_data['DEC'][ii].copy()
desi_target_stars = desi_target.copy()
bgs_target_stars = bgs_target.copy()
mws_target_stars = mws_target.copy()
obsconditions_stars = source_obsconditions.copy()
if target_name in ['SKY']:
if source_name in ['SKY']:
ra_sky = source_data['RA'][ii].copy()
dec_sky = source_data['DEC'][ii].copy()
desi_target_sky = desi_target.copy()
bgs_target_sky = bgs_target.copy()
mws_target_sky = mws_target.copy()
obsconditions_sky = source_obsconditions.copy()
if target_name not in ['SKY', 'STD_FSTAR']:
if source_name not in ['SKY', 'STD_FSTAR']:
ra_total = np.append(ra_total, source_data['RA'][ii])
dec_total = np.append(dec_total, source_data['DEC'][ii])
z_total = np.append(z_total, source_data['Z'][ii])
Expand All @@ -381,7 +384,8 @@ def targets_truth(params, output_dir):



print('{} {}: selected {} out of {}'.format(source_name, target_name, len(source_data['RA'][ii]), len(source_data['RA'])))
print('source {} target {} truth {}: selected {} out of {}'.format(
source_name, target_name, truth_name, len(source_data['RA'][ii]), len(source_data['RA'])))



Expand Down
20 changes: 14 additions & 6 deletions py/desitarget/mock/selection.py
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,7 @@
import warnings

############################################################
def mag_select(data, targetname, sourcename, brick_info=None, density_fluctuations=False, **kwargs):
def mag_select(data, sourcename, targetname, truthname, brick_info=None, density_fluctuations=False, **kwargs):
"""
Apply the selection function to determine the target class of each entry in
the input catalog.
Expand Down Expand Up @@ -258,7 +258,7 @@ def estimate_density(ra, dec, bounds=(170, 190, 0, 35)):
return density


def ndens_select(data, targetname, sourcename, brick_info = None, density_fluctuations = False, **kwargs):
def ndens_select(data, sourcename, targetname, truthname, brick_info = None, density_fluctuations = False, **kwargs):

"""Apply selection function based only on number density and redshift criteria.
Expand All @@ -271,7 +271,7 @@ def ndens_select(data, targetname, sourcename, brick_info = None, density_fluctu
if ('min_z' in kwargs) & ('max_z' in kwargs):
in_z = ((z>=kwargs['min_z']) & (z<=kwargs['max_z']))
else:
in_z = z>0.0
in_z = z>=0.0

# if we don't have a mean NTARGET in the input file, we fall back to constant mean values from the config file
constant_density = False
Expand All @@ -283,11 +283,17 @@ def ndens_select(data, targetname, sourcename, brick_info = None, density_fluctu
mean_density = kwargs['density']
constant_density = True


try:
global_density = kwargs['global_density']
except:
global_density = False

n = len(ra)
target_class = np.zeros(n,dtype=np.int64) - 1
keepornot = np.random.uniform(0.,1.,n)

if density_fluctuations and constant_density == False:
if density_fluctuations and constant_density == False and global_density == False:
bricks = desispec.brick.brickname(ra, dec)
unique_bricks = list(set(bricks))

Expand All @@ -310,6 +316,7 @@ def ndens_select(data, targetname, sourcename, brick_info = None, density_fluctu

mock_dens = n_in_brick/brick_area

print('in brick mock density {} - desired num density {}'.format(mock_dens, num_density))
frac_keep = num_density/mock_dens
if(frac_keep>1.0):
warnings.warn("target {}: frac_keep>1.0.: frac_keep={} ".format(sourcename, frac_keep), RuntimeWarning)
Expand All @@ -322,7 +329,7 @@ def ndens_select(data, targetname, sourcename, brick_info = None, density_fluctu
# print('len kept {}'.format(np.count_nonzero(select_sample)))
target_class[select_sample] = desi_mask.mask(targetname)
else:
print('No Fluctuations for this target')
print('No Fluctuations for this target {}'.format(sourcename))
try:
bounds = kwargs['min_ra'], kwargs['max_ra'], kwargs['min_dec'], kwargs['max_dec']
mock_dens = estimate_density(ra[in_z], dec[in_z], bounds=bounds)
Expand All @@ -331,9 +338,10 @@ def ndens_select(data, targetname, sourcename, brick_info = None, density_fluctu

num_density = mean_density

print('mock density {} - desired num density {}'.format(mock_dens, num_density))
frac_keep = num_density/mock_dens
if(frac_keep>1.0):
warnings.warn("frac_keep>1.0.: frac_keep={} ".format(frac_keep), RuntimeWarning)
warnings.warn("target {} frac_keep>1.0.: frac_keep={} ".format(sourcename, frac_keep), RuntimeWarning)
# print('num density desired {}, num density in mock {}, frac_keep {}'.format(num_density, mock_num_density, frac_keep))
kept = keepornot < frac_keep

Expand Down

0 comments on commit 96a23a9

Please sign in to comment.