{"metadata":{"kernelspec":{"language":"python","display_name":"Python 3","name":"python3"},"language_info":{"pygments_lexer":"ipython3","nbconvert_exporter":"python","version":"3.6.4","file_extension":".py","codemirror_mode":{"name":"ipython","version":3},"name":"python","mimetype":"text/x-python"}},"nbformat_minor":4,"nbformat":4,"cells":[{"cell_type":"markdown","source":"Further information can be found in [this post](https://www.kaggle.com/competitions/uw-madison-gi-tract-image-segmentation/discussion/337196)\n\nIn summary, the key points are:\n* Group folds by case\n* Stratify by the different possible combinations of labeled organs\n* Implement a good version of the evaluation metric","metadata":{}},{"cell_type":"markdown","source":"# Parameters","metadata":{}},{"cell_type":"code","source":"N_FOLDS = 5\nSEED = 42","metadata":{"execution":{"iopub.status.busy":"2022-07-15T01:02:39.557918Z","iopub.execute_input":"2022-07-15T01:02:39.558360Z","iopub.status.idle":"2022-07-15T01:02:39.564666Z","shell.execute_reply.started":"2022-07-15T01:02:39.558324Z","shell.execute_reply":"2022-07-15T01:02:39.563242Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Imports","metadata":{}},{"cell_type":"code","source":"import pandas as pd\nfrom sklearn.model_selection import StratifiedGroupKFold","metadata":{"execution":{"iopub.status.busy":"2022-07-15T01:02:39.578491Z","iopub.execute_input":"2022-07-15T01:02:39.579042Z","iopub.status.idle":"2022-07-15T01:02:39.585498Z","shell.execute_reply.started":"2022-07-15T01:02:39.578984Z","shell.execute_reply":"2022-07-15T01:02:39.584091Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Load data","metadata":{}},{"cell_type":"code","source":"df = pd.read_csv('../input/uw-madison-gi-tract-image-segmentation/train.csv')\ndf['segmentation'] = df.segmentation.fillna('')\ndf['case_day'] = df['id'].str.split('_').str[0] + '_' + df['id'].str.split('_').str[1]","metadata":{"execution":{"iopub.status.busy":"2022-07-15T01:02:39.622671Z","iopub.execute_input":"2022-07-15T01:02:39.623181Z","iopub.status.idle":"2022-07-15T01:02:41.002464Z","shell.execute_reply.started":"2022-07-15T01:02:39.623136Z","shell.execute_reply":"2022-07-15T01:02:41.001347Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"for class_annotated in ('large_bowel', 'small_bowel', 'stomach'):\n    df['class_' + class_annotated] = 1*((df['class'] == class_annotated) & (df['segmentation'] != ''))","metadata":{"execution":{"iopub.status.busy":"2022-07-15T01:02:41.005153Z","iopub.execute_input":"2022-07-15T01:02:41.005620Z","iopub.status.idle":"2022-07-15T01:02:41.068779Z","shell.execute_reply.started":"2022-07-15T01:02:41.005556Z","shell.execute_reply":"2022-07-15T01:02:41.067291Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"information_by_id = pd.DataFrame()\ninformation_by_id['annotations'] = df.groupby('id').aggregate({'class_large_bowel': 'sum', 'class_small_bowel': 'sum', 'class_stomach': 'sum'}).apply(lambda x: ''.join([str(annotated) for annotated in x]), axis=1)\ninformation_by_id['case_id'] = information_by_id.index.str.split('_').str[0].str[len('case'):]","metadata":{"execution":{"iopub.status.busy":"2022-07-15T01:02:41.070356Z","iopub.execute_input":"2022-07-15T01:02:41.070717Z","iopub.status.idle":"2022-07-15T01:02:41.660496Z","shell.execute_reply.started":"2022-07-15T01:02:41.070682Z","shell.execute_reply":"2022-07-15T01:02:41.659222Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"information_by_id.head()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T01:02:41.662914Z","iopub.execute_input":"2022-07-15T01:02:41.663361Z","iopub.status.idle":"2022-07-15T01:02:41.685128Z","shell.execute_reply.started":"2022-07-15T01:02:41.663328Z","shell.execute_reply":"2022-07-15T01:02:41.684079Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"information_by_id.annotations.value_counts()","metadata":{"execution":{"iopub.status.busy":"2022-07-15T01:02:41.686778Z","iopub.execute_input":"2022-07-15T01:02:41.687538Z","iopub.status.idle":"2022-07-15T01:02:41.707408Z","shell.execute_reply.started":"2022-07-15T01:02:41.687496Z","shell.execute_reply":"2022-07-15T01:02:41.706353Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Get fold for each ID","metadata":{}},{"cell_type":"code","source":"skf = StratifiedGroupKFold(n_splits=N_FOLDS, shuffle=True, random_state=SEED)\nfor fold, (train_index, fold_index) in enumerate(skf.split(X=information_by_id.index, y=information_by_id.annotations, groups=information_by_id.case_id)):\n    fold_ids = information_by_id.iloc[fold_index].index\n    information_by_id.loc[fold_ids, 'fold'] = fold\n    df.loc[df.id.isin(fold_ids), 'fold'] = fold","metadata":{"execution":{"iopub.status.busy":"2022-07-15T01:02:41.709093Z","iopub.execute_input":"2022-07-15T01:02:41.709683Z","iopub.status.idle":"2022-07-15T01:02:41.963794Z","shell.execute_reply.started":"2022-07-15T01:02:41.709643Z","shell.execute_reply":"2022-07-15T01:02:41.962632Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"information_by_id.fold = information_by_id.fold.astype(int)\ndf.fold = df.fold.astype(int)","metadata":{"execution":{"iopub.status.busy":"2022-07-15T01:02:41.965367Z","iopub.execute_input":"2022-07-15T01:02:41.965780Z","iopub.status.idle":"2022-07-15T01:02:41.972971Z","shell.execute_reply.started":"2022-07-15T01:02:41.965704Z","shell.execute_reply":"2022-07-15T01:02:41.971878Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Check distribution","metadata":{}},{"cell_type":"code","source":"information_by_id.groupby('fold').annotations.value_counts().sort_index().plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2022-07-15T01:02:41.974549Z","iopub.execute_input":"2022-07-15T01:02:41.975439Z","iopub.status.idle":"2022-07-15T01:02:42.377223Z","shell.execute_reply.started":"2022-07-15T01:02:41.975395Z","shell.execute_reply":"2022-07-15T01:02:42.376083Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"information_by_id.groupby('fold').annotations.value_counts(normalize=True).sort_index().plot(kind='bar')","metadata":{"execution":{"iopub.status.busy":"2022-07-15T01:02:42.379553Z","iopub.execute_input":"2022-07-15T01:02:42.380620Z","iopub.status.idle":"2022-07-15T01:02:42.746649Z","shell.execute_reply.started":"2022-07-15T01:02:42.380548Z","shell.execute_reply":"2022-07-15T01:02:42.745546Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"markdown","source":"# Store train with folds","metadata":{}},{"cell_type":"code","source":"folds = df.drop(columns=['class', 'segmentation']).groupby('id').agg({'fold': 'min'})\nfolds","metadata":{"execution":{"iopub.status.busy":"2022-07-15T01:03:22.517006Z","iopub.execute_input":"2022-07-15T01:03:22.518677Z","iopub.status.idle":"2022-07-15T01:03:22.587331Z","shell.execute_reply.started":"2022-07-15T01:03:22.518556Z","shell.execute_reply":"2022-07-15T01:03:22.585797Z"},"trusted":true},"execution_count":null,"outputs":[]},{"cell_type":"code","source":"folds.to_csv('train_folds.csv')","metadata":{"execution":{"iopub.status.busy":"2022-07-15T01:03:13.168480Z","iopub.execute_input":"2022-07-15T01:03:13.169053Z","iopub.status.idle":"2022-07-15T01:03:13.215715Z","shell.execute_reply.started":"2022-07-15T01:03:13.168994Z","shell.execute_reply":"2022-07-15T01:03:13.214655Z"},"trusted":true},"execution_count":null,"outputs":[]}]}