Kaggle competition: RSNA Pneumonia Detection Challenge
<SOS> metric: RSNAObjectDetectionAP Kaggle data source: rsna-pneumonia-detection-challenge<train_model>
on_kaggle = True
perm = PermutationImportance(clf_lgb_1, random_state=21 ).fit(X_valid, y_valid)
 eli5.show_weights(perm, feature_names = X_valid.columns.tolist() )<choose_model_class>
OTHRESH=0.05
PTHRESH=0.94
AVPTHRESH=0.65
THRESH =.175
MIN_MAX_MRCNN_CONF =.975
MINPROB = 0.69
C1 = 1e6
C2 = 0.06
UNET_MINPROB = 0.28
UNET_MINCONF = 0.35
YOLO_MINCONF = 0.15
MINMIN = 0.20
clf_lgb = lgb.LGBMClassifier(colsample_bytree= 0.6641632439141401,
max_depth= 5,
min_child_samples= 224,
min_child_weight= 0.1,
num_leaves= 28,
reg_alpha= 2,
reg_lambda= 10,
subsample= 0.9244622777209361,
random_state=21,
silent=True,
metric='auc',
n_jobs=4,
n_estimators=1000)
 clf_lgb.fit(X_train, y_train, eval_set=[(X_valid, y_valid)], early_stopping_rounds=10)
 test_df = test_df[X_train.columns]
probs = clf_lgb.predict_proba(test_df)
 submission = pd.DataFrame({"ID":test_id, "TARGET": probs[:,1]})
 submission.to_csv("submission.csv", index=False )<import_modules>
import numpy as np
import pandas as pd
import os
from sklearn import metrics
from scipy.special import logit,expit
from sklearn.linear_model import LogisticRegression
from sklearn.metrics import mean_squared_error
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from sklearn.model_selection import train_test_split
import xgboost as xgb
import seaborn as sns<load_from_csv>
RESULTS_LOC = '.. /input'
RAW_DATA_LOC = '.. /input/rsna-pneumonia-detection-challenge'
YOLO_SUB_STEM = 'yolov3-inference-from-multiple-saved-wts-stage-2/submission_yolo'
YOLO_SUB_STEM2 = 'yolov3-inference-from-more-saved-weights-stage-2/submission_yolo'
MORE_YOLO_EPOCHS = [5300, 1500, 6700, 2000] if on_kaggle else []
UNET_RESULTS_PATH = '.. /input/resnet-unet-from-saved-weights-stage-2/submission_resnet34unet_thr0.35.csv'
CHEXNET_OOF_STEM = 'rsna-oof-predictions/predictions_valid_fold_'
CHEXNET_TEST_STEM = 'gs-dense-chexnet-predict-stage-2-from-all-models/test_preds_pth_fold'
SPECIAL_FILE0 = 'gs-dense-chexnet-predict-stage-2-from-all-models/test_preds_pth_fold0_for_combined_folds'
CHEXNET_TEST_FILE0 = SPECIAL_FILE0 if on_kaggle else CHEXNET_TEST_STEM + '0'
ADENSE_TEST_STEM = 'andy-densenet-from-multiple-saved-weights-stage-2/submission_adense_f'
ADEMSE_OOF_STEM = 'rsna-oof-predictions/val_dense_v5_a1_f'
MRCNN_TEST_STEM = 'mrcnn-inference-from-saved-wts-4dig-70-stage-2/submission_mrcnn_v'
MRCNN95_TEST_STEM = 'mrcnn-inference-from-saved-wts-2dig-95-stage-2/submission_mrcnn_v' \
if on_kaggle else MRCNN_TEST_STEM
MRCNN_OUT_STEM = 'temp_mrcnn_v' if on_kaggle else MRCNN_TEST_STEM
MRCNN_OOF_STEM = 'rsna-oof-predictions/val_v'
MRCNN_OOF_OUT_STEM = 'val_v' if on_kaggle else MRCNN_OOF_STEM
MRCNN_TEST_SEP = ''
MRCNN_OOF_SEP = '_'
IN_OOF_SUFFIX = ''
IN_SUFFIX = '.csv'
OUT_SUFFIX = '.csv'
CHEX_CLASS_PRED_PATH = '.. /input/combine-fold-class-predictions-stage-2/test_probs.csv'
KERNEL_OUTPUT_PATH = '.. /input/filter-199-final-with-higher-thresh-stage-2/filter199.csv'
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<load_from_csv>
N_FOLDS = 5
OUTPUT_LOC = '.'
OOF_INFILE_STEM = MRCNN_OOF_STEM + '9' + MRCNN_OOF_SEP + 'a1' + MRCNN_OOF_SEP + 'f'
vers = '8' if on_kaggle else '9'
TEST_INFILE_STEM = MRCNN_TEST_STEM + vers + MRCNN_TEST_SEP + 'a1' + MRCNN_TEST_SEP + 'f'
OOF_OUTFILE_NAME = MRCNN_OOF_OUT_STEM + '10' + MRCNN_OOF_SEP + 'a1' + \
MRCNN_OOF_SEP + 'allfolds_out.csv'
TEST_OUTFILE_STEM = MRCNN_OUT_STEM + '10' + MRCNN_TEST_SEP + 'a1' + MRCNN_TEST_SEP + 'f_out'
OOF_CLASS_PROBS_OUTFILE = 'phase3_oof_out.csv'
TEST_CLASS_PROBS_OUTFILE_STEM = 'phase3_test_out'
FULL_TEST_CLASS_PROBS_OUTFILE = 'phase3_test_avg_out.csv'
train = pd.read_csv('.. /input/train.csv')
 test = pd.read_csv('.. /input/test.csv' )<count_unique_values>
dfs = []
for a in range(1,3):
for f in range(5):
vers = '8' if a==2 and f==0 else '1'
fn = RESULTS_LOC +'/'+ MRCNN95_TEST_STEM + vers + MRCNN_TEST_SEP + \
'a' + str(a)+ MRCNN_TEST_SEP + 'f' + str(f)+ IN_SUFFIX
dfs.append(pd.read_csv(fn ).set_index('patientId'))
for i,f in enumerate(dfs):
if i:
df = df.join(f.rename(columns={'PredictionString':'pred'+str(i)}))
else:
df = f.rename(columns={'PredictionString':'pred'+str(i)} )
print('Shapes:')
 print(train.shape)
 print(test.shape)
 print('
IDs únicos:')
 print(train.ID.nunique())
 print(test.ID.nunique())
 print('
Colunas com nulos:')
 print(( train.isnull().sum() !=0 ).sum() )<count_values>
def non_max_suppression(boxes, probs=None, overlapThresh=OTHRESH):
	
	if len(boxes)== 0:
		return []
	
	
	if boxes.dtype.kind == "i":
		boxes = boxes.astype("float")
 	
	pick = []
	
	x1 = boxes[:, 0]
	y1 = boxes[:, 1]
	x2 = boxes[:, 2]
	y2 = boxes[:, 3]
	
	
	
	area =(x2 - x1 + 1)*(y2 - y1 + 1)
 	idxs = y2
	
	if probs is not None:
		idxs = probs
	
	idxs = np.argsort(idxs)
 	
	while len(idxs)> 0:
		
		
		last = len(idxs)- 1
		i = idxs[last]
		pick.append(i)
 		
		
		
		xx1 = np.maximum(x1[i], x1[idxs[:last]])
 		yy1 = np.maximum(y1[i], y1[idxs[:last]])
 		xx2 = np.minimum(x2[i], x2[idxs[:last]])
 		yy2 = np.minimum(y2[i], y2[idxs[:last]])
 		
		w = np.maximum(0, xx2 - xx1 + 1)
 		h = np.maximum(0, yy2 - yy1 + 1)
 		
		overlap =(w * h)/ area[idxs[:last]]
		
		
		idxs = np.delete(idxs, np.concatenate(( [last],
			np.where(overlap > overlapThresh)[0])))
 	
	return boxes[pick].astype("int"), list(np.array(probs)[pick] )
train.TARGET.value_counts()<drop_column>
box_dict = {}
conf_dict = {}
for pat,row in df.iterrows() :
boxes = []
confs = []
maxconfs = []
for pred in row:
maxconf = 0.
 if isinstance(pred, str):
s = pred.split(' ')
 if s[-1]=='':
s.pop()
if s[0]=='':
s.pop(0)
 if(len(s)%5):
print('Bad prediction string.')
 while len(s):
conf = float(s.pop(0))
x = int(round(float(s.pop(0))))
y = int(round(float(s.pop(0))))
w = int(round(float(s.pop(0))))
h = int(round(float(s.pop(0))))
if conf>maxconf:
maxconf = conf
if conf>PTHRESH:
boxes.append([x,y,x+w,y+h])
 confs.append(conf)
 maxconfs.append(maxconf)
 avgconf = sum(maxconfs)/len(maxconfs)
 if len(boxes)and avgconf>AVPTHRESH:
box_dict[pat] = boxes
conf_dict[pat] = confs
len(box_dict), len(conf_dict )
to_drop = []
n_cols = len(train.drop(['TARGET'],axis=1 ).columns)
 for i in range(n_cols):
v1 = train.iloc[i].values
for j in range(i+1,n_cols):
v2 = train.iloc[j].values
if np.all(v1 == v2):
to_drop.append(train.columns[j])
 train.drop(to_drop, axis=1, inplace=True)
 test.drop(to_drop, axis=1, inplace=True)
 print('dropped: ' + str(len(to_drop)))
 print(train.shape)
 print(test.shape )<split>
box_dict_nms = {}
conf_dict_nms = {}
for p in box_dict:
boxes, confs = non_max_suppression(np.array(box_dict[p]), np.array(conf_dict[p]))
box_dict_nms[p] = boxes
conf_dict_nms[p] = confs
X = train.drop(['TARGET','ID'],axis=1)
 y = train['TARGET']
X_train, X_test, y_train, y_test = train_test_split(X,y)
 print(X_train.shape)
 print(X_test.shape)
 print(y_train.shape)
 print(y_test.shape)
 X = train.drop(['TARGET','ID'],axis=1)
 y = train.TARGET
X_train, X_test, y_train, y_test = train_test_split(X,y )<train_model>
sub_dict = {}
for p in df.index:
predictionString = ''
if p in box_dict_nms:
for box, conf in zip(box_dict_nms[p], conf_dict_nms[p]):
x, y, x2, y2 = box
height = y2 - y
width = x2 - x
predictionString += str(conf)+ ' ' + str(x)+ ' ' + str(y)+ ' ' + str(width)+ ' ' + str(height)+ ' '
sub_dict[p] = predictionString
sub = pd.DataFrame.from_dict(sub_dict,orient='index')
 sub.index.names = ['patientId']
sub.columns = ['PredictionString']
phase0_output = sub
clf = xgb.XGBClassifier(max_depth = 5, n_estimators=1000, learning_rate=0.2, nthread=3, subsample=1.0,
colsample_bytree=0.5, min_child_weight=3, reg_alpha=0.03, eed=1301)
 clf.fit(X_train, y_train, early_stopping_rounds=50, eval_metric="auc", eval_set=[(X_train, y_train),(X_test, y_test)] )<predict_on_test>
probs = pd.read_csv(CHEX_CLASS_PRED_PATH )
submission = test[['ID']]
submission['TARGET'] = clf.predict_proba(test.drop(['ID'],axis=1), ntree_limit=clf.best_iteration)[:,1]<save_to_csv>
df = phase0_output.join(probs.set_index('patientId'))
out = df.copy()
out.loc[df.prob<THRESH,'PredictionString'] = np.nan
out.loc[df.PredictionString=='','PredictionString'] = np.nan
phase1_output = out.drop(['prob'],axis=1 )
submission.to_csv('submission.csv', index=False)
 submission.head()<import_modules>
df1 = pd.read_csv(KERNEL_OUTPUT_PATH ).set_index('patientId')
 df2 = phase1_output
df1_cases = df1[~df1.PredictionString.isnull() ]
df2_cases = df2[~df2.PredictionString.isnull() ]
df1_pos_ids = df1_cases.index.values
df2_pos_ids = df2_cases.index.values
tf.logging.set_verbosity(tf.logging.ERROR)
 pd.options.display.max_rows = 10
pd.options.display.float_format = '{:.1f}'.format
print(os.listdir(".. /input")) 
<load_from_csv>
df1_only_dict = {}
for p in df1_pos_ids:
if not p in df2_pos_ids:
df1_only_dict[p] = float(df1_cases.loc[p,'PredictionString'].split(' ')[0] )
train_df = pd.read_csv(".. /input/train.csv",sep=",")
 test_df = pd.read_csv(".. /input/test.csv",sep="," )<create_dataframe>
df2_only_dict = {}
for p in df2_pos_ids:
if not p in df1_pos_ids:
df2_only_dict[p] = float(df2_cases.loc[p,'PredictionString'].split(' ')[0] )
combined_df=pd.DataFrame()
combined_df=pd.concat([train_df,test_df] )<create_dataframe>
df_out = df1.copy()
for p,r in df1.iterrows() :
if p in df2_only_dict:
if df2_only_dict[p] > MIN_MAX_MRCNN_CONF:
df_out.loc[p,'PredictionString'] = df2.loc[p,'PredictionString']
phase2_output = df_out
num_df=pd.DataFrame.copy(combined_df)
 target_df=pd.DataFrame()
target_df["TARGET"]=num_df["TARGET"]
num_df=num_df.drop(["TARGET"],axis=1)
 ID_df=pd.DataFrame()
ID_df["ID"]=num_df["ID"]
num_df=num_df.drop(["ID"],axis=1 )<count_values>
input_dir = RESULTS_LOC
infile_stem = OOF_INFILE_STEM
oof_preds_input = []
for ifold in range(N_FOLDS):
fp_in = os.path.join(input_dir, infile_stem + str(ifold)+ IN_OOF_SUFFIX)
 oof_preds_input.append(pd.read_csv(fp_in))
oof_preds_input[-1]['fold'] = ifold
oof_preds = pd.concat(oof_preds_input ).set_index('patientId' )
clean_df=pd.DataFrame.copy(num_df)
 clean_df["var3"].value_counts()
clean_df.loc[clean_df['var3']==-999999,'var3']=2
clean_df["var3"].value_counts()<feature_engineering>
probs_dict = {}
for p, r in oof_preds.iterrows() :
probs_dict[p] = MINPROB
oof_positive = oof_preds[~oof_preds.PredictionString.isnull() ]
for p, r in oof_positive.iterrows() :
s = r.PredictionString.split(' ')
 if s[-1]=='':
s.pop()
if s[0]=='':
s.pop(0)
 if(len(s)%5):
print('Bad prediction string.')
 while len(s):
conf = float(s.pop(0))
x = int(round(float(s.pop(0))))
y = int(round(float(s.pop(0))))
w = int(round(float(s.pop(0))))
h = int(round(float(s.pop(0))))
if conf>probs_dict[p]:
probs_dict[p] = conf
prob_df = pd.DataFrame(probs_dict,index=['prob'] ).transpose()
clean_df[num_df==9999999999] = 100
clean_df.skew().sort_values(ascending=False )<create_dataframe>
input_dir = RAW_DATA_LOC
fp = os.path.join(input_dir, 'stage_2_train_labels.csv')
 act = pd.read_csv(fp ).set_index('patientId' ).rename(columns={'Target':'actual'})[['actual']]
act = act[~act.index.duplicated() ]
def preprocess_features(training_df):
selected_features = training_df[training_df.columns.difference(["TARGET","ID"])]
processed_features = selected_features.copy()
return processed_features
def preprocess_targets(training_df):
output_targets = pd.DataFrame()
output_targets["TARGET"] =(
 training_df["TARGET"])
 return output_targets<feature_engineering>
df = act.join(prob_df,how='right')
 df.head()
training_examples= preprocess_features(Eng_train_df)
 test_examples = preprocess_features(Eng_test_df)
 training_targets = preprocess_targets(Eng_train_df )<train_on_grid>
rawprobs = df.prob.values.reshape(-1,1)
 lr = LogisticRegression(C=C1)
 lr.fit(logit(rawprobs),df.actual)
 b1 = lr.coef_[0,0]
b0 = lr.intercept_[0]
b0, b1
model_lasso = LassoCV(cv=10 ).fit(training_examples, np.ravel(training_targets))
coef = pd.Series(model_lasso.coef_, index = training_examples.columns)
 print("Lasso picked " + str(sum(coef != 0)) + " variables and eliminated the other " + str(sum(coef == 0)) + " variables" )<save_to_csv>
folds = []
for ifold in range(N_FOLDS):
folds.append(oof_preds[oof_preds.fold==ifold].index.values )
lasso_preds = model_lasso.predict(test_examples)
 solution = pd.DataFrame({"ID":test_df["ID"], "Target":np.abs(lasso_preds)})
 solution.to_csv("Lasso.csv", index = False )<sort_values>
def run_logistic_by_fold() :
df = act.join(prob_df,how='right')
 logits = df.prob.apply(logit)
 df['oofprob'] = np.nan
b1 = []
b0 = []
for i in range(len(folds)) :
fs = folds.copy()
te = fs.pop(i)
 tr = np.concatenate(fs)
 Xtr = logits[tr].values.reshape(-1,1)
 Xte = logits[te].values.reshape(-1,1)
 ytr = df.actual[tr].copy()
yte = df.actual[te].copy()
lr.fit(Xtr, ytr)
 df.loc[te,'oofprob'] = lr.predict_proba(Xte)[:,1]
b1.append(lr.coef_[0,0])
 b0.append(lr.intercept_[0])
 coefs = pd.DataFrame({'b0':b0, 'b1':b1}, index=range(len(folds)))
 coefs.index.name = 'fold'
return(df.sort_values('oofprob'), coefs )
lasso_features = coef.iloc[coef.nonzero() [0]].sort_values()
lasso_features=lasso_features.index
lasso_features<save_to_csv>
lr = LogisticRegression(C=C2 )
model_xgb=xgb.XGBClassifier()
model_xgb.fit(training_examples, np.ravel(training_targets))
xgb_preds = model_xgb.predict_proba(test_examples)
 solution = pd.DataFrame({"ID":test_df["ID"], "Target":np.abs(xgb_preds[:,1])})
 solution.to_csv("XGB_v1.csv", index = False )<save_to_csv>
df, coefs = run_logistic_by_fold()
model_xgb2=xgb.XGBClassifier()
model_xgb2.fit(training_examples[lasso_features], np.ravel(training_targets))
xgb_preds2 = model_xgb2.predict_proba(test_examples[lasso_features])
 solution = pd.DataFrame({"ID":test_df["ID"], "Target":np.abs(xgb_preds2[:,1])})
 solution.to_csv("XGB_v2.csv", index = False )<install_modules>
pat_dict = {}
fold_dict = {}
for pat,row in oof_preds.iterrows() :
fold = row.fold
pred = row.PredictionString
boxes = []
confs = []
if isinstance(pred, str):
s = pred.split(' ')
 if s[-1]=='':
s.pop()
if s[0]=='':
s.pop(0)
 if(len(s)%5):
print('Bad prediction string.')
 b0 = coefs.b0[fold]
b1 = coefs.b1[fold]
while len(s):
conf = float(s.pop(0))
x = int(round(float(s.pop(0))))
y = int(round(float(s.pop(0))))
w = int(round(float(s.pop(0))))
h = int(round(float(s.pop(0))))
boxes.append([x,y,w,h])
 confs.append(expit(b0+b1*logit(conf)))
 predictionString = ''
if len(boxes):
for box, conf in zip(boxes, confs):
x, y, w, h = box
predictionString += '{:6.4f} '.format(conf)
 predictionString += str(x)+ ' ' + str(y)+ ' ' + str(w)+ ' ' + str(h)+ ' '
pat_dict[pat] = predictionString
fold_dict[pat] = fold
xform_oof_preds = pd.DataFrame(pat_dict,
index=['PredictionString'] ).transpose()
xform_oof_preds.index.name = 'patientId'
xform_oof_preds['fold'] = [fold_dict[x] for x in xform_oof_preds.index.values]
xform_oof_preds.head()
!pip install.. /input/kerasapplications/keras-team-keras-applications-3b180cb -f./ --no-index
!pip install.. /input/efficientnet/efficientnet-1.1.0/ -f./ --no-index<import_modules>
output_dir = OUTPUT_LOC
xform_oof_preds.to_csv(os.path.join(output_dir, OOF_OUTFILE_NAME))
import os
import cv2
import pydicom
import pandas as pd
import numpy as np
import tensorflow as tf
import matplotlib.pyplot as plt
import random
from tqdm.notebook import tqdm
from sklearn.model_selection import train_test_split, KFold
from sklearn.metrics import mean_absolute_error
from tensorflow_addons.optimizers import RectifiedAdam
from tensorflow.keras import Model
import tensorflow.keras.backend as K
import tensorflow.keras.layers as L
import tensorflow.keras.models as M
from tensorflow.keras.optimizers import Nadam
import seaborn as sns
import plotly.express as px
import plotly.graph_objects as go
from PIL import Image<define_variables>
input_dir = RESULTS_LOC
output_dir = OUTPUT_LOC
infile_stem = TEST_INFILE_STEM
outfile_stem = TEST_OUTFILE_STEM
def seed_everything(seed=2020):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 tf.random.set_seed(seed )<define_variables>
for ifold in range(N_FOLDS):
fp_in = os.path.join(input_dir, infile_stem + str(ifold)+ IN_SUFFIX)
 fp_out = os.path.join(output_dir, outfile_stem + str(ifold)+ OUT_SUFFIX)
 inpreds = pd.read_csv(fp_in ).set_index('patientId')
 pat_dict = {}
for pat,row in inpreds.iterrows() :
pred = row.PredictionString
boxes = []
confs = []
if isinstance(pred, str):
s = pred.split(' ')
 if s[-1]=='':
s.pop()
if s[0]=='':
s.pop(0)
 if(len(s)%5):
print('Bad prediction string.')
 while len(s):
conf = float(s.pop(0))
x = int(round(float(s.pop(0))))
y = int(round(float(s.pop(0))))
w = int(round(float(s.pop(0))))
h = int(round(float(s.pop(0))))
boxes.append([x,y,w,h])
 confs.append(expit(b0+b1*logit(conf)))
 predictionString = ''
if len(boxes):
for box, conf in zip(boxes, confs):
x, y, w, h = box
predictionString += '{:6.4f} '.format(conf)
 predictionString += str(x)+ ' ' + str(y)+ ' ' + str(w)+ ' ' + str(h)+ ' '
pat_dict[pat] = predictionString
outpreds = pd.DataFrame(pat_dict,
index=['PredictionString'] ).transpose()
outpreds.index.name = 'patientId'
outpreds.to_csv(fp_out)
 fp_out
seed_everything(42 )<set_options>
outprobs = prob_df.join(oof_preds[['fold']])
 b0_ = coefs.b0[outprobs.fold].values
b1_ = coefs.b1[outprobs.fold].values
outprobs['prob'] = expit(b0_ + logit(outprobs.prob)*b1_)
 outprobs.index.name = 'patientId'
output_dir = OUTPUT_LOC
outprobs.to_csv(os.path.join(output_dir, OOF_CLASS_PROBS_OUTFILE))
outprobs.head()
config = tf.compat.v1.ConfigProto()
config.gpu_options.allow_growth = True
session = tf.compat.v1.Session(config=config )<init_hyperparams>
output_dir = OUTPUT_LOC
box_outfile_stem = TEST_OUTFILE_STEM
probs_outfile_stem = TEST_CLASS_PROBS_OUTFILE_STEM
minprob_out = expit(b0 + b1*logit(MINPROB))
for ifold in range(N_FOLDS):
fp_in = os.path.join(output_dir, box_outfile_stem + str(ifold)+ OUT_SUFFIX)
 inpreds = pd.read_csv(fp_in ).set_index('patientId')
 fp_out = os.path.join(output_dir, probs_outfile_stem + str(ifold)+ OUT_SUFFIX)
 probs_dict = {}
for p, r in inpreds.iterrows() :
probs_dict[p] = minprob_out
inpreds_positive = inpreds[~inpreds.PredictionString.isnull() ]
for p, r in inpreds_positive.iterrows() :
s = r.PredictionString.split(' ')
 if s[-1]=='':
s.pop()
if s[0]=='':
s.pop(0)
 if(len(s)%5):
print('Bad prediction string.')
 while len(s):
conf = float(s.pop(0))
s.pop(0)
 s.pop(0)
 s.pop(0)
 s.pop(0)
 if conf>probs_dict[p]:
probs_dict[p] = conf
outprob_df = pd.DataFrame(probs_dict,index=['prob'] ).transpose()
outprob_df.index.name = 'patientId'
outprob_df.to_csv(fp_out)
 if not ifold:
allprobs_df = outprob_df.rename(columns={'prob':ifold})
 else:
allprobs_df = allprobs_df.join(outprob_df.rename(columns={'prob':ifold}))
Dropout_model = 0.3856
FVC_weight = 0.2
Confidence_weight = 0.2<load_from_csv>
fp = OOF_CLASS_PROBS_OUTFILE
oof_probs_mrcnn = pd.read_csv(fp ).set_index('patientId' ).rename(columns={'prob':'ph'} )
train = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/train.csv' )<concatenate>
for i in range(N_FOLDS):
fp = TEST_CLASS_PROBS_OUTFILE_STEM + str(i)+ '.csv'
indf = pd.read_csv(fp ).set_index('patientId' ).rename(columns={'prob':'ph'+str(i)})
 if i:
test_prob_df = test_prob_df.join(indf)
 else:
test_prob_df = indf
def get_tab(df):
vector = [(df.Age.values[0] - 30)/ 30]
if df.Sex.values[0] == 'male':
vector.append(0)
 else:
vector.append(1)
 if df.SmokingStatus.values[0] == 'Never smoked':
vector.extend([0,0])
 elif df.SmokingStatus.values[0] == 'Ex-smoker':
vector.extend([1,1])
 elif df.SmokingStatus.values[0] == 'Currently smokes':
vector.extend([0,1])
 else:
vector.extend([1,0])
 return np.array(vector )<feature_engineering>
f0 = pd.read_csv(RESULTS_LOC + '/' + ADEMSE_OOF_STEM + '0.csv' ).set_index('patientId')
 f1 = pd.read_csv(RESULTS_LOC + '/' + ADEMSE_OOF_STEM + '1.csv' ).set_index('patientId')
 f2 = pd.read_csv(RESULTS_LOC + '/' + ADEMSE_OOF_STEM + '2.csv' ).set_index('patientId')
 f3 = pd.read_csv(RESULTS_LOC + '/' + ADEMSE_OOF_STEM + '3.csv' ).set_index('patientId')
 f4 = pd.read_csv(RESULTS_LOC + '/' + ADEMSE_OOF_STEM + '4.csv' ).set_index('patientId' )
A = {}
TAB = {}
P = []
for i, p in tqdm(enumerate(train.Patient.unique())) :
sub = train.loc[train.Patient == p, :]
fvc = sub.FVC.values
weeks = sub.Weeks.values
c = np.vstack([weeks, np.ones(len(weeks)) ] ).T
a, b = np.linalg.lstsq(c, fvc)[0]
A[p] = a
TAB[p] = get_tab(sub)
 P.append(p )<feature_engineering>
den = pd.concat([f0,f1,f2,f3,f4],axis=0)
 print(den.shape)
 den.head()
class IGenerator(Sequence):
BAD_ID = ['ID00011637202177653955184', 'ID00052637202186188008618']
def __init__(self, keys, a, tab, batch_size=32):
self.keys = [k for k in keys if k not in self.BAD_ID]
self.a = a
self.tab = tab
self.batch_size = batch_size
self.train_data = {}
for p in train.Patient.values:
self.train_data[p] = os.listdir(f'.. /input/osic-pulmonary-fibrosis-progression/train/{p}/')
 def __len__(self):
return 1000
def __getitem__(self, idx):
x = []
a, tab = [], []
keys = np.random.choice(self.keys, size = self.batch_size)
 for k in keys:
try:
i = np.random.choice(self.train_data[k], size=1)[0]
img = get_img(f'.. /input/osic-pulmonary-fibrosis-progression/train/{k}/{i}')
 x.append(img)
 a.append(self.a[k])
 tab.append(self.tab[k])
 except:
print(k, i)
 x,a,tab = np.array(x), np.array(a), np.array(tab)
 x = np.expand_dims(x, axis=-1)
 return [x, tab] , a<choose_model_class>
tf0 = pd.read_csv(RESULTS_LOC + '/' + ADENSE_TEST_STEM + '0.csv' ).set_index('patientId')
 tf1 = pd.read_csv(RESULTS_LOC + '/' + ADENSE_TEST_STEM + '1.csv' ).set_index('patientId')
 tf2 = pd.read_csv(RESULTS_LOC + '/' + ADENSE_TEST_STEM + '2.csv' ).set_index('patientId')
 tf3 = pd.read_csv(RESULTS_LOC + '/' + ADENSE_TEST_STEM + '3.csv' ).set_index('patientId')
 tf4 = pd.read_csv(RESULTS_LOC + '/' + ADENSE_TEST_STEM + '4.csv' ).set_index('patientId' )
Dense, Dropout, Activation, Flatten, Input, BatchNormalization, GlobalAveragePooling2D, Add, Conv2D, AveragePooling2D,
LeakyReLU, Concatenate 
)
 def get_efficientnet(model, shape):
models_dict = {
'b0': efn.EfficientNetB0(input_shape=shape,weights=None,include_top=False),
'b1': efn.EfficientNetB1(input_shape=shape,weights=None,include_top=False),
'b2': efn.EfficientNetB2(input_shape=shape,weights=None,include_top=False),
'b3': efn.EfficientNetB3(input_shape=shape,weights=None,include_top=False),
'b4': efn.EfficientNetB4(input_shape=shape,weights=None,include_top=False),
'b5': efn.EfficientNetB5(input_shape=shape,weights=None,include_top=False),
'b6': efn.EfficientNetB6(input_shape=shape,weights=None,include_top=False),
'b7': efn.EfficientNetB7(input_shape=shape,weights=None,include_top=False)
 }
return models_dict[model]
def build_model(shape=(512, 512, 1), model_class=None):
inp = Input(shape=shape)
 base = get_efficientnet(model_class, shape)
 x = base(inp)
 x = GlobalAveragePooling2D()(x)
 inp2 = Input(shape=(4,))
x2 = tf.keras.layers.GaussianNoise(0.2 )(inp2)
 x = Concatenate()([x, x2])
 x = Dropout(Dropout_model )(x)
 x = Dense(1 )(x)
 model = Model([inp, inp2] , x)
 weights = [w for w in os.listdir('.. /input/osic-model-weights')if model_class in w][0]
model.load_weights('.. /input/osic-model-weights/' + weights)
 return model
model_classes = ['b5']
models = [build_model(shape=(512, 512, 1), model_class=m)for m in model_classes]
print('Number of models: ' + str(len(models)) )<split>
test_den = tf0.rename(columns={'predicted':'pa0'})
 test_den = test_den.join(tf1.rename(columns={'predicted':'pa1'}))
test_den = test_den.join(tf2.rename(columns={'predicted':'pa2'}))
test_den = test_den.join(tf3.rename(columns={'predicted':'pa3'}))
test_den = test_den.join(tf4.rename(columns={'predicted':'pa4'}))
print(test_den.shape)
 test_den.head()
tr_p, vl_p = train_test_split(P, shuffle=True, train_size = 0.9 )<compute_test_metric>
p0 = f0.index.values
p1 = f1.index.values
p2 = f2.index.values
p3 = f3.index.values
p4 = f4.index.values
pt = test_den.index.values
def score(fvc_true, fvc_pred, sigma):
sigma_clip = np.maximum(sigma, 69)
 delta = np.abs(fvc_true - fvc_pred)
 delta = np.minimum(delta, 1000)
 sq2 = np.sqrt(2)
 metric =(delta / sigma_clip)*sq2 + np.log(sigma_clip* sq2)
 return np.mean(metric )<define_variables>
c0 = pd.read_csv(RESULTS_LOC + '/' + CHEXNET_OOF_STEM + '0.csv' ).set_index('patientId')
 c1 = pd.read_csv(RESULTS_LOC + '/' + CHEXNET_OOF_STEM + '1.csv' ).set_index('patientId')
 c2 = pd.read_csv(RESULTS_LOC + '/' + CHEXNET_OOF_STEM + '2.csv' ).set_index('patientId')
 c3 = pd.read_csv(RESULTS_LOC + '/' + CHEXNET_OOF_STEM + '3.csv' ).set_index('patientId')
 c4 = pd.read_csv(RESULTS_LOC + '/' + CHEXNET_OOF_STEM + '4.csv' ).set_index('patientId')
 chex = pd.concat([c0,c1,c2,c3,c4],axis=0)
 print(chex.shape)
 chex.head()
subs = []
for model in models:
metric = []
for q in tqdm(range(1, 10)) :
m = []
for p in vl_p:
x = []
tab = []
if p in ['ID00011637202177653955184', 'ID00052637202186188008618']:
continue
ldir = os.listdir(f'.. /input/osic-pulmonary-fibrosis-progression/train/{p}/')
 for i in ldir:
if int(i[:-4])/ len(ldir)< 0.8 and int(i[:-4])/ len(ldir)> 0.15:
x.append(get_img(f'.. /input/osic-pulmonary-fibrosis-progression/train/{p}/{i}'))
tab.append(get_tab(train.loc[train.Patient == p, :]))
if len(x)< 1:
continue
tab = np.array(tab)
 x = np.expand_dims(x, axis=-1)
 _a = model.predict([x, tab])
 a = np.quantile(_a, q / 10)
 percent_true = train.Percent.values[train.Patient == p]
fvc_true = train.FVC.values[train.Patient == p]
weeks_true = train.Weeks.values[train.Patient == p]
fvc = a *(weeks_true - weeks_true[0])+ fvc_true[0]
percent = percent_true[0] - a * abs(weeks_true - weeks_true[0])
 m.append(score(fvc_true, fvc, percent))
print(np.mean(m))
metric.append(np.mean(m))
q =(np.argmin(metric)+ 1)/ 10
sub = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/sample_submission.csv')
 test = pd.read_csv('.. /input/osic-pulmonary-fibrosis-progression/test.csv')
 A_test, B_test, P_test,W, FVC= {}, {}, {},{},{}
STD, WEEK = {}, {}
for p in test.Patient.unique() :
x = []
tab = []
ldir = os.listdir(f'.. /input/osic-pulmonary-fibrosis-progression/test/{p}/')
 for i in ldir:
if int(i[:-4])/ len(ldir)< 0.8 and int(i[:-4])/ len(ldir)> 0.15:
x.append(get_img(f'.. /input/osic-pulmonary-fibrosis-progression/test/{p}/{i}'))
tab.append(get_tab(test.loc[test.Patient == p, :]))
if len(x)<= 1:
continue
tab = np.array(tab)
 x = np.expand_dims(x, axis=-1)
 _a = model.predict([x, tab])
 a = np.quantile(_a, q)
 A_test[p] = a
B_test[p] = test.FVC.values[test.Patient == p] - a*test.Weeks.values[test.Patient == p]
P_test[p] = test.Percent.values[test.Patient == p]
WEEK[p] = test.Weeks.values[test.Patient == p]
for k in sub.Patient_Week.values:
p, w = k.split('_')
 w = int(w)
 fvc = A_test[p] * w + B_test[p]
sub.loc[sub.Patient_Week == k, 'FVC'] = fvc
sub.loc[sub.Patient_Week == k, 'Confidence'] =(
 P_test[p] - A_test[p] * abs(WEEK[p] - w)
)
 _sub = sub[["Patient_Week","FVC","Confidence"]].copy()
subs.append(_sub )<prepare_output>
df = den.join(chex[['validPredProba']] ).rename(columns={'predicted':'pa','validPredProba':'pg'})
 df = df.join(oof_probs_mrcnn ).drop(['fold'],axis=1)
 print(df.shape)
 df.head()
N = len(subs)
 sub = subs[0].copy()
sub["FVC"] = 0
sub["Confidence"] = 0
for i in range(N):
sub["FVC"] += subs[0]["FVC"] *(1/N)
 sub["Confidence"] += subs[0]["Confidence"] *(1/N )<save_to_csv>
tc0 = pd.read_csv(RESULTS_LOC + '/' + CHEXNET_TEST_FILE0 + '.csv' ).set_index('patientId')
 tc1 = pd.read_csv(RESULTS_LOC + '/' + CHEXNET_TEST_STEM + '1.csv' ).set_index('patientId')
 tc2 = pd.read_csv(RESULTS_LOC + '/' + CHEXNET_TEST_STEM + '2.csv' ).set_index('patientId')
 tc3 = pd.read_csv(RESULTS_LOC + '/' + CHEXNET_TEST_STEM + '3.csv' ).set_index('patientId')
 tc4 = pd.read_csv(RESULTS_LOC + '/' + CHEXNET_TEST_STEM + '4.csv' ).set_index('patientId')
 tc0.head()
sub[["Patient_Week","FVC","Confidence"]].to_csv("submission_img.csv", index=False )<prepare_output>
INFINITY = 100
lr = LogisticRegression(C=INFINITY )
img_sub = sub[["Patient_Week","FVC","Confidence"]].copy()<load_from_csv>
test_chex = tc0.rename(columns={'targetPredProba':'pg0'} ).drop(['targetPred'],axis=1)
 test_chex = test_chex.join(tc1.rename(columns={'targetPredProba':'pg1'} ).drop(['targetPred'],axis=1))
test_chex = test_chex.join(tc2.rename(columns={'targetPredProba':'pg2'} ).drop(['targetPred'],axis=1))
test_chex = test_chex.join(tc3.rename(columns={'targetPredProba':'pg3'} ).drop(['targetPred'],axis=1))
test_chex = test_chex.join(tc4.rename(columns={'targetPredProba':'pg4'} ).drop(['targetPred'],axis=1))
print(test_chex.shape)
 test_chex.head()
ROOT = ".. /input/osic-pulmonary-fibrosis-progression"
BATCH_SIZE=128
tr = pd.read_csv(f"{ROOT}/train.csv")
 tr.drop_duplicates(keep=False, inplace=True, subset=['Patient','Weeks'])
 chunk = pd.read_csv(f"{ROOT}/test.csv")
 print("add infos")
 sub = pd.read_csv(f"{ROOT}/sample_submission.csv")
 sub['Patient'] = sub['Patient_Week'].apply(lambda x:x.split('_')[0])
 sub['Weeks'] = sub['Patient_Week'].apply(lambda x: int(x.split('_')[-1]))
sub = sub[['Patient','Weeks','Confidence','Patient_Week']]
sub = sub.merge(chunk.drop('Weeks', axis=1), on="Patient" )<concatenate>
all_test_probs = test_prob_df.join(test_den.join(test_chex))
all_test_probs.head()
tr['WHERE'] = 'train'
chunk['WHERE'] = 'val'
sub['WHERE'] = 'test'
data = tr.append([chunk, sub] )<count_unique_values>
test_sets = []
test_set = ['pa','pg','ph']
for i in range(5):
this_set = [n + str(i)for n in test_set]
test_sets.append(this_set)
 test_sets
print(tr.shape, chunk.shape, sub.shape, data.shape)
 print(tr.Patient.nunique() , chunk.Patient.nunique() , sub.Patient.nunique() ,
data.Patient.nunique() )<feature_engineering>
yps = []
MAXCONF =.996
Xtr = df.drop('actual',axis=1 ).copy()
ytr = df.actual.copy()
X_train_adjust = Xtr
X_train_adjust.loc[Xtr.ph>MAXCONF,'ph'] = MAXCONF
X_train_logit = X_train_adjust.apply(logit)
 lr.fit(X_train_logit, ytr)
 for tset in test_sets:
Xte = all_test_probs[tset].copy()
Xte.columns = test_set
X_test_adjust = Xte
X_test_adjust.loc[Xte.ph>MAXCONF,'ph'] = MAXCONF
X_test_logit = X_test_adjust.apply(logit)
 yp = lr.predict_proba(X_test_logit)[:,1]
yps.append(yp)
 len(yps), [len(y)for y in yps]
data['min_week'] = data['Weeks']
data.loc[data.WHERE=='test','min_week'] = np.nan
data['min_week'] = data.groupby('Patient')['min_week'].transform('min' )<categorify>
p = pd.DataFrame(yps,columns=Xte.index ).transpose().apply(logit ).mean(axis=1 ).apply(expit)
 class_preds = pd.DataFrame(p,columns=['prob'])
 class_preds.index.name = 'patientId'
print(class_preds.shape)
 class_preds.head()
base = data.loc[data.Weeks == data.min_week]
base = base[['Patient','FVC']].copy()
base.columns = ['Patient','min_FVC']
base['nb'] = 1
base['nb'] = base.groupby('Patient')['nb'].transform('cumsum')
 base = base[base.nb==1]
base.drop('nb', axis=1, inplace=True )<merge>
phase4_test_output = class_preds
data = data.merge(base, on='Patient', how='left')
 data['base_week'] = data['Weeks'] - data['min_week']
del base<data_type_conversions>
yps = []
ids = []
MAXCONF =.995
folds = [p0,p1,p2,p3,p4]
for i in range(len(folds)) :
fs = folds.copy()
te = fs.pop(i)
 tr = np.concatenate(fs)
 Xtr = df.loc[tr,:].drop('actual',axis=1 ).copy()
Xte = df.loc[te,:].drop('actual',axis=1 ).copy()
ytr = df.actual[tr].copy()
yte = df.actual[te].copy()
X_train_adjust = Xtr
X_test_adjust = Xte
X_train_adjust.loc[Xtr.ph>MAXCONF,'ph'] = MAXCONF
X_test_adjust.loc[Xte.ph>MAXCONF,'ph'] = MAXCONF
X_train_logit = X_train_adjust.apply(logit)
 X_test_logit = X_test_adjust.apply(logit)
 lr.fit(X_train_logit, ytr)
 yp = lr.predict_proba(X_test_logit)[:,1]
yps = yps + list(yp)
 ids = ids + list(Xte.index.values )
COLS = ['Sex','SmokingStatus']
FE = []
for col in COLS:
for mod in data[col].unique() :
FE.append(mod)
 data[mod] =(data[col] == mod ).astype(int )<feature_engineering>
oof_class_preds = pd.DataFrame({'prob':yps},index=ids)
 oof_class_preds.index.name = 'patientId'
print(oof_class_preds.shape)
 oof_class_preds.head()
data['age'] =(data['Age'] - data['Age'].min())/(data['Age'].max() - data['Age'].min())
 data['BASE'] =(data['min_FVC'] - data['min_FVC'].min())/(data['min_FVC'].max() - data['min_FVC'].min())
 data['week'] =(data['base_week'] - data['base_week'].min())/(data['base_week'].max() - data['base_week'].min())
 data['percent'] =(data['Percent'] - data['Percent'].min())/(data['Percent'].max() - data['Percent'].min())
 FE += ['age','percent','week','BASE']<filter>
phase4_oof_output = oof_class_preds
tr = data.loc[data.WHERE=='train']
chunk = data.loc[data.WHERE=='val']
sub = data.loc[data.WHERE=='test']
del data<compute_test_metric>
df1 = phase2_output
df2 = pd.read_csv(UNET_RESULTS_PATH ).rename(columns={
'predictionString':'PredictionString'} ).set_index('patientId')
 class_preds = phase4_test_output
class_probs = class_preds.prob.to_dict()
df1_cases = df1[~df1.PredictionString.isnull() ]
df2_cases = df2[~df2.PredictionString.isnull() ]
df1_pos_ids = df1_cases.index.values
df2_pos_ids = df2_cases.index.values
C1, C2 = tf.constant(70, dtype='float32'), tf.constant(1000, dtype="float32")
 def score(y_true, y_pred):
tf.dtypes.cast(y_true, tf.float32)
 tf.dtypes.cast(y_pred, tf.float32)
 sigma = y_pred[:, 2] - y_pred[:, 0]
fvc_pred = y_pred[:, 1]
sigma_clip = tf.maximum(sigma, C1)
 delta = tf.abs(y_true[:, 0] - fvc_pred)
 delta = tf.minimum(delta, C2)
 sq2 = tf.sqrt(tf.dtypes.cast(2, dtype=tf.float32))
metric =(delta / sigma_clip)*sq2 + tf.math.log(sigma_clip* sq2)
 return K.mean(metric)
 def qloss(y_true, y_pred):
qs = [0.2, 0.50, 0.8]
q = tf.constant(np.array([qs]), dtype=tf.float32)
 e = y_true - y_pred
v = tf.maximum(q*e,(q-1)*e)
 return K.mean(v)
 def mloss(_lambda):
def loss(y_true, y_pred):
return _lambda * qloss(y_true, y_pred)+(1 - _lambda)*score(y_true, y_pred)
 return loss
def make_model(nh):
z = L.Input(( nh,), name="Patient")
 x = L.Dense(100, activation="relu", name="d1" )(z)
 x = L.Dense(100, activation="relu", name="d2" )(x)
 p1 = L.Dense(3, activation="linear", name="p1" )(x)
 p2 = L.Dense(3, activation="relu", name="p2" )(x)
 preds = L.Lambda(lambda x: x[0] + tf.cumsum(x[1], axis=1),
name="preds" )([p1, p2])
 model = M.Model(z, preds, name="CNN")
 model.compile(loss=mloss(0.65), optimizer=tf.keras.optimizers.Adam(lr=0.1, beta_1=0.9, beta_2=0.999, epsilon=None, decay=0.01, amsgrad=False), metrics=[score])
 return model<prepare_x_and_y>
df1_only_dict = {}
for p in df1_pos_ids:
if not p in df2_pos_ids:
df1_only_dict[p] = float(df1_cases.loc[p,'PredictionString'].split(' ')[0])
 df2_only_dict = {}
for p in df2_pos_ids:
if not p in df1_pos_ids:
df2_only_dict[p] = float(df2_cases.loc[p,'PredictionString'].split(' ')[0])
 candidates = pd.DataFrame(pd.Series(df2_only_dict,name='conf')).join(class_preds,how='left')
 accepted = candidates[(candidates.prob>UNET_MINPROB)&(candidates.conf>UNET_MINCONF)
 ].index.values
y = tr['FVC'].values
z = tr[FE].values
ze = sub[FE].values
nh = z.shape[1]
pe = np.zeros(( ze.shape[0], 3))
pred = np.zeros(( z.shape[0], 3))<choose_model_class>
df_out = df1.copy()
for p,r in df1.iterrows() :
if p in accepted:
df_out.loc[p,'PredictionString'] = df2.loc[p,'PredictionString']
phase5_output = df_out
phase5_output.head()
NFOLD = 5
kf = KFold(n_splits=NFOLD )<predict_on_test>
yolo_epochs = [1500, 2000, 3200, 4100, 5300, 6700, 10000, 13700]
%%time
cnt = 0
EPOCHS = 855
for tr_idx, val_idx in kf.split(z):
cnt += 1
print(f"FOLD {cnt}")
 net = make_model(nh)
 net.fit(z[tr_idx], y[tr_idx], batch_size=BATCH_SIZE, epochs=EPOCHS,
validation_data=(z[val_idx], y[val_idx]), verbose=0)
 print("train", net.evaluate(z[tr_idx], y[tr_idx], verbose=0, batch_size=BATCH_SIZE))
print("val", net.evaluate(z[val_idx], y[val_idx], verbose=0, batch_size=BATCH_SIZE))
print("predict val...")
 pred[val_idx] = net.predict(z[val_idx], batch_size=BATCH_SIZE, verbose=0)
 print("predict test...")
 pe += net.predict(ze, batch_size=BATCH_SIZE, verbose=0)/ NFOLD<compute_test_metric>
df1 = phase5_output
dfs = []
for eps in yolo_epochs:
yolo_stem = YOLO_SUB_STEM2 if(eps in MORE_YOLO_EPOCHS)else YOLO_SUB_STEM
dfs.append(
 pd.read_csv(RESULTS_LOC+'/'+yolo_stem+str(eps)+'.csv' ).set_index('patientId'))
class_preds = phase4_test_output
class_probs = class_preds.prob.to_dict()
df1_cases = df1[~df1.PredictionString.isnull() ]
df1_pos_ids = df1_cases.index.values
included = df1_pos_ids.tolist()
df_out = df1.copy()
sigma_opt = mean_absolute_error(y, pred[:, 1])
 unc = pred[:,2] - pred[:, 0]
sigma_mean = np.mean(unc)
 print(sigma_opt, sigma_mean )<prepare_output>
while True:
for df2 in dfs:
df2_cases = df2[~df2.PredictionString.isnull() ]
df2_pos_ids = df2_cases.index.values
df2_only_dict = {}
outstring_dict = {}
for p in df2_pos_ids:
if not p in included:
maxconf = 0
boxes = []
confs = []
s = df2.loc[p,'PredictionString'].split(' ')
 if s[-1]=='':
s.pop()
if s[0]=='':
s.pop(0)
 if(len(s)%5):
print('Bad prediction string.')
 while len(s):
conf = float(s.pop(0))
x = int(round(float(s.pop(0))))
y = int(round(float(s.pop(0))))
w = int(round(float(s.pop(0))))
h = int(round(float(s.pop(0))))
if(conf>YOLO_MINCONF):
boxes.append([x,y,w,h])
 confs.append(conf)
 if conf>maxconf:
maxconf = conf
predictionString = ''
if len(boxes):
for box, conf in zip(boxes, confs):
x, y, w, h = box
predictionString += '{:6.4f} '.format(conf)
 predictionString += str(x)+ ' ' + str(y)+ ' ' + str(w)+ ' ' + str(h)+ ' '
df2_only_dict[p] = maxconf*class_probs[p]
outstring_dict[p] = predictionString
candidates = pd.DataFrame(pd.Series(df2_only_dict,name='prod'))
best = candidates.sort_values('prod' ).tail(1 ).index.values[0]
print(best,candidates.loc[best,'prod'])
 df_out.loc[best,'PredictionString'] = outstring_dict[best]
included.append(best)
 minprob = class_preds.loc[included[-8:],:].prob.min()
if minprob<MINMIN:
break
sub['FVC1'] = 1.*pe[:, 1]
sub['Confidence1'] = pe[:, 2] - pe[:, 0]
subm = sub[['Patient_Week','FVC','Confidence','FVC1','Confidence1']].copy()
subm.loc[~subm.FVC1.isnull() ].head(10 )<feature_engineering>
class_preds.loc[included[-32:],:]
<load_from_csv><EOS> 
df_out.to_csv('phase6_output.csv' )