Project: s41467-024-51933-2 | Function: heteroSample (python)

system: You are an expert Python programmer implementing algorithms from scientific papers.
Complete the masked function based on the algorithm description.
Follow Python best practices and maintain consistent code style.
Return **only** the complete Python function code.

user: Complete this Python function:

**DESCRIPTION:** Core sampling function that generates optimized antimicrobial peptide sequences using the trained deepAMP model. Implements the sequence degradation approach and peptide pair generation described in the paper for creating training data and generating novel AMPs.

**KEY TERMS:** {
  "masked_seqs": "input peptide sequences",
  "pred_token": "predicted amino acid tokens",
  "pairCabin": "generated peptide pairs",
  "mask_ratio": "masking ratio for sequence degradation"
}

**CONTEXT CODE:** import os, sys
import torch
import numpy as np
import random
import fixed_parameters as FP
from dataPre import train_conf, model_conf, sample_conf
from torch.utils.data.dataloader import DataLoader
from src.DataProcessTools import MaskCollate
from src.utils import checkDirs, stripTAG
from rwHelper import *
from alive_progress import alive_bar

def heteroSample(device,att_model,sample_loader,itos,sample_times,isRandom, ForceRandom,isPair,mask_ratio,save_path, src_data=None):
    """Core sampling function that generates optimized antimicrobial peptide sequences using the trained deepAMP model. Implements the sequence degradation approach and peptide pair generation described in the paper for creating training data and generating novel AMPs."""
    raise NotImplementedError("Function implementation has been masked")

        


if __name__ == "__main__":
    att_model = train_conf.model
    sample_dataset = sample_conf.dataset
    d = 'cpu'
    device = None
    if torch.cuda.is_available():
        d = train_conf.device
        if d == 'cuda:multi':
            device = torch.cuda.current_device()
            att_model = torch.nn.DataParallel(att_model).to(device)
        else:
            device = torch.device(d)
            att_model = att_model.to(device)

    print('The code uses ' + d)
    print(train_conf.train_attr)
    pin_memory = False if d=='cpu' else True
    sample_loader = DataLoader(sample_dataset, 
            shuffle=True, pin_memory=pin_memory,batch_size=sample_conf.batch_size,num_workers=sample_conf.num_workers, drop_last=False,
            collate_fn=MaskCollate(sample_dataset,mask=sample_conf.mask,mask_ratio=sample_conf.mask_ratio,max_pred=sample_conf.max_pred,sample=False))
    itos = sample_conf.dataset.itos

    modeldir = train_conf.basicPath
    dir_list = sorted([i for i in os.listdir(modeldir) if i.startswith('epoch_')], key=lambda s:int(s.split('_')[1]))
    dir_list_star = ['epoch_'+str(i-1) for i in FP.SPECULATED_EPOCH]
    dir_list = list(set(dir_list) & set(dir_list_star))
    dir_list = sorted(dir_list,key = lambda s:int(s.split('_')[1]))
    print(f'isRandom:{sample_conf.isRandom}, isPair:{sample_conf.isPair} ,sample times:{sample_conf.sample_times}, batchsize:{sample_conf.batch_size}, mask_ratio:{sample_conf.mask_ratio}, max_pred:{sample_conf.max_pred}')
    print(f'Epochs:{dir_list}')
    for ep in dir_list:
        print(ep+':')
        # if ep.startswith('epoch_'):
        in_path = f'{modeldir}{ep}/'
        ckpt_path = in_path + 'model.pkl'
        data_path = checkDirs(in_path + 'data/')
        sample_polypeptide_path = checkDirs(data_path + 'Sample_polypeptide/')

        file_name = f'daset={sample_conf.daset},sample_times={sample_conf.sample_times},batchsize={sample_conf.batch_size},mask_ratio={sample_conf.mask_ratio},max_pred={sample_conf.max_pred},isPair={sample_conf.isPair},isRandom={sample_conf.isRandom}.txt'
        save_path = f'{sample_polypeptide_path}{file_name}'

        att_model.load_state_dict(torch.load(ckpt_path, map_location = lambda storage, loc:storage))
        att_model = att_model.to(device)
        
        output= heteroSample(device=device,
                        att_model=att_model,
                        sample_loader=sample_loader,
                        itos=itos,
                        sample_times=sample_conf.sample_times,
                        isRandom=sample_conf.isRandom,
                        ForceRandom=sample_conf.ForceRandom,
                        isPair=sample_conf.isPair,
                        mask_ratio=sample_conf.mask_ratio,
                        save_path=save_path)

        if sample_conf.outside_record:
            sample_conf.alterAttr(sample_model=ckpt_path)
            outsidePath = checkDirs(sample_conf.outsidePath)
            file_name = f'daset={sample_conf.daset},sample_times={sample_conf.sample_times},batchsize={sample_conf.batch_size},mask_ratio={sample_conf.mask_ratio},max_pred={sample_conf.max_pred},isPair={sample_conf.isPair},isRandom={sample_conf.isRandom}.txt'
            save_file = f'{outsidePath}{file_name}'
            lineTxtHelper(save_file).writeLines(output)
            print(f'Outside path:{save_file}')
        print()





Complete the function implementation replacing "raise NotImplementedError".