# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/00_core.ipynb (unless otherwise specified).

__all__ = ['get_x', 'get_y', 'vocab', 'patient_id_splitter', 'build_DataBlock', 'get_preds', 'create_submission_csv']

# Cell
from nbdev.showdoc import *
from fastai.medical.imaging import *
from fastai.vision.all import *

# Cell
def get_x(r, path_images=None): return path_images/f'{str(r["image_name"])}.dcm'
def get_y(r): return r['benign_malignant']

# Cell
vocab = {'benign': 0, 'malignant': 1}

# Cell
def patient_id_splitter(df, val_pct, iteration_set_num_patients, seed):
    """Split images by patient_id, using subset of total data if desired.

    If no value for 'iteration_set_num_patients' is passed, entire dataset is used.
    """
    np.random.seed(seed)
    patient_ids = df['patient_id'].unique()
    if iteration_set_num_patients is not None: patient_ids = np.random.choice(patient_ids, iteration_set_num_patients, replace=False)
    split_idx = int(len(patient_ids)*val_pct)
    train = df[df['patient_id'].isin(patient_ids[split_idx:])].index.values
    valid = df[df['patient_id'].isin(patient_ids[:split_idx])].index.values
    return train, valid

# Cell
def build_DataBlock(path_images, val_pct=0.2, seed=42, iteration_set_num_patients=None):
    """Builds DataBlock for benign/malignant classification of DICOM images of moles, for melanoma."""
    return DataBlock(blocks=(ImageBlock(cls=PILDicom), CategoryBlock(vocab=vocab)),
                    get_x=partial(get_x, path_images=path_images), get_y=get_y,
                    splitter=partial(patient_id_splitter, val_pct=val_pct, iteration_set_num_patients=iteration_set_num_patients, seed=seed))

# Cell
def get_preds(learn, path_to_test_csv):
    """Generates predictions and formats them for submission file."""
    test_dl = learn.dls.test_dl(pd.read_csv(path_to_test_csv))
    probs = learn.get_preds(dl=test_dl)[0]
    return probs[1] # Return prob of target class

# Cell
def create_submission_csv(path, preds):
    """Saves submission.csv.

    Assumes order of preds is save as order of images in sample_submission.csv.
    """
    submission = pd.read_csv(path/'sample_submission.csv')
    submission['target'] = preds
    submission.to_csv('submission.csv', index=False)
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/00_core.ipynb (unless otherwise specified).

__all__ = ['get_x', 'get_y', 'vocab', 'patient_id_splitter', 'build_DataBlock', 'get_preds', 'create_submission_csv']

# Cell
from nbdev.showdoc import *
from fastai.medical.imaging import *
from fastai.vision.all import *

# Cell
def get_x(r, path_images=None): return path_images/f'{str(r["image_name"])}.dcm'
def get_y(r): return r['benign_malignant']

# Cell
vocab = {'benign': 0, 'malignant': 1}

# Cell
def patient_id_splitter(df, val_pct, iteration_set_num_patients, seed):
    """Split images by patient_id, using subset of total data if desired.

    If no value for 'iteration_set_num_patients' is passed, entire dataset is used.
    """
    np.random.seed(seed)
    patient_ids = df['patient_id'].unique()
    if iteration_set_num_patients is not None: patient_ids = np.random.choice(patient_ids, iteration_set_num_patients, replace=False)
    split_idx = int(len(patient_ids)*val_pct)
    train = df[df['patient_id'].isin(patient_ids[split_idx:])].index.values
    valid = df[df['patient_id'].isin(patient_ids[:split_idx])].index.values
    return train, valid

# Cell
def build_DataBlock(path_images, val_pct=0.2, seed=42, iteration_set_num_patients=None):
    """Builds DataBlock for benign/malignant classification of DICOM images of moles, for melanoma."""
    return DataBlock(blocks=(ImageBlock(cls=PILDicom), CategoryBlock(vocab=vocab)),
                    get_x=partial(get_x, path_images=path_images), get_y=get_y,
                    splitter=partial(patient_id_splitter, val_pct=val_pct, iteration_set_num_patients=iteration_set_num_patients, seed=seed))

# Cell
def get_preds(learn, path_to_test_csv):
    """Generates predictions and formats them for submission file."""
    test_dl = learn.dls.test_dl(pd.read_csv(path_to_test_csv))
    probs = learn.get_preds(dl=test_dl)[0]
    return probs[1] # Return prob of target class

# Cell
def create_submission_csv(path, preds):
    """Saves submission.csv.

    Assumes order of preds is save as order of images in sample_submission.csv.
    """
    submission = pd.read_csv(path/'sample_submission.csv')
    submission['target'] = preds
    submission.to_csv('submission.csv', index=False)
# AUTOGENERATED! DO NOT EDIT! File to edit: nbs/00_core.ipynb (unless otherwise specified). # Cell # Cell # Cell # Cell Split images by patient_id, using subset of total data if desired.      If no value for 'iteration_set_num_patients' is passed, entire dataset is used. # Cell Builds DataBlock for benign/malignant classification of DICOM images of moles, for melanoma. # Cell Generates predictions and formats them for submission file. # Return prob of target class # Cell Saves submission.csv.      Assumes order of preds is save as order of images in sample_submission.csv.