SciCodePile/SciCode-Domain-Code
DATA1: Domain-Specific Code Dataset Dataset Overview DATA1 is a large-scale domain-specific code dataset focusing on code samples from interdisciplinary fields such as biology, chemistry, materials science, and related areas. The dataset is collected and organized from GitHub repositories, covering 178 different domain topics with over 1.1 billion lines of code. Dataset Statistics Total Datasets: 178 CSV files Total Data Size: ~115 GB Total Lines… See the full description on the dataset page: https://huggingface.co/datasets/SciCodePile/SciCode-Domain-Code.
42.4k
1"keyword","repo_name","file_path","file_extension","file_size","line_count","content","language"
2"ADMET","KwangSun-Ryu/ADMET-AGI-Toxicity-AI-Prototype-and-Baseline--","utils.py",".py","2617","85","# utils.py3import os4import json5import asyncio6import re7from concurrent.futures import ThreadPoolExecutor8from tqdm import tqdm9 10# ==========================11# 파일 저장12# ==========================13def save_jsonl(data_list, out_path):14 os.makedirs(os.path.dirname(out_path), exist_ok=True)15 with open(out_path, ""w"", encoding=""utf-8"") as f:16 for item in data_list:17 f.write(json.dumps(item, ensure_ascii=False) + ""\n"")18 print(f""Saved results to {out_path}"")19 20# ==========================21# EM 점수 계산22# ==========================23def compute_em_score(pred, reference):24 return 1 if pred == reference else 025 26def compute_em_score_mmlu(pred, reference):27 return 1 if pred in reference else 028 29# ==========================30# Summary31# ==========================32def summarize_scores(results):33 total = len(results)34 em_total = sum(r.get(""score"", 0) for r in results)35 return {36 ""n_samples"": total,37 ""em_score"": em_total / total if total > 0 else None38 }39 40# ==========================41# 비동기 모델 호출42# ==========================43async def call_model_async(messages, client, retries=3, initial_delay=1.0):44 delay = initial_delay45 for attempt in range(retries):46 try:47 resp = await client.chat.completions.create(48 model=""25TOXMC_Blowfish_v1.0.9-AWQ"",49 messages=messages,50 temperature=0.0,51 top_p=0.95,52 stream=False53 )54 return resp.choices[0].message.content55 except Exception as e:56 if attempt == retries-1:57 raise58 await asyncio.sleep(delay)59 delay *= 260 61# ==========================62# 공통 비동기 워커63# ==========================64async def run_concurrent_worker(data, build_messages_func, client, concurrency=16):65 sem = asyncio.Semaphore(concurrency)66 results = [None] * len(data)67 68 async def worker(i):69 async with sem:70 messages = build_messages_func(data[i])71 out = await call_model_async(messages, client)72 # <think> 제거 + JSON 파싱73 try:74 out_clean = re.sub(r"".*?</think>"", """", out, flags=re.DOTALL).strip()75 out_json = json.loads(out_clean)76 results[i] = out_json.get(""output"")77 except:78 results[i] = out_clean79 80 tasks = [asyncio.create_task(worker(i)) for i in range(len(data))]81 for f in tqdm(asyncio.as_completed(tasks), total=len(data), desc=""추론 진행중""):82 await f83 84 return results85 86","Python"
87"ADMET","KwangSun-Ryu/ADMET-AGI-Toxicity-AI-Prototype-and-Baseline--","run_vllm.sh",".sh","991","29","#!/usr/bin/env bash88 89# VLLM 컨테이너 실행 예시 (경로/포트/GPU는 환경에 맞게 수정)90docker run -it --rm \91 --gpus all \92 -p 30002:8000 \93 -v ""/mnt/e/Google Drive/External SSD/HealthCare/ADMET/코드/ADMET-AGI/Toxicity AI"":/workspace:rw \94 -e CUDA_VISIBLE_DEVICES=0 \95 -e TP_SIZE=1 \96 -e MODEL_PATH=/workspace/25TOXMC_Blowfish_v1.0.9-AWQ \97 -e CHAT_TEMPLATE_PATH=/workspace/no_tool_chat_template_qwen3.jinja \98 -e GPU_MEMORY_UTILIZATION=0.9 \99 -e DTYPE=bfloat16 \100 vllm-25admet-vllm \101 --host=0.0.0.0 \102 --model=/workspace/25TOXMC_Blowfish_v1.0.9-AWQ \103 --dtype=bfloat16 \104 --chat-template=/workspace/no_tool_chat_template_qwen3.jinja \105 --gpu-memory-utilization=0.9 \106 --tensor-parallel-size=1 \107 --max-model-len=16384108 109# 컨테이너 내부에서 .env 변수 설정 후 평가 스크립트 실행 예시110# export BASE_URL=http://<host>:30002/v1/111# export GPT_API_KEY=<your_gpt_key>112# python3 mobile_eval_e.py113# python3 mmlu_toxic.py114# python3 chem_cot.py115","Shell"
116"ADMET","KwangSun-Ryu/ADMET-AGI-Toxicity-AI-Prototype-and-Baseline--","Generalized ADMET Inference Baseline/chem_cot.py",".py","1519","50","# ChemCoT.py117import asyncio118import json119import datasets120from utils import run_concurrent_worker, save_jsonl, compute_em_score, summarize_scores121import openai122from dotenv import load_dotenv123import os124 125load_dotenv()126 127BASE_URL = os.getenv(""BASE_URL"")128print(BASE_URL)129client = openai.AsyncOpenAI(api_key=""dummy"", base_url=BASE_URL)130 131def build_messages(item):132 system = '''You are a chemical assistant. Given the SMILES structural formula of a molecule, help me add a specified functional group and output the improved SMILES sequence of the molecule. 133Your response must be directly parsable JSON format:134{135 ""output"": ""Modified Molecule SMILES""136}'''137 prompt = item.get(""prompt"") or item.get(""query"", """")138 return [139 {""role"": ""system"", ""content"": system},140 {""role"": ""user"", ""content"": prompt},141 ]142 143def main():144 ds = datasets.load_from_disk('./ChemCoTBench')145 outputs = asyncio.run(run_concurrent_worker(ds, build_messages, client, concurrency=16))146 147 results = []148 for i, item in enumerate(ds):149 pred = outputs[i]150 gold = json.loads(item[""meta""]).get(""reference"")151 em = compute_em_score(pred, gold)152 results.append({153 ""id"": item.get(""id"", i),154 ""prompt"": item.get(""prompt"") or item.get(""query""),155 ""model_output"": pred,156 ""reference"": gold,157 ""score"": em158 })159 160 save_jsonl(results, ""./ChemCoT_results.jsonl"")161 print(""SUMMARY:"", summarize_scores(results))162 163if __name__ == ""__main__"":164 main()165","Python"
166"ADMET","KwangSun-Ryu/ADMET-AGI-Toxicity-AI-Prototype-and-Baseline--","Generalized ADMET Inference Baseline/utils.py",".py","2536","80","# utils.py (Generalized ADMET Inference Baseline)167import os168import json169import asyncio170import re171from concurrent.futures import ThreadPoolExecutor172from tqdm import tqdm173 174# ==========================175# 파일 저장176# ==========================177def save_jsonl(data_list, out_path):178 os.makedirs(os.path.dirname(out_path), exist_ok=True)179 with open(out_path, ""w"", encoding=""utf-8"") as f:180 for item in data_list:181 f.write(json.dumps(item, ensure_ascii=False) + ""\n"")182 print(f""Saved results to {out_path}"")183 184# ==========================185# EM 점수 계산186# ==========================187def compute_em_score(pred, reference):188 return 1 if pred == reference else 0189 190# ==========================191# Summary192# ==========================193def summarize_scores(results):194 total = len(results)195 em_total = sum(r.get(""score"", 0) for r in results)196 return {197 ""n_samples"": total,198 ""em_score"": em_total / total if total > 0 else None199 }200 201# ==========================202# 비동기 모델 호출203# ==========================204async def call_model_async(messages, client, retries=3, initial_delay=1.0):205 delay = initial_delay206 for attempt in range(retries):207 try:208 resp = await client.chat.completions.create(209 model=""25TOXMC_Blowfish_v1.0.9-AWQ"",210 messages=messages,211 temperature=0.0,212 top_p=0.95,213 stream=False214 )215 return resp.choices[0].message.content216 except Exception as e:217 if attempt == retries-1:218 raise219 await asyncio.sleep(delay)220 delay *= 2221 222# ==========================223# 공통 비동기 워커224# ==========================225async def run_concurrent_worker(data, build_messages_func, client, concurrency=16):226 sem = asyncio.Semaphore(concurrency)227 results = [None] * len(data)228 229 async def worker(i):230 async with sem:231 messages = build_messages_func(data[i])232 out = await call_model_async(messages, client)233 try:234 out_clean = re.sub(r"".*?</think>"", """", out, flags=re.DOTALL).strip()235 out_json = json.loads(out_clean)236 results[i] = out_json.get(""output"")237 except Exception:238 results[i] = out_clean239 240 tasks = [asyncio.create_task(worker(i)) for i in range(len(data))]241 for f in tqdm(asyncio.as_completed(tasks), total=len(data), desc=""추론 진행중""):242 await f243 244 return results245","Python"
246"ADMET","KwangSun-Ryu/ADMET-AGI-Toxicity-AI-Prototype-and-Baseline--","Toxicity AI Prototype/mobile_eval_e.py",".py","6442","213","# Mobile-Eval-E.py247import json248from datasets import load_dataset249from tqdm import tqdm250from openai import OpenAI251from utils import save_jsonl252import os253from dotenv import load_dotenv254load_dotenv()255# -------------------------256# GPT 클라이언트257# -------------------------258 259API_KEY = os.getenv(""GPT_API_KEY"")260 261client = OpenAI(api_key=API_KEY)262 263# -------------------------264# 시스템 프롬프트265# -------------------------266SYSTEM_PROMPT = """"""You are a mobile task planner that controls an Android phone via high-level actions.267Given a user instruction and a list of available apps, your goal is to output a step-by-step action sequence268to complete the task on the phone.269 270You MUST output a single JSON object with the following structure:271 272{273 ""plan"": [""high-level step 1"", ""high-level step 2"", ...],274 ""operations"": [275 ""action 1"",276 ""action 2"",277 ...278 ]279}280 281Each action should be a short imperative phrase describing a concrete phone operation282(e.g., ""open Maps"", ""tap on the search bar"", ""type 'korean restaurant'"", ""press enter"").283Do not include any explanations or extra text outside the JSON object.284""""""285 286JUDGE_SYSTEM_PROMPT = """"""287You are an expert evaluator for a mobile phone agent benchmark.288 289Your job is to evaluate how well a model-generated action sequence (operations)290solves a given mobile task, based on:291 2921) The natural language instruction.2932) The list of available apps and scenario.2943) A list of rubrics describing what a good solution should do.2954) A human reference action sequence (operations).2965) The model-generated action sequence.297 298You must output a single JSON object with the following fields:299 300{301 ""rubric_score"": float, // between 0.0 and 1.0302 ""action_match_score"": float, // between 0.0 and 1.0303 ""overall_score"": float, // between 0.0 and 1.0304 ""reason"": ""short explanation""305}306 307- rubric_score: how well the model operations satisfy the rubrics.308- action_match_score: how similar the model operations are to the human reference operations.309- overall_score: your overall judgement, not necessarily the average.310Return only the JSON object, with no additional text.311""""""312 313# -------------------------314# JSON 파싱315# -------------------------316def extract_json(text: str):317 start = text.find(""{"")318 end = text.rfind(""}"")319 if start == -1 or end == -1 or end <= start:320 raise ValueError(f""JSON block not found in model output: {text[:200]}..."")321 json_str = text[start:end+1]322 return json.loads(json_str)323 324# -------------------------325# Judge Prompt 빌드326# -------------------------327def build_judge_prompt(example, model_ops):328 instruction = example[""instruction""]329 apps = example.get(""apps"", [])330 scenario = example.get(""scenario"", """")331 rubrics = example.get(""rubrics"", [])332 human_ops = example.get(""human_reference_operations"", [])333 return f""""""334[Instruction]335{instruction}336 337[Apps]338{apps}339 340[Scenario]341{scenario}342 343[Rubrics]344{json.dumps(rubrics, ensure_ascii=False, indent=2)}345 346[Human Reference Operations]347{json.dumps(human_ops, ensure_ascii=False, indent=2)}348 349[Model Operations to Evaluate]350{json.dumps(model_ops, ensure_ascii=False, indent=2)}351""""""352 353# -------------------------354# GPT Judge 호출355# -------------------------356def judge_with_gpt(example, model_ops):357 user_prompt = build_judge_prompt(example, model_ops)358 messages = [359 {""role"": ""system"", ""content"": JUDGE_SYSTEM_PROMPT},360 {""role"": ""user"", ""content"": user_prompt},361 ]362 resp = client.chat.completions.create(363 model=""gpt-5-mini"",364 messages=messages,365 )366 text = resp.choices[0].message.content367 data = extract_json(text)368 return {369 ""rubric_score"": float(data.get(""rubric_score"", 0.0)),370 ""action_match_score"": float(data.get(""action_match_score"", 0.0)),371 ""overall_score"": float(data.get(""overall_score"", 0.0)),372 ""reason"": data.get(""reason"", """"),373 }374 375# -------------------------376# Actor 호출377# -------------------------378def process_request_vl(messages):379 import openai380 openai.api_key = ""sk-None-1234""381 openai.base_url = ""http://192.168.0.202:25321/v1/""382 output = openai.chat.completions.create(383 model='25TOXMC_Blowfish_v1.0.9-AWQ',384 messages=messages,385 temperature=0.0,386 top_p=0.95,387 stream=False388 )389 return output390 391def build_actor_prompt(example):392 instruction = example[""instruction""]393 apps = example.get(""apps"", [])394 scenario = example.get(""scenario"", """")395 apps_str = "", "".join(apps) if apps else ""no specific apps""396 return f""""""User instruction:397{instruction}398 399You may use the following apps: {apps_str}400Scenario: {scenario}401 402Return ONLY a JSON object with the fields ""plan"" and ""operations"".403""""""404 405def call_actor(example):406 messages = [407 {""role"": ""system"", ""content"": SYSTEM_PROMPT},408 {""role"": ""user"", ""content"": build_actor_prompt(example)},409 ]410 resp = process_request_vl(messages)411 data = extract_json(resp.choices[0].message.content)412 ops = [str(o).strip() for o in data.get(""operations"", []) if str(o).strip()]413 return ops414 415# -------------------------416# 메인 루프417# -------------------------418def main():419 ds = load_dataset(""mikewang/mobile_eval_e"", split=""test"")420 scores = []421 422 for ex in tqdm(ds, desc=""Evaluating with GPT judge""):423 try:424 model_ops = call_actor(ex)425 except Exception as e:426 print(""Actor model failed:"", e)427 model_ops = []428 429 try:430 judge_result = judge_with_gpt(ex, model_ops)431 except Exception as e:432 print(""Judge model failed:"", e)433 judge_result = {434 ""rubric_score"": 0.0,435 ""action_match_score"": 0.0,436 ""overall_score"": 0.0,437 ""reason"": f""Judge error: {e}"",438 }439 440 scores.append(judge_result)441 442 avg_rubric = sum(s[""rubric_score""] for s in scores) / len(scores)443 avg_action = sum(s[""action_match_score""] for s in scores) / len(scores)444 avg_overall = sum(s[""overall_score""] for s in scores) / len(scores)445 446 print(""\n===== GPT Judge Overall Results ====="")447 print(f""#examples : {len(scores)}"")448 print(f""Avg rubric_score : {avg_rubric:.4f}"")449 print(f""Avg action_match : {avg_action:.4f}"")450 print(f""Avg overall_score : {avg_overall:.4f}"")451 452 # JSONL 저장 (공통 구조)453 save_jsonl(scores, ""./MobileEvalE_results.jsonl"")454 455if __name__ == ""__main__"":456 main()457 458","Python"
459"ADMET","KwangSun-Ryu/ADMET-AGI-Toxicity-AI-Prototype-and-Baseline--","Toxicity AI Prototype/mmlu_toxic.py",".py","1223","47","# MMLU_toxic.py460import json461import asyncio462from utils import run_concurrent_worker, save_jsonl, compute_em_score_mmlu, summarize_scores463import openai464import os465from dotenv import load_dotenv466 467load_dotenv()468 469BASE_URL = os.getenv(""BASE_URL"")470 471client = openai.AsyncOpenAI(api_key=""dummy"", base_url=BASE_URL)472 473def build_messages(item):474 system = item.get(""system"", """")475 prompt = item.get(""prompt"", """")476 return [477 {""role"": ""system"", ""content"": system},478 {""role"": ""user"", ""content"": prompt},479 ]480 481def main():482 with open(""./mmlu_toxic.json"", ""r"", encoding=""utf-8"") as f:483 data = json.load(f)484 485 outputs = asyncio.run(run_concurrent_worker(data, build_messages, client, concurrency=16))486 487 results = []488 for i, item in enumerate(data):489 pred = outputs[i]490 em = compute_em_score_mmlu(pred, item.get(""answer"", []))491 results.append({492 ""id"": item.get(""id"", i),493 ""prompt"": item.get(""prompt""),494 ""model_output"": pred,495 ""reference"": item.get(""answer""),496 ""score"": em497 })498 499 save_jsonl(results, ""./MMLU_toxic_results.jsonl"")500 print(""SUMMARY:"", summarize_scores(results))501 502if __name__ == ""__main__"":503 main()504 505","Python"
506"ADMET","KwangSun-Ryu/ADMET-AGI-Toxicity-AI-Prototype-and-Baseline--","Toxicity AI Prototype/utils.py",".py","2526","80","# utils.py (Toxicity AI Prototype)507import os508import json509import asyncio510import re511from concurrent.futures import ThreadPoolExecutor512from tqdm import tqdm513 514# ==========================515# 파일 저장516# ==========================517def save_jsonl(data_list, out_path):518 os.makedirs(os.path.dirname(out_path), exist_ok=True)519 with open(out_path, ""w"", encoding=""utf-8"") as f:520 for item in data_list:521 f.write(json.dumps(item, ensure_ascii=False) + ""\n"")522 print(f""Saved results to {out_path}"")523 524# ==========================525# EM 점수 계산526# ==========================527def compute_em_score_mmlu(pred, reference):528 return 1 if pred in reference else 0529 530# ==========================531# Summary532# ==========================533def summarize_scores(results):534 total = len(results)535 em_total = sum(r.get(""score"", 0) for r in results)536 return {537 ""n_samples"": total,538 ""em_score"": em_total / total if total > 0 else None539 }540 541# ==========================542# 비동기 모델 호출543# ==========================544async def call_model_async(messages, client, retries=3, initial_delay=1.0):545 delay = initial_delay546 for attempt in range(retries):547 try:548 resp = await client.chat.completions.create(549 model=""25TOXMC_Blowfish_v1.0.9-AWQ"",550 messages=messages,551 temperature=0.0,552 top_p=0.95,553 stream=False554 )555 return resp.choices[0].message.content556 except Exception as e:557 if attempt == retries-1:558 raise559 await asyncio.sleep(delay)560 delay *= 2561 562# ==========================563# 공통 비동기 워커564# ==========================565async def run_concurrent_worker(data, build_messages_func, client, concurrency=16):566 sem = asyncio.Semaphore(concurrency)567 results = [None] * len(data)568 569 async def worker(i):570 async with sem:571 messages = build_messages_func(data[i])572 out = await call_model_async(messages, client)573 try:574 out_clean = re.sub(r"".*?</think>"", """", out, flags=re.DOTALL).strip()575 out_json = json.loads(out_clean)576 results[i] = out_json.get(""output"")577 except Exception:578 results[i] = out_clean579 580 tasks = [asyncio.create_task(worker(i)) for i in range(len(data))]581 for f in tqdm(asyncio.as_completed(tasks), total=len(data), desc=""추론 진행중""):582 await f583 584 return results585","Python"
586"ADMET","rnzhiw/HuaweiCupMathModel","train.py",".py","6269","184","import numpy as np587import torch588import torch.nn as nn589from dataloader import DataLoader590from model import Model591from utils import AverageMeter, accuracy, F1_Score592from dice_loss import DiceLoss593import argparse594import time595import warnings596import mmcv597warnings.filterwarnings(""ignore"")598try:599 import wandb600except:601 pass602 603def adjust_learning_rate(optimizer, dataloader, epoch, iter):604 cur_iter = epoch * len(dataloader) + iter605 max_iter_num = args.epoch * len(dataloader)606 lr = args.lr * (1 - float(cur_iter) / max_iter_num) ** 0.9607 for param_group in optimizer.param_groups:608 param_group['lr'] = lr609 610 611def model_structure(model):612 blank = ' '613 print('-' * 90)614 print('|' + ' ' * 11 + 'weight name' + ' ' * 10 + '|' \615 + ' ' * 15 + 'weight shape' + ' ' * 15 + '|' \616 + ' ' * 3 + 'number' + ' ' * 3 + '|')617 print('-' * 90)618 num_para = 0619 type_size = 1 ##如果是浮点数就是4620 621 for index, (key, w_variable) in enumerate(model.named_parameters()):622 if len(key) <= 30:623 key = key + (30 - len(key)) * blank624 shape = str(w_variable.shape)625 if len(shape) <= 40:626 shape = shape + (40 - len(shape)) * blank627 each_para = 1628 for k in w_variable.shape:629 each_para *= k630 num_para += each_para631 str_num = str(each_para)632 if len(str_num) <= 10:633 str_num = str_num + (10 - len(str_num)) * blank634 635 print('| {} | {} | {} |'.format(key, shape, str_num))636 print('-' * 90)637 print('The total number of parameters: ' + str(num_para))638 print('The parameters of Model {}: {:4f}M'.format(model._get_name(), num_para * type_size / 1000 / 1000))639 print('-' * 90)640 641def valid(valid_loader, model, epoch):642 model.eval()643 644 for iter, (x, y) in enumerate(valid_loader):645 x = x.cuda()646 y = y.cuda()647 with torch.no_grad():648 outputs = model(x)649 loss = criterion(outputs, y)650 acc = ((outputs > 0) == y).sum(dim=0).float() / args.valid_batch_size651 652 mean_acc = acc.mean()653 output_log = '(Valid) Loss: {loss:.3f} | Mean Acc: {acc:.3f}'.format(654 loss=loss.item(),655 acc=mean_acc.item()656 )657 print(output_log)658 print(acc)659 if args.wandb:660 wandb.log({'epoch': epoch,661 'Caco-2': acc[0].item(),662 'CYP3A4': acc[1].item(),663 'hERG': acc[2].item(),664 'HOB': acc[3].item(),665 'MN': acc[4].item(),666 'Mean': mean_acc.item()})667 return mean_acc668 669def train(train_loader, model, optimizer, epoch):670 model.train()671 672 # meters673 batch_time = AverageMeter()674 data_time = AverageMeter()675 676 # start time677 start = time.time()678 for iter, (x, y) in enumerate(train_loader):679 x = x.cuda()680 y = y.cuda()681 # time cost of data loader682 data_time.update(time.time() - start)683 684 # adjust learning rate685 adjust_learning_rate(optimizer, train_loader, epoch, iter)686 687 outputs = model(x)688 loss = criterion(outputs, y)689 with torch.no_grad():690 acc = ((outputs > 0) == y).sum(dim=0).float() / args.batch_size691 # backward692 optimizer.zero_grad()693 loss.backward()694 optimizer.step()695 696 batch_time.update(time.time() - start)697 698 # update start time699 start = time.time()700 701 # print log702 if iter % 10 == 0:703 output_log = '({batch}/{size}) LR: {lr:.6f} | Batch: {bt:.3f}s | Total: {total:.0f}min | ' \704 'ETA: {eta:.0f}min | Loss: {loss:.3f} | ' \705 'Mean Acc: {acc:.3f}'.format(706 batch=iter + 1,707 size=len(train_loader),708 lr=optimizer.param_groups[0]['lr'],709 bt=batch_time.avg,710 total=batch_time.avg * iter / 60.0,711 eta=batch_time.avg * (len(train_loader) - iter) / 60.0,712 loss=loss.item(),713 acc=acc.mean().item()714 )715 716 print(output_log)717 print(acc)718 # if args.wandb:719 # wandb.log({'epoch': epoch,720 # 'Caco-2': acc[0].item(),721 # 'CYP3A4': acc[1].item(),722 # 'hERG': acc[2].item(),723 # 'HOB': acc[3].item(),724 # 'MN':acc[4].item()})725 726def main():727 train_loader = torch.utils.data.DataLoader(728 DataLoader(split=""train""), batch_size=args.batch_size,729 shuffle=True, num_workers=0, drop_last=True, pin_memory=True730 )731 valid_loader = torch.utils.data.DataLoader(732 DataLoader(split=""valid""), batch_size=args.valid_batch_size,733 shuffle=False, num_workers=0, drop_last=False, pin_memory=True734 )735 model = Model().cuda()736 model_structure(model)737 if args.wandb:738 wandb.watch(model)739 # optimizer = torch.optim.SGD(model.parameters(), lr=args.lr, momentum=0.9, weight_decay=1e-4)740 optimizer = torch.optim.Adam(model.parameters(), lr=args.lr)741 742 start_epoch, start_iter, best_mean_acc = 0, 0, 0743 for epoch in range(start_epoch, args.epoch):744 print('\nEpoch: [%d | %d]' % (epoch + 1, args.epoch))745 train(train_loader, model, optimizer, epoch)746 mean_acc = valid(valid_loader, model, epoch)747 if mean_acc >= best_mean_acc:748 best_mean_acc = mean_acc749 750 torch.save(model.state_dict(), ""checkpoint/checkpoint.pth"")751 752if __name__ == '__main__':753 parser = argparse.ArgumentParser(description='Hyperparams')754 parser.add_argument('--epoch', default=1000, type=int, help='epoch')755 parser.add_argument('--batch_size', default=1776, type=int, help='batch size')756 parser.add_argument('--valid_batch_size', default=198, type=int, help='batch size')757 parser.add_argument('--lr', default=0.01, type=float, help='batch size')758 parser.add_argument('--wandb', action='store_true', help='use wandb')759 760 mmcv.mkdir_or_exist(""checkpoint/"")761 args = parser.parse_args()762 print(args)763 # torch.backends.cudnn.benchmark = True764 765 if args.wandb:766 wandb.init(project=""math-model"")767 768 criterion = DiceLoss(loss_weight=1.0)769 main()","Python"
770"ADMET","rnzhiw/HuaweiCupMathModel","dice_loss.py",".py","752","29","import torch771import torch.nn as nn772 773 774class DiceLoss(nn.Module):775 def __init__(self, loss_weight=1.0):776 super(DiceLoss, self).__init__()777 self.loss_weight = loss_weight778 779 def forward(self, input, target, reduce=True):780 batch_size = input.size(0)781 input = torch.sigmoid(input)782 783 input = input.contiguous().view(batch_size, -1)784 target = target.contiguous().view(batch_size, -1).float()785 786 a = torch.sum(input * target, dim=1)787 b = torch.sum(input * input, dim=1) + 0.001788 c = torch.sum(target * target, dim=1) + 0.001789 d = (2 * a) / (b + c)790 loss = 1 - d791 792 loss = self.loss_weight * loss793 794 if reduce:795 loss = torch.mean(loss)796 797 return loss798","Python"
799"ADMET","rnzhiw/HuaweiCupMathModel","draw.py",".py","587","22","import seaborn as sns800import numpy as np801import matplotlib.pyplot as plt802 803def plot(matrix):804 sns.set()805 f,ax=plt.subplots()806 print(matrix) #打印出来看看807 sns.heatmap(matrix, annot=True,808 xticklabels=['Small', 'Fit', 'Large'],809 yticklabels=['Small', 'Fit', 'Large'],810 cmap=""Blues"", ax=ax, fmt='.20g') #画热力图811 ax.set_title('Confusion Matrix') #标题812 ax.set_xlabel('Predict') #x轴813 ax.set_ylabel('True') #y轴814 815matrix=np.array([[1116, 587, 105],816 [827, 10134, 855],817 [66, 355, 955]])818plot(matrix)# 画原始的数据819plt.show()820","Python"
821"ADMET","rnzhiw/HuaweiCupMathModel","model.py",".py","2444","79","import torch822import torch.nn as nn823import math824from timm.models.layers import DropPath, to_2tuple, trunc_normal_825 826 827class Model(nn.Module):828 def __init__(self):829 super(Model, self).__init__()830 self.model = nn.Sequential(831 nn.LayerNorm([729]),832 nn.Dropout(0.1),833 nn.Linear(729, 512),834 nn.ReLU(inplace=True),835 836 nn.LayerNorm([512]),837 nn.Dropout(0.1),838 nn.Linear(512, 128),839 nn.ReLU(inplace=True),840 841 nn.LayerNorm([128]),842 nn.Dropout(0.1),843 nn.Linear(128, 5)844 )845 846 847 def forward(self, x):848 y = self.model(x)849 return y850 851 852 853class Attention(nn.Module):854 def __init__(self, dim, ratio=4, qkv_bias=False, qk_scale=None, attn_drop=0., proj_drop=0., sr_ratio=1, linear=False):855 super().__init__()856 857 self.scale = qk_scale or 1 ** -0.5858 self.ratio = ratio859 self.q = nn.Linear(dim, dim//ratio, bias=qkv_bias)860 self.kv = nn.Linear(dim, dim*2//ratio, bias=qkv_bias)861 self.attn_drop = nn.Dropout(attn_drop)862 self.proj = nn.Linear(dim//ratio, dim)863 self.proj_drop = nn.Dropout(proj_drop)864 865 self.apply(self._init_weights)866 867 def _init_weights(self, m):868 if isinstance(m, nn.Linear):869 trunc_normal_(m.weight, std=.02)870 if isinstance(m, nn.Linear) and m.bias is not None:871 nn.init.constant_(m.bias, 0)872 elif isinstance(m, nn.LayerNorm):873 nn.init.constant_(m.bias, 0)874 nn.init.constant_(m.weight, 1.0)875 elif isinstance(m, nn.Conv2d):876 fan_out = m.kernel_size[0] * m.kernel_size[1] * m.out_channels877 fan_out //= m.groups878 m.weight.data.normal_(0, math.sqrt(2.0 / fan_out))879 if m.bias is not None:880 m.bias.data.zero_()881 882 def forward(self, x):883 B, C = x.shape884 q = self.q(x).unsqueeze(-1) # [1776, 729]885 kv = self.kv(x).reshape(B, C//self.ratio, -1, 1).permute(2, 0, 1, 3) # [1776, 1458]886 k, v = kv[0], kv[1] # [1776, 729, 1]887 # print(q.shape, k.shape, v.shape)888 889 attn = (q @ k.transpose(-2, -1)) * self.scale890 attn = attn.softmax(dim=-1)891 attn = self.attn_drop(attn)892 893 o = (attn @ v).squeeze(-1)894 # print(x.shape)895 o = self.proj(o)896 o = self.proj_drop(o)897 898 return o + x899","Python"
900"ADMET","rnzhiw/HuaweiCupMathModel","valid.py",".py","1145","43","import torch901from dataloader import DataLoader902from model import Model903from dice_loss import DiceLoss904import warnings905 906warnings.filterwarnings(""ignore"")907 908 909def valid(valid_loader, model):910 model.eval()911 criterion = DiceLoss()912 for iter, (x, y) in enumerate(valid_loader):913 x = x.cuda()914 y = y.cuda()915 with torch.no_grad():916 outputs = model(x)917 loss = criterion(outputs, y)918 acc = ((outputs > 0) == y).sum(dim=0).float() / VALID_BATCH_SIZE919 920 mean_acc = acc.mean()921 output_log = '(Valid) Loss: {loss:.3f} | Mean Acc: {acc:.3f}'.format(922 loss=loss.item(),923 acc=mean_acc.item()924 )925 print(output_log)926 print(acc)927 return mean_acc928 929def main():930 valid_loader = torch.utils.data.DataLoader(931 DataLoader(split=""valid""), batch_size=VALID_BATCH_SIZE,932 shuffle=False, num_workers=0, drop_last=False, pin_memory=True933 )934 model = Model().cuda()935 state_dict = torch.load(""checkpoint/checkpoint.pth"")936 model.load_state_dict(state_dict)937 valid(valid_loader, model)938 939 940if __name__ == '__main__':941 VALID_BATCH_SIZE = 198942 main()","Python"
943"ADMET","rnzhiw/HuaweiCupMathModel","test.py",".py","1215","43","import torch944from dataloader import DataLoader945from model import Model946import pandas as pd947import warnings948warnings.filterwarnings(""ignore"")949 950 951def test(valid_loader, model):952 model.eval()953 954 smiles = pd.read_csv('data/Molecular_Descriptor.csv')['SMILES'].tolist()955 y_preds = []956 for iter, x in enumerate(valid_loader):957 x = x.cuda()958 with torch.no_grad():959 outputs = model(x)960 y_pred = (outputs > 0).int().cpu().numpy().tolist()961 y_preds.append(y_pred)962 y_preds = y_preds[0]963 print(len(y_preds))964 f = open(""data/ADEMT_test_pre.csv"", ""w+"")965 f.write(""SMILES,Caco-2,CYP3A4,hERG,HOB,MN\n"")966 for index, y_pred in enumerate(y_preds):967 text = smiles[index] + "","" + "","".join([str(i) for i in y_pred])968 f.write(text + ""\n"")969 print(text)970 f.close()971 972 973def main():974 valid_loader = torch.utils.data.DataLoader(975 DataLoader(split=""test""), batch_size=50,976 shuffle=False, num_workers=0, drop_last=False, pin_memory=True977 )978 model = Model().cuda()979 state_dict = torch.load(""checkpoint/checkpoint.pth"")980 model.load_state_dict(state_dict)981 test(valid_loader, model)982 983 984if __name__ == '__main__':985 main()","Python"
986"ADMET","rnzhiw/HuaweiCupMathModel","utils.py",".py","2558","77","import torch987import numpy as np988 989 990class AverageMeter(object):991 """"""Computes and stores the average and current value""""""992 def __init__(self, max_len=-1):993 self.val = []994 self.count = []995 self.max_len = max_len996 self.avg = 0997 998 def update(self, val, n=1):999 self.val.append(val * n)1000 self.count.append(n)1001 if self.max_len > 0 and len(self.val) > self.max_len:1002 self.val = self.val[-self.max_len:]1003 self.count = self.count[-self.max_len:]1004 self.avg = sum(self.val) / sum(self.count)1005 1006 1007def accuracy(output, target, topk=(1,)):1008 """"""Computes the accuracy over the k top predictions for the specified values of k""""""1009 with torch.no_grad():1010 maxk = max(topk)1011 batch_size = target.size(0)1012 1013 _, pred = output.topk(maxk, 1, True, True)1014 pred = pred.t()1015 correct = pred.eq(target.view(1, -1).expand_as(pred))1016 1017 res = []1018 for k in topk:1019 correct_k = correct[:k].reshape(-1).float().sum(0, keepdim=True)1020 res.append(correct_k.mul_(100.0 / batch_size))1021 return res1022 1023 1024class F1_Score:1025 __name__ = 'F1 macro'1026 def __init__(self,n=3):1027 self.n = n1028 self.TP = np.zeros(self.n)1029 self.FP = np.zeros(self.n)1030 self.FN = np.zeros(self.n)1031 1032 def get_confusion_matrix(self, prediction, ground_truth, num_classes):1033 gt_onehot = torch.nn.functional.one_hot(ground_truth, num_classes=num_classes).float()1034 prediction = torch.argmax(prediction, dim=1)1035 pd_onehot = torch.nn.functional.one_hot(prediction, num_classes=num_classes).float()1036 return pd_onehot.t().matmul(gt_onehot)1037 1038 def __call__(self, preds, targs):1039 cm = self.get_confusion_matrix(preds, targs, num_classes=3)1040 print(cm)1041 TP = cm.diagonal()1042 FP = cm.sum(1) - TP1043 FN = cm.sum(0) - TP1044 self.TP += TP.float().cpu().numpy()1045 self.FP += FP.float().cpu().numpy()1046 self.FN += FN.float().cpu().numpy()1047 1048 def print(self):1049 precision = self.TP / (self.TP + self.FP + 1e-12)1050 recall = self.TP / (self.TP + self.FN + 1e-12)1051 self.precision = precision1052 self.recall = recall1053 # precision = precision.mean()1054 # recall = recall.mean()1055 score = 2.0 * (precision * recall) / (precision + recall + 1e-12)1056 score = score.mean()1057 return score1058 1059 def reset(self):1060 self.TP = np.zeros(self.n)1061 self.FP = np.zeros(self.n)1062 self.FN = np.zeros(self.n)","Python"
1063"ADMET","rnzhiw/HuaweiCupMathModel","dataloader.py",".py","2384","73","import pandas as pd1064import numpy as np1065from torch.utils import data1066import torch.nn as nn1067import torch1068import os1069 1070pd.set_option('display.max_columns', None)1071 1072 1073class DataLoader(data.Dataset):1074 def __init__(self, split):1075 self.split = split1076 if split == 'train' or split == 'valid':1077 feature = pd.read_csv('data/Molecular_Descriptor.csv', index_col='SMILES').values.tolist()1078 label = pd.read_csv('data/ADMET.csv', index_col='SMILES').values.tolist()1079 1080 feature_train, label_train = [], []1081 feature_valid, label_valid = [], []1082 1083 for i in range(0, 1974):1084 if i % 10 != 0:1085 feature_train.append(feature[i])1086 label_train.append(label[i])1087 else:1088 feature_valid.append(feature[i])1089 label_valid.append(label[i])1090 1091 if split == 'train':1092 self.feature = np.array(feature_train)1093 self.label = np.array(label_train)1094 elif split == 'valid':1095 self.feature = np.array(feature_valid)1096 self.label = np.array(label_valid)1097 else:1098 print(""split must in [train, valid]"")1099 1100 elif split == 'test':1101 feature = pd.read_csv(""data/Molecular_Descriptor_test.csv"", index_col='SMILES').values.tolist()1102 self.feature = np.array(feature)1103 else:1104 print('Error: split must be train, valid or test!')1105 1106 1107 def __len__(self):1108 return len(self.feature)1109 1110 def __getitem__(self, index):1111 if self.split == 'train' or self.split == 'valid':1112 x = torch.from_numpy(self.feature[index]).float()1113 y = torch.from_numpy(np.array(self.label[index])).float()1114 return x, y1115 1116 elif self.split == 'test':1117 x = torch.from_numpy(self.feature[index]).float()1118 return x1119 1120 1121if __name__ == '__main__':1122 dataloader = DataLoader(split='train')1123 print(len(dataloader))1124 x, y = dataloader.__getitem__(0)1125 print(x.shape, y.shape)1126 1127 dataloader = DataLoader(split='valid')1128 print(len(dataloader))1129 x, y = dataloader.__getitem__(0)1130 print(x.shape, y.shape)1131 1132 dataloader = DataLoader(split='test')1133 x = dataloader.__getitem__(0)1134 print(x.shape)1135","Python"
1136"ADMET","rnzhiw/HuaweiCupMathModel","data/question1_2.ipynb",".ipynb","345116","10473","{1137 ""cells"": [1138 {1139 ""cell_type"": ""code"",1140 ""execution_count"": 169,1141 ""id"": ""ccdac8db"",1142 ""metadata"": {},1143 ""outputs"": [],1144 ""source"": [1145 ""import matplotlib.pyplot as plt\n"",1146 ""import numpy as np\n"",1147 ""import pandas as pd\n"",1148 ""import sklearn\n"",1149 ""import seaborn as sns""1150 ]1151 },1152 {1153 ""cell_type"": ""code"",1154 ""execution_count"": 20,1155 ""id"": ""89d498f6"",1156 ""metadata"": {},1157 ""outputs"": [],1158 ""source"": [1159 ""?pd.read_csv""1160 ]1161 },1162 {1163 ""cell_type"": ""code"",1164 ""execution_count"": 95,1165 ""id"": ""aaa5a4b3"",1166 ""metadata"": {},1167 ""outputs"": [],1168 ""source"": [1169 ""feature_train=pd.read_csv('Molecular_Descriptor.csv',index_col='SMILES')\n"",1170 ""label_train=pd.read_csv('ERα_activity.csv',index_col='SMILES')\n""1171 ]1172 },1173 {1174 ""cell_type"": ""code"",1175 ""execution_count"": 96,1176 ""id"": ""39b96bbf"",1177 ""metadata"": {},1178 ""outputs"": [],1179 ""source"": [1180 ""del label_train['IC50_nM']""1181 ]1182 },1183 {1184 ""cell_type"": ""code"",1185 ""execution_count"": 23,1186 ""id"": ""239b91fc"",1187 ""metadata"": {},1188 ""outputs"": [1189 {1190 ""name"": ""stdout"",1191 ""output_type"": ""stream"",1192 ""text"": [1193 ""<class 'pandas.core.frame.DataFrame'>\n"",1194 ""Index: 1974 entries, Oc1ccc2O[C@H]([C@H](Sc2c1)C3CCCC3)c4ccc(OCCN5CCCCC5)cc4 to COc1cc(OC)cc(\\C=C\\c2ccc(OS(=O)(=O)[C@H]3C[C@H]4O[C@@H]3C(=C4c5ccc(O)cc5)c6ccc(O)cc6)cc2)c1\n"",1195 ""Columns: 729 entries, nAcid to Zagreb\n"",1196 ""dtypes: float64(359), int64(370)\n"",1197 ""memory usage: 11.0+ MB\n""1198 ]1199 }1200 ],