Initial commit
Browse files- data_generation_tutorial.ipynb +151 -0
data_generation_tutorial.ipynb
ADDED
@@ -0,0 +1,151 @@
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
1 |
+
{
|
2 |
+
"cells": [
|
3 |
+
{
|
4 |
+
"cell_type": "code",
|
5 |
+
"execution_count": 3,
|
6 |
+
"metadata": {},
|
7 |
+
"outputs": [],
|
8 |
+
"source": [
|
9 |
+
"import random\n",
|
10 |
+
"import numpy as np\n",
|
11 |
+
"import pandas as pd\n",
|
12 |
+
"\n",
|
13 |
+
"def generate_random_dna_sequence(length):\n",
|
14 |
+
" return ''.join(random.choices('ATGC', k=length))\n",
|
15 |
+
"\n",
|
16 |
+
"np.random.seed(42)\n",
|
17 |
+
"\n",
|
18 |
+
"# Generate 200 sequences of random DNA sequences with lengths ranging from 200 to 2000\n",
|
19 |
+
"sequence_lengths = np.random.randint(200, 2001, size=2000)\n",
|
20 |
+
"dna_sequences = [generate_random_dna_sequence(length) for length in sequence_lengths]\n",
|
21 |
+
"labels1 = np.random.randint(0, 2, size=2000)\n",
|
22 |
+
"labels2 = np.random.randint(0, 3, size=2000)\n",
|
23 |
+
"labels3 = np.random.randint(0, 5, size=2000)\n",
|
24 |
+
"\n",
|
25 |
+
"# Create a DataFrame with the DNA sequences and random labels\n",
|
26 |
+
"df_dna = pd.DataFrame({\n",
|
27 |
+
" 'sequence': dna_sequences,\n",
|
28 |
+
" 'label1': labels1,\n",
|
29 |
+
" 'label2': labels2,\n",
|
30 |
+
" 'label3': labels3\n",
|
31 |
+
"})\n",
|
32 |
+
"\n",
|
33 |
+
"# Save to CSV\n",
|
34 |
+
"csv_dna_path = \"/data/project/hf_tutorial/data/train.csv\"\n",
|
35 |
+
"df_dna.to_csv(csv_dna_path, index=False)\n",
|
36 |
+
"\n",
|
37 |
+
"\n"
|
38 |
+
]
|
39 |
+
},
|
40 |
+
{
|
41 |
+
"cell_type": "code",
|
42 |
+
"execution_count": 4,
|
43 |
+
"metadata": {},
|
44 |
+
"outputs": [],
|
45 |
+
"source": [
|
46 |
+
"\n",
|
47 |
+
"# Generate 200 sequences of random DNA sequences with lengths ranging from 200 to 2000\n",
|
48 |
+
"sequence_lengths = np.random.randint(200, 2001, size=200)\n",
|
49 |
+
"dna_sequences = [generate_random_dna_sequence(length) for length in sequence_lengths]\n",
|
50 |
+
"labels1 = np.random.randint(0, 2, size=200)\n",
|
51 |
+
"labels2 = np.random.randint(0, 3, size=200)\n",
|
52 |
+
"labels3 = np.random.randint(0, 5, size=200)\n",
|
53 |
+
"\n",
|
54 |
+
"# Create a DataFrame with the DNA sequences and random labels\n",
|
55 |
+
"df_dna = pd.DataFrame({\n",
|
56 |
+
" 'sequence': dna_sequences,\n",
|
57 |
+
" 'label1': labels1,\n",
|
58 |
+
" 'label2': labels2,\n",
|
59 |
+
" 'label3': labels3\n",
|
60 |
+
"})\n",
|
61 |
+
"\n",
|
62 |
+
"# Save to CSV\n",
|
63 |
+
"csv_dna_path = \"/data/project/hf_tutorial/data/eval.csv\"\n",
|
64 |
+
"df_dna.to_csv(csv_dna_path, index=False)\n"
|
65 |
+
]
|
66 |
+
},
|
67 |
+
{
|
68 |
+
"cell_type": "code",
|
69 |
+
"execution_count": 5,
|
70 |
+
"metadata": {},
|
71 |
+
"outputs": [],
|
72 |
+
"source": [
|
73 |
+
"# Function to generate a random string of a given length\n",
|
74 |
+
"def generate_random_string(length):\n",
|
75 |
+
" return ''.join(random.choices('ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789', k=length))\n",
|
76 |
+
"\n",
|
77 |
+
"# Generate random strings for each label category\n",
|
78 |
+
"label1_strings = {0: generate_random_string(2), 1: generate_random_string(2)}\n",
|
79 |
+
"label2_strings = {i: generate_random_string(3) for i in range(3)}\n",
|
80 |
+
"label3_strings = {i: generate_random_string(5) for i in range(5)}\n",
|
81 |
+
"\n",
|
82 |
+
"# Save each string to a separate text file\n",
|
83 |
+
"label1_path = \"/data/project/hf_tutorial/data/label1.txt\"\n",
|
84 |
+
"label2_path = \"/data/project/hf_tutorial/data/label2.txt\"\n",
|
85 |
+
"label3_path = \"/data/project/hf_tutorial/data/label3.txt\"\n",
|
86 |
+
"\n",
|
87 |
+
"def save_label_strings(label_strings, path):\n",
|
88 |
+
" with open(path, 'w') as f:\n",
|
89 |
+
" for label, string in label_strings.items():\n",
|
90 |
+
" f.write(f\"{label}: {string}\\n\")\n",
|
91 |
+
"\n",
|
92 |
+
"save_label_strings(label1_strings, label1_path)\n",
|
93 |
+
"save_label_strings(label2_strings, label2_path)\n",
|
94 |
+
"save_label_strings(label3_strings, label3_path)\n"
|
95 |
+
]
|
96 |
+
},
|
97 |
+
{
|
98 |
+
"cell_type": "code",
|
99 |
+
"execution_count": 7,
|
100 |
+
"metadata": {},
|
101 |
+
"outputs": [
|
102 |
+
{
|
103 |
+
"data": {
|
104 |
+
"text/plain": [
|
105 |
+
"CommitInfo(commit_url='https://huggingface.co/datasets/ZJdog/hf_tutorial_dna/commit/8ebcb0406ec3cc2b6cbbdefac6b07ca720603508', commit_message='Initial commit', commit_description='', oid='8ebcb0406ec3cc2b6cbbdefac6b07ca720603508', pr_url=None, pr_revision=None, pr_num=None)"
|
106 |
+
]
|
107 |
+
},
|
108 |
+
"execution_count": 7,
|
109 |
+
"metadata": {},
|
110 |
+
"output_type": "execute_result"
|
111 |
+
}
|
112 |
+
],
|
113 |
+
"source": [
|
114 |
+
"from huggingface_hub import HfApi, HfFolder\n",
|
115 |
+
"\n",
|
116 |
+
"dataset_path = \"/data/project/hf_tutorial/data\" # 你的数据集文件夹路径\n",
|
117 |
+
"dataset_name = \"ZJdog/hf_tutorial_dna\" # 数据集名称\n",
|
118 |
+
"\n",
|
119 |
+
"api = HfApi()\n",
|
120 |
+
"# api.create_repo(repo_id=dataset_name, repo_type=\"dataset\")\n",
|
121 |
+
"api.upload_folder(\n",
|
122 |
+
" repo_id=dataset_name,\n",
|
123 |
+
" folder_path=dataset_path,\n",
|
124 |
+
" repo_type=\"dataset\",\n",
|
125 |
+
" commit_message=\"Initial commit\"\n",
|
126 |
+
")\n"
|
127 |
+
]
|
128 |
+
}
|
129 |
+
],
|
130 |
+
"metadata": {
|
131 |
+
"kernelspec": {
|
132 |
+
"display_name": "Python 3",
|
133 |
+
"language": "python",
|
134 |
+
"name": "python3"
|
135 |
+
},
|
136 |
+
"language_info": {
|
137 |
+
"codemirror_mode": {
|
138 |
+
"name": "ipython",
|
139 |
+
"version": 3
|
140 |
+
},
|
141 |
+
"file_extension": ".py",
|
142 |
+
"mimetype": "text/x-python",
|
143 |
+
"name": "python",
|
144 |
+
"nbconvert_exporter": "python",
|
145 |
+
"pygments_lexer": "ipython3",
|
146 |
+
"version": "3.10.13"
|
147 |
+
}
|
148 |
+
},
|
149 |
+
"nbformat": 4,
|
150 |
+
"nbformat_minor": 2
|
151 |
+
}
|