def create_balanced_geojson(input_file, classes, output_file='balanced.geojson',
                            samples_per_class=None):
    '''
    Create a geojson comprised of balanced classes from the class_name property in
        input_file. Randomly selects polygons from all classes.

    Args:
        input_file (str): File name
        classes (list[str]): Classes in input_file to include in the balanced output file.
            Must exactly match the 'class_name' property in the features of input_file.
        output_file (str): Name under which to save the balanced output file. Defualts to
            balanced.geojson.
        samples_per_class (int or None): Number of features to select per class in
            input_file. If None will use the smallest class size. Defaults to None.
    '''

    if not output_file.endswith('.geojson'):
        output_file += '.geojson'

    with open(input_file) as f:
        data = geojson.load(f)

    # Sort classes in separate lists
    sorted_classes = {clss : [] for clss in classes}

    for feat in data['features']:
        try:
            sorted_classes[feat['properties']['class_name']].append(feat)
        except (KeyError):
            continue

    # Determine sample size per class
    if not samples_per_class:
        smallest_class = min(sorted_classes, key=lambda clss: len(sorted_classes[clss]))
        samples_per_class = len(sorted_classes[smallest_class])

    # Randomly select features from each class
    try:
        samps = [random.sample(feats, samples_per_class) for feats in sorted_classes.values()]
        final = [feat for sample in samps for feat in sample]
    except (ValueError):
        raise Exception('Insufficient features in at least one class. Set ' \
                            'samples_per_class to None to use maximum amount of '\
                            'features.')

    # Shuffle and save balanced data
    np.random.shuffle(final)
    data['features'] = final

    with open(output_file, 'wb') as f:
        geojson.dump(data, f)
Create a geojson comprised of balanced classes from the class_name property in
        input_file. Randomly selects polygons from all classes.

    Args:
        input_file (str): File name
        classes (list[str]): Classes in input_file to include in the balanced output file.
            Must exactly match the 'class_name' property in the features of input_file.
        output_file (str): Name under which to save the balanced output file. Defualts to
            balanced.geojson.
        samples_per_class (int or None): Number of features to select per class in
            input_file. If None will use the smallest class size. Defaults to None.