def stratified_sample_raster(strata_data, target_sample_size=<NUM_LIT:30>, min_sample_proportion=<NUM_LIT:0.1>):
with rasterio.open(strata_data) as src:<EOL><INDENT>strata = src.read().flatten()<EOL><DEDENT>index_array = np.arange(strata.size)<EOL>sample = dict([(int(s),[]) for s in np.unique(strata)])<EOL>satisfied = []<EOL>bins = np.bincount(strata)<EOL>ii = np.nonzero(bins)[<NUM_LIT:0>]<EOL>stratum_count = dict(zip(ii,bins[ii]))<EOL>np.random.shuffle(index_array)<EOL>for idx in index_array:<EOL><INDENT>stratum = strata[index_array[idx]]<EOL>if stratum in satisfied:<EOL><INDENT>continue<EOL><DEDENT>sample[stratum].append(idx)<EOL>nsamples = len(sample[stratum])<EOL>target = stratum_count[stratum] * min_sample_proportion<EOL>if target < target_sample_size:<EOL><INDENT>target = target_sample_size<EOL><DEDENT>if nsamples >= target:<EOL><INDENT>satisfied.append(stratum)<EOL><DEDENT>if len(satisfied) == len(sample.keys()):<EOL><INDENT>break<EOL><DEDENT><DEDENT>selected = []<EOL>for k, v in sample.items():<EOL><INDENT>if len(v) < target_sample_size:<EOL><INDENT>pass<EOL><DEDENT>else:<EOL><INDENT>selected.extend(v)<EOL><DEDENT><DEDENT>return np.array(selected)<EOL>
Parameters
----------
strata_data: Path to raster dataset containing strata to sample from (e.g. zones)

Returns
-------
selected: array of selected indices