from pathlib import Path


def _parse_annotations(annotations, pad_amount=0., skip_empty=False):
    """Parses annotation file data into features.

    Args:
        annotations: annotation file contents.

    Keyword Args:
        pad_amount: padding amount used for satsim offsets. Default is 0.
        skip_empty: whether to skip empty frames. Default is False.

    Returns:
        Tuple of (class_id, y_min, y_max, y_center, x_min, x_max, x_center,
            magnitude, path_name)
    """

    class_id = [obj['class_id'] for obj in annotations['objects']]
    y_min = [obj['y_min'] - pad_amount for obj in annotations['objects']]
    y_max = [obj['y_max'] + pad_amount for obj in annotations['objects']]
    y_center = [obj['y_center'] for obj in annotations['objects']]
    x_min = [obj['x_min'] - pad_amount for obj in annotations['objects']]
    x_max = [obj['x_max'] + pad_amount for obj in annotations['objects']]
    x_center = [obj['x_center'] for obj in annotations['objects']]
    magnitude = [obj['magnitude'] for obj in annotations['objects']]
    dir_name = annotations['file']['dirname']
    file_name = annotations['file']['filename']
    path_name = (Path(dir_name) / Path(file_name)).as_posix()
    if skip_empty and not x_center:
        return None
    # Replace the unknown magnitude's with NaN's
    for i in range(len(magnitude)):
        if magnitude[i] is None:
            magnitude[i] = float('NaN')
    return (class_id, y_min, y_max, y_center, x_min, x_max, x_center,
            magnitude, path_name)