-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcreate_folder_structure_bids_script.py
159 lines (159 loc) · 4.06 KB
/
create_folder_structure_bids_script.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
{
"cells": [
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# this script is for creating the BIDS folder structure and standard file names based on the number of subjects in your neuroimaging study, enjoy!\n",
"# import the os module\n",
"import os\n",
"\n",
"# detect the current working directory and print it\n",
"path = os.getcwd()\n",
"print (\"The current working directory is %s\" % path)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"ls #listing files the files in the working dir"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"# define the name of the directory to be created, checking access via creating some folders, setting working dir\n",
"path = \"tmp/month/week\"\n",
"\n",
"try:\n",
" os.makedirs(path)\n",
"except OSError:\n",
" print (\"Creation of the directory %s failed\" % path)\n",
"else:\n",
" print (\"Successfully created the directory %s\" % path)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"Dogs=[\"Akira\",\"Alma\",\"Apacs\",\"Barack\",\"Barney\",\"Bodor\",\"Bodza\",\"Bran\",\"Buksi\",\"Csele\",\"Demi\",\"Floyd\",\"Grog\",\"Joey\",\"Kamilla\",\"Kara\",\"Kefir\",\"Kope\",\"Kosza\",\"Kunkun\",\"Luna\",\"Maverick\",\"Maya\",\"Mini\",\"Mirza\",\"Mokka\",\"Molly\",\"Monty\",\"Nia\",\"Odin\",\"Pan\",\"Sander\",\"Yuki\"]\n",
"dogs=[]\n",
"\n",
"for s in Dogs:\n",
" z=s.lower()\n",
" dogs.append(z)\n",
"\n",
"dogs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#creating BIDS folder structure for all subjects\n",
"for i in range(1,34):\n",
" Path(\"sub-\"+str(i).zfill(2)+\"/func\").mkdir(parents=True, exist_ok=True)\n",
" Path(\"sub-\"+str(i).zfill(2)+\"/anat\").mkdir(parents=True, exist_ok=True)"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#creating new file names based on total no. of subjects\n",
"subs=[]\n",
"for i in range(1,34):\n",
" z=\"sub-\"+str(i).zfill(2)+\"_func_sub-\"+str(i).zfill(2)+\"_dog-rest_bold\"\n",
" subs.append(z)\n",
"\n",
"subs"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#putting them together in a dict\n",
"keys = dogs\n",
"values = subs\n",
"dictionary = dict(zip(keys, values))\n",
"dictionary"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#adding some extra for session renaming\n",
"dictionary.update({\"_rsfm_A_FH\": \"_run-01\", \"_rsfm_B_FH\" : \"_run-02\", \"wBBT_wr\":\"\"})\n",
"dictionary"
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#renaming the file after going through all the replacement steps in the filename string\n",
"for fileName in os.listdir(\".\"):\n",
" address= fileName\n",
" for key in dictionary.keys():\n",
" address =address.replace(key, dictionary[key])\n",
" \n",
" os.rename(fileName, address)\n",
" "
]
},
{
"cell_type": "code",
"execution_count": null,
"metadata": {},
"outputs": [],
"source": [
"#checking that renaming run successfully\n",
"os.listdir(\".\")"
]
}
],
"metadata": {
"kernelspec": {
"display_name": "Python 3",
"language": "python",
"name": "python3"
},
"language_info": {
"codemirror_mode": {
"name": "ipython",
"version": 3
},
"file_extension": ".py",
"mimetype": "text/x-python",
"name": "python",
"nbconvert_exporter": "python",
"pygments_lexer": "ipython3",
"version": "3.7.6"
}
},
"nbformat": 4,
"nbformat_minor": 4
}