def addtrans(raw, y, label, plot): """ Add little triangles marking where the transits are. """ if raw.dtype.names.count('finj') == 1: finjkw = dict(color='m', mew=2, marker=7, ms=5, lw=0, mfc='none') if label == True: finjkw['label'] = 'Injected Transits' ym = ma.masked_array(y, raw['finj'] > -1e-6) t = raw['t'] sL = ma.clump_unmasked(ym) id = [s.start for s in ma.clump_unmasked(ym)] plot(t[id], ym[id] + 0.001, **finjkw)
def adapt_mask_to_roi(consmask, roifp, chrom): """ :param consmask: :param roifp: :param chrom: :return: """ roimask = np.ones(consmask.size, dtype=np.bool) opn, mode = text_file_mode(roifp) chrom_found = False with opn(roifp, mode) as rois: for line in rois: if not line or line.startswith('#'): continue cols = line.strip().split() if cols[0] != chrom: continue chrom_found = True roimask[int(cols[1]):int(cols[2])] = 0 comb_mask = consmask | roimask # 0 | 0 = 0 assert not comb_mask.sum() == consmask.size,\ 'All positions on chromosome {} masked - ' \ 'any regions for that chromosome in ROI file: {}'.format(chrom, chrom_found) pos = np.arange(consmask.size) pos = msk.array(pos, mask=comb_mask) select = np.array([k for k, g in itt.groupby(~comb_mask)], dtype=np.bool) splits = np.array(list( itt.chain.from_iterable( (item.start, item.stop) for item in msk.clump_unmasked(pos))), dtype=np.int32) return splits, select
def wet_spells(rainfall, threshold=0, start=True, mode='both'): """ Compute the wet spells for a series of precipitations Parameters ---------- rainfall : TimeSeries TimeSeries of precipitations. threshold : float, optional Minimum amount of precipitation defining a wet day. start : boolean, optional Whether the spells are associated with the first or last day of the spell. mode : {'durations', 'intensities', 'all'} Whether to return only durations, intensities or both. Returns ------- wet_spells : TimeSeries A :class:`TimeSeries` giving the duration and/or intensity of a spell at either the first or last date. """ rdates = getattr(rainfall, 'dates', None) rdata = ma.masked_array(rainfall, subok=False) condition = ma.masked_less(rdata, threshold) slices = ma.clump_unmasked(condition) # Get the durations and starting dates of each spell mode = (mode or 'both').lower()[0] if mode == 'd': # Durations result = np.array([s.stop - s.start for s in slices], dtype=int) elif mode == 'i': # Intensities result = np.array([rdata[s].sum() for s in slices]) else: durations = [s.stop - s.start for s in slices] intensities = [rdata[s].sum() for s in slices] result = np.array(zip(durations, intensities), dtype=[('durations', int), ('intensities', float)],) if rdates is None: return result if start: dates = rdates[[s.start for s in slices]] else: dates = rdates[[s.stop - 1 for s in slices]] ensoi = getattr(rainfall, 'ensoindicator', None) if mode in 'id': spells = enso.climate_series(result, dates=dates, ensoindicator=ensoi) else: spells = enso.climate_records(result, dates=dates, ensoindicator=ensoi) return spells
def process_alignments(parameter_set): chrom, chrom_size, mapq_low, mapq_high, quantifier, assembly_threshold, dump_type, dump_counts, aln_store, aln_keys = parameter_set if quantifier in ['all', 'any']: bool_mask = build_boolean_mask( chrom, chrom_size, mapq_low, mapq_high, quantifier, aln_store, aln_keys ) if dump_type == 'alignments': # the mask is build by iterating over / masking # alignment regions, so need to _invert_ # the mask to mask out all unaligned # (unselected) regions genomic_coordinates = ma.masked_array(np.arange(chrom_size, dtype=np.int32), mask=~bool_mask) elif dump_type == 'inverse': # mask can be used as-is, all unaligned # (unselected) regions will be dumped genomic_coordinates = ma.masked_array(np.arange(chrom_size, dtype=np.int32), mask=bool_mask) else: raise ValueError('Unexpected value for "dump_type": {}'.format(dump_type)) elif quantifier in ['lt', 'geq']: quant_mask = build_quantitative_mask( chrom, chrom_size, mapq_low, mapq_high, quantifier, assembly_threshold, dump_counts, aln_store, aln_keys ) # negating mask: see reason above genomic_coordinates = ma.masked_array(np.arange(chrom_size, dtype=np.int32), mask=~quant_mask) else: raise ValueError('Unsupported quantifier: {}'.format(quantifier)) regions = [(s.start, s.stop) for s in ma.clump_unmasked(genomic_coordinates)] return chrom, regions
def dry_spells(rainfall, threshold=0.254, start=True): """ Compute the dry spells for a series of precipitations Parameters ---------- rainfall : TimeSeries TimeSeries of precipitations. threshold : float, optional Minimum amount of precipitation defining a wet day. start : boolean, optional Whether the spells are associated with the first or last day of the spell. Returns ------- dry_spells : TimeSeries A :class:`TimeSeries` giving the duration of the spell at either the first or last date. """ rdates = getattr(rainfall, 'dates', None) rdata = ma.masked_array(rainfall, subok=False) condition = ma.masked_greater(rdata, threshold) slices = ma.clump_unmasked(condition) # Get the durations and starting dates of each spell durations = [s.stop - s.start for s in slices] # Give up if we have no dates if rdates is None: return np.array(durations) # Get the dates, then... if start: dates = rdates[[s.start for s in slices]] else: dates = rdates[[s.stop - 1 for s in slices]] ensoi = getattr(rainfall, 'ensoindicator', None) spells = enso.climate_series(durations, dates=dates, ensoindicator=ensoi) return spells