Kaggle competition: Mechanisms of Action (MoA) Prediction
def repeat_2(a):
return np.tile(a,a.shape )<train_model>
warnings.filterwarnings('ignore' )
task = get_data(str(evaluation_path / evaluation_tasks[325]))
check_p(task, resize_c )<train_model>
train_features = pd.read_csv('.. /input/lish-moa/train_features.csv')
 train_targets_scored = pd.read_csv('.. /input/lish-moa/train_targets_scored.csv')
 train_targets_nonscored = pd.read_csv('.. /input/lish-moa/train_targets_nonscored.csv')
 test_features = pd.read_csv('.. /input/lish-moa/test_features.csv')
 sample_submission = pd.read_csv('.. /input/lish-moa/sample_submission.csv')
 train_drug = pd.read_csv(".. /input/lish-moa/train_drug.csv")
task = get_data(str(evaluation_path / evaluation_tasks[148]))
check_p(task, resize_2 )<train_model>
params = {"n_genes_pca": 50,
"n_cells_pca": 20,
"batch_size": 256,
"lr": 1e-3,
"weight_decay": 1e-5,
"n_folds": 5,
"early_stopping_steps": 5,
"hidden_size": 512,
"boost_rate": 1.0,
"num_nets": 20,
"epochs_per_stage": 1,
"correct_epoch": 1,
"model_order": "second"
}
task = get_data(str(training_path / training_tasks[222]))
check_p(task, resize_o )<define_variables>
GENES = [col for col in train_features.columns if col.startswith('g-')]
CELLS = [col for col in train_features.columns if col.startswith('c-')]
task = get_data(str(evaluation_path / evaluation_tasks[311]))
check_p(task, repeat_2 )<train_model>
def seed_everything(seed=42):
random.seed(seed)
 os.environ['PYTHONHASHSEED'] = str(seed)
 np.random.seed(seed)
 torch.manual_seed(seed)
 torch.cuda.manual_seed(seed)
 torch.backends.cudnn.deterministic = True
seed_everything(seed=42 )
task = get_data(str(training_path / training_tasks[288]))
check_p(task, resize_o )<define_variables>
n_comp = params["n_genes_pca"]
data = pd.concat([pd.DataFrame(train_features[GENES]), pd.DataFrame(test_features[GENES])])
 data2 =(PCA(n_components=n_comp, random_state=42 ).fit_transform(data[GENES]))
train2 = data2[:train_features.shape[0]]; test2 = data2[-test_features.shape[0]:]
train2 = pd.DataFrame(train2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 test2 = pd.DataFrame(test2, columns=[f'pca_G-{i}' for i in range(n_comp)])
 train_features = pd.concat(( train_features, train2), axis=1)
 test_features = pd.concat(( test_features, test2), axis=1 )
task = get_data(str(evaluation_path / evaluation_tasks[251]))
check_p(task, repeat_1 )<define_variables>
n_comp = params["n_cells_pca"]
data = pd.concat([pd.DataFrame(train_features[CELLS]), pd.DataFrame(test_features[CELLS])])
 data2 =(PCA(n_components=n_comp, random_state=42 ).fit_transform(data[CELLS]))
train2 = data2[:train_features.shape[0]]; test2 = data2[-test_features.shape[0]:]
train2 = pd.DataFrame(train2, columns=[f'pca_C-{i}' for i in range(n_comp)])
 test2 = pd.DataFrame(test2, columns=[f'pca_C-{i}' for i in range(n_comp)])
 train_features = pd.concat(( train_features, train2), axis=1)
 test_features = pd.concat(( test_features, test2), axis=1 )
task = get_data(str(evaluation_path / evaluation_tasks[224]))
check_p(task, crop_max )<load_from_csv>
data = pd.concat([pd.DataFrame(train_features), pd.DataFrame(test_features)])
 data2 = pd.get_dummies(data, columns=["cp_time", "cp_dose"])
 train2 = data2[:train_features.shape[0]]; test2 = data2[-test_features.shape[0]:]
train_features = train2
test_features = test2
train_features
submission = pd.read_csv(data_path / 'sample_submission.csv', index_col='output_id' )<define_variables>
feature_cols = train_features.columns[4:].tolist()
params["feat_d"] = len(feature_cols)
 train_features
for output_id in tqdm(submission.index):
task_id = output_id.split('_')[0]
pair_id = int(output_id.split('_')[1])
 f = test_path / str(task_id + '.json')
 if f.is_file() :
task = get_data(f)
 data = task['test'][pair_id]['input']
pred_1 = flattener(data)
 for oper in ['crop_min','crop_max','resize_o','resize_c','resize_2','repeat_1','repeat_2']:
data = train(task, globals() [oper])
 if data:
pred_1 = flattener(data)
 break
data = task['test'][pair_id]['input']
pred_2 = flattener(data)
 data = [[5 if i==0 else i for i in j] for j in data]
pred_3 = flattener(data)
 pred = pred_1 + ' ' + pred_2 + ' ' + pred_3 + ' '
submission.loc[output_id, 'output'] = pred<save_to_csv>
train = train_features.merge(train_targets_scored, on='sig_id')
 train = train[train['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 test = test_features[test_features['cp_type']!='ctl_vehicle'].reset_index(drop=True)
 target = train[train_targets_scored.columns]
submission.to_csv('submission.csv' )<import_modules>
train = train.drop('cp_type', axis=1)
 test = test.drop('cp_type', axis=1 )
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
from matplotlib import colors
import os
import json
import random
import warnings
import datetime<create_dataframe>
target_cols = target.drop('sig_id', axis=1 ).columns.values.tolist()
def get_filenames(directory):

 return [os.path.join(dirname, filename)for dirname, _, filenames in os.walk(directory)for filename in filenames]
def import_json_to_df(filename):

 with open(filename, 'r')as f:
task = json.load(f)
 result = []
for key in task:
df = pd.DataFrame(task[key])
 df['task_id'] = os.path.splitext(os.path.basename(filename)) [0]
if key == 'test':
df['submission_id'] = df.apply(lambda x: f"{x['task_id']}_{x.name}", axis=1)
 df['type'] = key
result.append(df)
 result = pd.concat(result, ignore_index=True, sort=False)
 result['input'] = result['input'].apply(np.array)
 result['output'] = result['output'].apply(np.array)
 return result
data_dir = '/kaggle/input/abstraction-and-reasoning-challenge/'
submission = pd.read_csv(os.path.join(data_dir, 'sample_submission.csv'))
training_files = sorted(get_filenames(os.path.join(data_dir, 'training')))
 evaluation_files = sorted(get_filenames(os.path.join(data_dir, 'evaluation')))
 test_files = sorted(get_filenames(os.path.join(data_dir, 'test')))
 train = pd.concat([import_json_to_df(f)for f in training_files], ignore_index=True, sort=False)
 validation = pd.concat([import_json_to_df(f)for f in evaluation_files], ignore_index=True, sort=False)
 test = pd.concat([import_json_to_df(f)for f in test_files], ignore_index=True, sort=False)
 train.shape, validation.shape, test.shape, submission.shape
print(f'Train: {train.shape}')
 print(f'Validation: {validation.shape}')
 print(f'Test: {test.shape}')
 print(f'Submission: {submission.shape}' )<filter>
folds_ = train.copy()
folds = []
train_feats = train_features
scored = target
drug = train_drug
drug = drug.loc[train_feats['cp_type'] == 'trt_cp', :]
targets = target_cols
scored = scored.merge(drug, on='sig_id', how='left')
 vc = scored.drug_id.value_counts()
vc1 = vc.loc[vc <= 18].index.sort_values()
vc2 = vc.loc[vc > 18].index.sort_values()
dct1 = {}; dct2 = {}
skf = MultilabelStratifiedKFold(n_splits = params["n_folds"], shuffle = True, random_state = 0)
 tmp = scored.groupby('drug_id')[targets].mean().loc[vc1]
for fold,(idxT,idxV)in enumerate(skf.split(tmp,tmp[targets])) :
dd = {k:fold for k in tmp.index[idxV].values}
dct1.update(dd)
 skf = MultilabelStratifiedKFold(n_splits = params["n_folds"], shuffle = True, random_state = 0)
 tmp = scored.loc[scored.drug_id.isin(vc2)].reset_index(drop = True)
 for fold,(idxT,idxV)in enumerate(skf.split(tmp,tmp[targets])) :
dd = {k:fold for k in tmp.sig_id[idxV].values}
dct2.update(dd)
 scored['fold'] = scored.drug_id.map(dct1)
 scored.loc[scored.fold.isna() ,'fold'] =\
scored.loc[scored.fold.isna() ,'sig_id'].map(dct2)
 scored.fold = scored.fold.astype('int8')
 folds.append(scored.fold.values)
 del scored['fold']
s = np.stack(folds)
 train["kfold"] = s.reshape(-1,)
 train
print('Example with 1 test pair')
 display(train[train['task_id']=='007bbfb7'])
 print('Example with 2 test pairs')
 display(train[train['task_id']=='e9614598'] )<set_options>
class MoADataset:
def __init__(self, features, targets):
self.features = features
self.targets = targets
def __len__(self):
return(self.features.shape[0])
 def __getitem__(self, idx):
dct = {
'x' : torch.tensor(self.features[idx, :] , dtype=torch.float),
'y' : torch.tensor(self.targets[idx, :], dtype=torch.float)
 }
return dct
class TestDataset:
def __init__(self, features):
self.features = features
def __len__(self):
return(self.features.shape[0])
 def __getitem__(self, idx):
dct = {
'x' : torch.tensor(self.features[idx, :], dtype=torch.float)
 }
return dct
warnings.filterwarnings('ignore')
 pd.set_option('display.max_colwidth', -1)
 SEED = 13
random.seed(SEED)
 np.random.seed(SEED)
 MAX_SIZE = 30
PAD_ID = -1
PAD_COLOR = '
ORG_COLOR_MAP = {0:'
5:'
NUM_COLORS = len(ORG_COLOR_MAP)
 COLOR_MAP = {PAD_ID:PAD_COLOR}
COLOR_MAP.update(ORG_COLOR_MAP)
 DEFAULT_PREDICTION = [[0]]
DEFAULT_SUBMISSION = '|0| |0| |0|'
DF_NAME = {id(train): 'train', id(validation): 'validation', id(test): 'test'}<compute_test_metric>
class ForwardType(Enum):
SIMPLE = 0
STACKED = 1
CASCADE = 2
GRADIENT = 3
class DynamicNet(object):
def __init__(self, c0, lr):
self.models = []
self.c0 = c0
self.lr = lr
self.boost_rate = nn.Parameter(torch.tensor(lr, requires_grad=True, device="cuda"))
def add(self, model):
self.models.append(model)
 def parameters(self):
params = []
for m in self.models:
params.extend(m.parameters())
 params.append(self.boost_rate)
 return params
def zero_grad(self):
for m in self.models:
m.zero_grad()
def to_cuda(self):
for m in self.models:
m.cuda()
def to_eval(self):
for m in self.models:
m.eval()
def to_train(self):
for m in self.models:
m.train(True)
 def forward(self, x):
if len(self.models)== 0:
batch = x.shape[0]
c0 = np.repeat(self.c0.detach().cpu().numpy().reshape(1,-1), batch, axis=0)
 return None, torch.Tensor(c0 ).cuda()
middle_feat_cum = None
prediction = None
with torch.no_grad() :
for m in self.models:
if middle_feat_cum is None:
middle_feat_cum, prediction = m(x, middle_feat_cum)
 else:
middle_feat_cum, pred = m(x, middle_feat_cum)
 prediction += pred
return middle_feat_cum, self.c0 + self.boost_rate * prediction
def forward_grad(self, x):
if len(self.models)== 0:
batch = x.shape[0]
c0 = np.repeat(self.c0.detach().cpu().numpy().reshape(1, -1), batch, axis=0)
 return None, torch.Tensor(c0 ).cuda()
middle_feat_cum = None
prediction = None
for m in self.models:
if middle_feat_cum is None:
middle_feat_cum, prediction = m(x, middle_feat_cum)
 else:
middle_feat_cum, pred = m(x, middle_feat_cum)
 prediction += pred
return middle_feat_cum, self.c0 + self.boost_rate * prediction
@classmethod
def from_file(cls, path, builder):
d = torch.load(path)
 net = DynamicNet(d['c0'], d['lr'])
 net.boost_rate = d['boost_rate']
for stage, m in enumerate(d['models']):
submod = builder(stage)
 submod.load_state_dict(m)
 net.add(submod)
 return net
def to_file(self, path):
models = [m.state_dict() for m in self.models]
d = {'models': models, 'c0': self.c0, 'lr': self.lr, 'boost_rate': self.boost_rate}
torch.save(d, path )
def calculate_score(truth, pred1, pred2=None, pred3=None):

 result = []
result.append([np.array_equal(truth[i], pred1[i])for i in range(len(truth)) ])
 if pred2 is not None:
result.append([np.array_equal(truth[i], pred2[i])for i in range(len(truth)) ])
 if pred3 is not None:
result.append([np.array_equal(truth[i], pred3[i])for i in range(len(truth)) ])
 result = np.vstack(result)
 result = np.any(result, axis=0)
 score =(len(result)-np.sum(result)) /len(result)
 print(f'Score: {np.round(score, 4)}(Found {np.sum(result)}/{len(result)})')
 return score
def prediction_to_submission(pred1, pred2=None, pred3=None):

 def list_to_str(pred):
return '|'+'|'.join([''.join(map(str, r)) for r in pred])+'|'
result = list_to_str(pred1)
 if pred2 is not None:
result += ' '+list_to_str(pred2)
 else:
result += ' '+list_to_str(DEFAULT_PREDICTION)
 if pred3 is not None:
result += ' '+list_to_str(pred3)
 else:
result += ' '+list_to_str(DEFAULT_PREDICTION)
 return result
def submission_to_prediction(sub):

 pred = [np.array([list(map(int, list(s)))for s in sub.split() [i].split('|')[1:-1]])for i in range(3)]
return pred[0], pred[1], pred[2]
def image_padding(img, size=(MAX_SIZE, MAX_SIZE), pad_int=-1):

 top = int(np.ceil(( size[0] - img.shape[0])/2))
bottom = int(np.floor(( size[0] - img.shape[0])/2))
left = int(np.ceil(( size[1] - img.shape[1])/2))
right = int(np.floor(( size[1] - img.shape[1])/2))
return np.pad(img,(( top, bottom),(left, right)) , constant_values=pad_int)
 def remove_padding(img, pad_int=-1):

 row_pad = np.all(img == pad_int, axis=1)
 img = img[~row_pad,:]
col_pad = np.all(img == pad_int, axis=0)
 img = img[:,~col_pad]
return img
def plot_task(df, task_id):

 cmap = colors.ListedColormap(COLOR_MAP.values())
 norm = colors.Normalize(vmin=-1, vmax=9)
 df_name = DF_NAME[id(df)]
df = df[df['task_id'] == task_id].sort_values('type', ascending=False ).reset_index()
fig, axs = plt.subplots(2, df.shape[0], figsize=(3*df.shape[0],6))
axs[0][0].text(0,-10, f'Task ID: {task_id}({df_name})', fontsize=16)
 for idx, row in df.iterrows() :
input_img = row['input']
input_img = image_padding(input_img)
 output_img = row['output']
if np.isnan(output_img ).any() :
output_img = np.array([[]])
 output_img = image_padding(output_img)
 task_type = row['type'].upper()
axs[0][idx].imshow(input_img, cmap=cmap, norm=norm)
 axs[0][idx].set_yticklabels([])
 axs[0][idx].set_xticklabels([])
 axs[0][idx].set_title(f'{task_type}
Input', color='green' if task_type == 'TRAIN' else 'blue', fontsize=16)
 axs[1][idx].imshow(output_img, cmap=cmap, norm=norm)
 axs[1][idx].set_yticklabels([])
 axs[1][idx].set_xticklabels([])
 axs[1][idx].set_title('Output', color='green' if task_type == 'TRAIN' else 'blue', fontsize=16)
 plt.tight_layout()
plt.show()<prepare_output>
class MLP_1HL(nn.Module):
def __init__(self, dim_in, dim_hidden1, dim_hidden2, sparse=False, bn=True):
super(MLP_1HL, self ).__init__()
self.layer1 = nn.Sequential(
 nn.Dropout(0.2),
nn.Linear(dim_in, dim_hidden1), 
)
 self.layer2 = nn.Sequential(
 nn.ReLU() ,
nn.Linear(dim_hidden1, 206))
if bn:
self.bn = nn.BatchNorm1d(dim_hidden1)
 self.bn2 = nn.BatchNorm1d(dim_in)
 def forward(self, x, lower_f):
if lower_f is not None:
x = torch.cat([x, lower_f], dim=1)
 x = self.bn2(x)
 out = self.layer1(x)
 return out, self.layer2(out)
 @classmethod
def get_model(cls, stage, params):
if stage == 0:
dim_in = params["feat_d"]
else:
dim_in = params["feat_d"] + params["hidden_size"]
model = MLP_1HL(dim_in, params["hidden_size"], params["hidden_size"])
 return model
class MLP_2HL(nn.Module):
def __init__(self, dim_in, dim_hidden1, dim_hidden2, sparse=False, bn=True):
super(MLP_2HL, self ).__init__()
self.bn2 = nn.BatchNorm1d(dim_in)
 self.layer1 = nn.Sequential(
 nn.Dropout(0.2),
nn.Linear(dim_in, dim_hidden1),
nn.ReLU() ,
nn.BatchNorm1d(dim_hidden1),
nn.Dropout(0.4),
nn.Linear(dim_hidden1, dim_hidden2)
)
 self.layer2 = nn.Sequential(
 nn.ReLU() ,
nn.Linear(dim_hidden2, 206)
)
 def forward(self, x, lower_f):
if lower_f is not None:
x = torch.cat([x, lower_f], dim=1)
 x = self.bn2(x)
 middle_feat = self.layer1(x)
 out = self.layer2(middle_feat)
 return middle_feat, out
@classmethod
def get_model(cls, stage, params):
if stage == 0:
dim_in = params["feat_d"]
else:
dim_in = params["feat_d"] + params["hidden_size"]
model = MLP_2HL(dim_in, params["hidden_size"], params["hidden_size"])
 return model
pred = np.array([[1,0,1], [0,1,0], [1,0,1], [1,1,1]])
 print(f'Original Prediciton:
{pred}')
 pred_sub = prediction_to_submission(pred)
 print(f'
Prediction to Submission:
{pred_sub}')
 sub_pred = submission_to_prediction(pred_sub)[0]
print(f'
Submission to Prediction:
{sub_pred}' )<compute_test_metric>
class SmoothBCEwLogits(_WeightedLoss):
def __init__(self, weight=None, reduction='mean', smoothing=0.0):
super().__init__(weight=weight, reduction=reduction)
 self.smoothing = smoothing
self.weight = weight
self.reduction = reduction
@staticmethod
def _smooth(targets:torch.Tensor, n_labels:int, smoothing=0.0):
assert 0 <= smoothing < 1
with torch.no_grad() :
targets = targets *(1.0 - smoothing)+ 0.5 * smoothing
return targets
def forward(self, inputs, targets):
targets = SmoothBCEwLogits._smooth(targets, inputs.size(-1),
self.smoothing)
 loss = F.binary_cross_entropy_with_logits(inputs, targets,self.weight)
 if self.reduction == 'sum':
loss = loss.sum()
elif self.reduction == 'mean':
loss = loss.mean()
return loss
for df in [train, validation, test]:
print(f'DataFrame: {DF_NAME[id(df)]}')
 score = calculate_score(df[df['type'] == 'test']['output'].values, [[[0]]]*df.shape[0], [[[1]]]*df.shape[0], [[[2]]]*df.shape[0] )<categorify>
def get_optim(params, lr, weight_decay):
optimizer = optim.Adam(params, lr, weight_decay=weight_decay)
 return optimizer
def logloss(net_ensemble, test_loader):
loss = 0
total = 0
loss_f = nn.BCEWithLogitsLoss()
for data in test_loader:
x = data["x"].cuda()
y = data["y"].cuda()
with torch.no_grad() :
_, out = net_ensemble.forward(x)
 loss += loss_f(out, y)
 total += 1
return loss / total
def get_features(row):
def increased_height() :

 if row['type'] == 'test':
return None
else:
return row['output'].shape[0] / row['input'].shape[0] - 1
def increased_width() :

 if row['type'] == 'test':
return None
else:
return row['output'].shape[1] / row['input'].shape[1] - 1
def color_ratio(frame):

 if np.isnan(frame ).any() :
return None
else:
result = {i:0.0 for i in range(NUM_COLORS)}
unique, counts = np.unique(frame, return_counts=True)
 counts = counts /(frame.shape[0] * frame.shape[1])
 result.update(dict(zip(unique, counts)))
 return result
def get_objects(img):

 if np.isnan(img ).any() :
return None
else:
org = img.copy()
img = image_padding(img,(img.shape[0]+2, img.shape[1]+2), 0)
 objects = []
while True:
start = np.where(img != 0)
 if len(start[0])== 0:
break
start_x = start[1][0]
start_y = start[0][0]
end_x = start_x
end_y = start_y
control = set([(start_y, start_x)])
 while len(control)> 0:
c = control.pop()
act_y = c[0]
act_x = c[1]
if act_y > end_y: end_y = act_y
if start_y > act_y: start_y = act_y
if act_x > end_x: end_x = act_x
if start_x > act_x: start_x = act_x
img[act_y, act_x] = 0
sub = img[act_y-1:act_y+2, act_x-1: act_x+2]
for n in zip(np.where(sub)[0], np.where(sub)[1]):
n =(n[0]-1+act_y, n[1]-1+act_x)
 img[n[0], n[1]] = 0
control = control | set([n])
 img[start_y:end_y+1, start_x:end_x+1] = 0
start_y -= 1; end_y -= 1; start_x -= 1; end_x -= 1
obj = org[start_y:end_y+1, start_x:end_x+1]
objects.append({'start':(start_y, start_x), 'obj':obj})
 return objects
def has_vertical_split(frame_in=row['input'], frame_out=row['output']):

 if row['type'] == 'test':
return False
if(np.isnan(frame_in ).any())|(np.isnan(frame_out ).any()):
return False
else:
if frame_in.shape[1] % 2 == 0:
return False
mid = int(np.floor(frame_in.shape[1] / 2))
mid_color = np.unique(frame_in[:,mid])
 if mid_color.shape[0] > 1:
return False
mid_color = mid_color[0]
if mid_color in np.delete(frame_in, mid, 1):
return False
if frame_out.shape[1] != mid:
return False
return True
def has_horizontal_split(frame_in=row['input'], frame_out=row['output']):

 if row['type'] == 'test':
return False
if(np.isnan(frame_in ).any())|(np.isnan(frame_out ).any()):
return False
else:
if frame_in.shape[0] % 2 == 0:
return False
mid = int(np.floor(frame_in.shape[0] / 2))
mid_color = np.unique(frame_in[mid,:])
 if mid_color.shape[0] > 1:
return False
mid_color = mid_color[0]
if mid_color in np.delete(frame_in, mid, 0):
return False
if frame_out.shape[0] != mid:
return False
return True
def is_subset(frame, subframe):

 if row['type'] == 'test':
return False
if(np.isnan(frame ).any())|(np.isnan(subframe ).any()):
return False
else:
if(subframe.shape[0] > frame.shape[0])|(subframe.shape[1] > frame.shape[1]):
return False
for i in range(frame.shape[0]-subframe.shape[0]+1):
for j in range(frame.shape[1]-subframe.shape[1]+1):
match = np.array_equal(subframe, frame[i:i+subframe.shape[0], j:j+subframe.shape[1]])
 if match:
return match
return False
def color_change() :

 if row['type'] == 'test':
return False
else:
frame1 = np.where(row['input'] > 0, 1, 0)
 frame2 = np.where(row['output'] > 0, 1, 0)
 return np.array_equal(frame1, frame2)
 def adapted_size() :

 if row['type'] == 'test':
return False
else:
adapted_input = np.resize(row['input'], row['output'].shape)
 adapted_input = np.array_equal(adapted_input, row['output'])
 return adapted_input
result = {
'increased_height': increased_height() ,
'increased_width': increased_width() ,
'color_ratio_input': color_ratio(row['input']),
'color_ratio_output': color_ratio(row['output']),
'input_objects': get_objects(row['input']),
'output_objects': get_objects(row['output']),
'color_change': color_change() ,
'has_vertical_split': has_vertical_split() ,
'has_horizontal_split': has_horizontal_split() ,
'input_in_output': is_subset(row['output'], row['input']),
'output_in_input': is_subset(row['input'], row['output']),
}
return result
def get_features2(row):
def check_fill_out() :

 if(row['increased_width'] == 0)&(row['increased_height'] == 0):
output = row['output'].copy()
blanks = np.where(row['input'] == 0, True, False)
 output[blanks == True] = 0
return np.array_equal(row['input'], output)
 else:
return False
def fill_out_mask_color() :

 if(row['increased_width'] == 0)&(row['increased_height'] == 0):
diff = np.unique(row['input'][row['input'] != row['output']])
 if len(diff)== 1:
return diff[0]
else:
return -1
else:
return -1
def is_most_common_color() :

 if(row['increased_width'] == 0)&(row['increased_height'] == 0):
mc_color = np.argmax(list(row['color_ratio_input'].values()))
return np.sum(np.where(row['output'] == mc_color, 1, 0)) == row['output'].shape[0]*row['output'].shape[1]
else:
return False
def unique_output_color() :

 if row['color_ratio_output'] is None:
return -1
else:
max_color = np.argmax(list(row['color_ratio_output'].values())[1:])+1
if len(np.unique(list(row['color_ratio_output'].values())[1:])) > 2:
return 0
else:
return max_color
result = {
'fill_out': check_fill_out() ,
'fill_out_mask_color': fill_out_mask_color() ,
'is_most_common_color': is_most_common_color() ,
'unique_output_color': unique_output_color()
}
return result
def get_features3(row):
def left_diagonal_symmetric(frame):

 if row['type'] == 'test':
return False
else:
return np.array_equal(frame, frame.T)
 def right_diagonal_symmetric(frame):

 if row['type'] == 'test':
return False
else:
return np.array_equal(frame, np.flip(frame ).T)
 def horizontal_symmetric(frame):

 if row['type'] == 'test':
return False
else:
return np.array_equal(frame, np.flipud(frame))
def vertical_symmetric(frame):

 if row['type'] == 'test':
return False
else:
return np.array_equal(frame, np.fliplr(frame))
result = {
'input_left_diagonal_symmetric': left_diagonal_symmetric(row['input']),
'input_right_diagonal_symmetric': right_diagonal_symmetric(row['input']),
'input_horizontal_symmetric': horizontal_symmetric(row['input']),
'input_vertical_symmetric': vertical_symmetric(row['input']),
'output_left_diagonal_symmetric': left_diagonal_symmetric(row['output']),
'output_right_diagonal_symmetric': right_diagonal_symmetric(row['output']),
'output_horizontal_symmetric': horizontal_symmetric(row['output']),
'output_vertical_symmetric': vertical_symmetric(row['output']),
}
return result
def same_array_size(df, task_id, column='input'):

 result = df[(df['type'] != 'test')&(df['task_id'] == task_id)][column].apply(lambda x: np.array(x ).shape ).unique()
return True if len(result)== 1 else False<data_type_conversions>
device = "cuda" if torch.cuda.is_available() else "cpu"
for df in [train, validation, test]:
start_time = datetime.datetime.now()
features = pd.DataFrame(df.apply(lambda x: get_features(x), axis=1 ).to_list())
 df[features.columns] = features
features = pd.DataFrame(df.apply(lambda x: get_features2(x), axis=1 ).to_list())
 df[features.columns] = features
features = pd.DataFrame(df.apply(lambda x: get_features3(x), axis=1 ).to_list())
 df[features.columns] = features
df['same_input_size'] = df['task_id'].apply(lambda x: same_array_size(df, x, 'input'))
df['same_output_size'] = df['task_id'].apply(lambda x: same_array_size(df, x, 'output'))
df['count_input_objects'] = df['input_objects'].apply(lambda x: x if x is None else len(x))
df['count_output_objects'] = df['output_objects'].apply(lambda x: x if x is None else len(x))
end_time = datetime.datetime.now()
print(f'{DF_NAME[id(df)]}: {end_time-start_time}' )<groupby>
c0_ = np.log(np.mean(train_targets_scored.iloc[:, 1:].values, axis=0))
def train_fn(seed=0):
oof = np.zeros(( len(train), len(target_cols)))
 predictions = np.zeros(( len(test), len(target_cols)))
 for fold in range(params["n_folds"]):
seed_everything(seed)
 train_idx = train[train["kfold"] != fold].index
val_idx = train[train["kfold"] == fold].index
train_df = train[train["kfold"] != fold].reset_index(drop=True)
 val_df = train[train["kfold"] == fold].reset_index(drop=True)
 x_train = train_df[feature_cols].values
y_train = train_df[target_cols].values
x_val = val_df[feature_cols].values
y_val = val_df[target_cols].values
train_ds = MoADataset(x_train, y_train)
 val_ds = MoADataset(x_val, y_val)
 train_loader = DataLoader(train_ds, batch_size=params["batch_size"], shuffle=True)
 val_loader = DataLoader(val_ds, batch_size=params["batch_size"], shuffle=False)
 best_score = np.inf
val_score = best_score
best_stage = params["num_nets"] - 1
c0 = torch.tensor(c0_, dtype=torch.float ).to(device)
 net_ensemble = DynamicNet(c0, params["boost_rate"])
 loss_f1 = nn.MSELoss(reduction='none')
 loss_f2 = SmoothBCEwLogits(smoothing=0.001, reduction="none")
 loss_models = torch.zeros(( params["num_nets"], 3))
all_ensm_losses = []
all_ensm_losses_te = []
all_mdl_losses = []
dynamic_br = []
lr = params["lr"]
L2 = params["weight_decay"]
early_stop = 0
for stage in range(params["num_nets"]):
t0 = time.time()
model = MLP_2HL.get_model(stage, params)
 model.to(device)
 optimizer = get_optim(model.parameters() , lr, L2)
 net_ensemble.to_train()
stage_mdlloss = []
for epoch in range(params["epochs_per_stage"]):
for i, data in enumerate(train_loader):
x = data["x"].to(device)
 y = data["y"].to(device)
 middle_feat, out = net_ensemble.forward(x)
 if params["model_order"] == 'first':
grad_direction = y /(1.0 + torch.exp(y * out))
else:
h = 1 /(( 1 + torch.exp(y * out)) *(1 + torch.exp(-y * out)))
 grad_direction = y *(1.0 + torch.exp(-y * out))
nwtn_weights =(torch.exp(out)+ torch.exp(-out)).abs()
_, out = model(x, middle_feat)
 loss = loss_f1(net_ensemble.boost_rate * out, grad_direction)
 loss = loss * h
loss = loss.mean()
model.zero_grad()
loss.backward()
optimizer.step()
stage_mdlloss.append(loss.item())
 net_ensemble.add(model)
 sml = np.mean(stage_mdlloss)
 stage_loss = []
lr_scaler = 2
if stage != 0:
if stage % 3 == 0:
lr /= 2
optimizer = get_optim(net_ensemble.parameters() , lr / lr_scaler, L2)
 for _ in range(params["correct_epoch"]):
for i, data in enumerate(train_loader):
x = data["x"].to(device)
 y = data["y"].to(device)
 _, out = net_ensemble.forward_grad(x)
 loss = loss_f2(out, y ).mean()
optimizer.zero_grad()
loss.backward()
optimizer.step()
stage_loss.append(loss.item())
 sl_te = logloss(net_ensemble, val_loader)
 dynamic_br.append(net_ensemble.boost_rate.item())
 elapsed_tr = time.time() -t0
sl = 0
if stage_loss != []:
sl = np.mean(stage_loss)
 all_ensm_losses.append(sl)
 all_ensm_losses_te.append(sl_te)
 all_mdl_losses.append(sml)
 print(f'Stage - {stage}, training time: {elapsed_tr:.1f} sec, boost rate: {net_ensemble.boost_rate:.4f}, Training Loss: {sl:.5f}, Val Loss: {sl_te:.5f}')
 net_ensemble.to_cuda()
net_ensemble.to_eval()
')
 if sl_te < best_score:
best_score = sl_te
best_stage = stage
net_ensemble.to_file(f"./{fold}FOLD_{seed}_.pth")
 early_stop = 0
else:
early_stop += 1
if early_stop > params["early_stopping_steps"]:
print("early stopped!")
 break
print(f'Best validation stage: {best_stage}')
 net_ensemble = DynamicNet.from_file(f"./{fold}FOLD_{seed}_.pth", lambda stage: MLP_2HL.get_model(stage, params))
net_ensemble.to_cuda()
net_ensemble.to_eval()
preds = []
with torch.no_grad() :
for data in val_loader:
x = data["x"].to(device)
 _, pred = net_ensemble.forward(x)
 preds.append(pred.sigmoid().detach().cpu().numpy())
 oof[val_idx, :] = np.concatenate(preds)
 x_test = test[feature_cols].values
test_ds = TestDataset(x_test)
 test_loader = DataLoader(test_ds, batch_size=params["batch_size"], shuffle=False)
 preds = []
with torch.no_grad() :
for data in test_loader:
x = data["x"].to(device)
 _, pred = net_ensemble.forward(x)
 preds.append(pred.sigmoid().detach().cpu().numpy())
 predictions += np.concatenate(preds)/ params["n_folds"]
oof = np.clip(oof, 1e-3, 1 - 1e-3)
 predictions = np.clip(predictions, 1e-3, 1 - 1e-3)
 train[target_cols] = oof
test[target_cols] = predictions
val_results = train_targets_scored.drop(columns=target_cols ).merge(train[["sig_id"] + target_cols], on="sig_id", how="left" ).fillna(0)
 y_true = train_targets_scored[target_cols].values
y_pred = val_results[target_cols].values
score = 0
for i in range(len(target_cols)) :
score_ = log_loss(y_true[:, i], y_pred[:, i])
 score += score_ / len(target_cols)
 print("CV log_loss ", score)
 sub = sample_submission
sub = sub.drop(columns=target_cols ).merge(test[["sig_id"]+target_cols], on="sig_id", how="left" ).fillna(0)
 return sub
def get_train_average(df, task_id, column):

 return df[(df['task_id'] == task_id)&(df['type'] != 'test')][column].mean()
def check_boolean(df, task_id, column):

 return df[(df['task_id'] == task_id)&(df['type'] != 'test')][column].min()
column_types = train.columns.to_series().groupby(train.dtypes ).groups
for df in [train, validation, test]:
start_time = datetime.datetime.now()
df_tests = df[df['type'] == 'test']
for column in column_types[np.dtype('float64')]:
df.loc[df_tests.index, column] = df_tests['task_id'].apply(lambda x: get_train_average(df, x, column))
for column in column_types[np.dtype('int64')]:
df.loc[df_tests.index, column] = df_tests['task_id'].apply(lambda x: int(get_train_average(df, x, column)) if np.ceil(get_train_average(df, x, column)) == np.floor(get_train_average(df, x, column)) else -1)
 for column in column_types[np.dtype('bool')]:
df.loc[df_tests.index, column] = df_tests['task_id'].apply(lambda x: check_boolean(df, x, column))
end_time = datetime.datetime.now()
print(f'{DF_NAME[id(df)]}: {end_time-start_time}' )<filter>
sub.to_csv("submission.csv", index=False )