-
Notifications
You must be signed in to change notification settings - Fork 30
Expand file tree
/
Copy path_getcodingstruct.py
More file actions
201 lines (161 loc) · 6.6 KB
/
Copy path_getcodingstruct.py
File metadata and controls
201 lines (161 loc) · 6.6 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
# Software Name: Cool-Chic
# SPDX-FileCopyrightText: Copyright (c) 2023-2026 Orange
# SPDX-License-Identifier: BSD 3-Clause "New"
#
# This software is distributed under the BSD-3-Clause license.
#
# Authors: see CONTRIBUTORS.md
import argparse
import sys
from coolchic.utils.codingstructure import CodingStructure
from coolchic.utils.parsecli import get_coding_structure_from_args
def get_job_dependencies(coding_structure: CodingStructure) -> str:
"""Output a bash-like script file with dependencies for the coding
structure. Everything is indexed in display order.
Example
-------
With this coding structure,
I0 I5
\-------------------> B2 <-------------------------------/
\-------> B1 <-------/ \-------> B3 <-------------------/
\-------> B4 <-------/
it produces a string containing:
jobid_0000=$(/usr/local/bin/sbatch --parsable frame0000.sh)
jobid_0005=$(/usr/local/bin/sbatch --parsable frame0005.sh)
jobid_0002=$(/usr/local/bin/sbatch --parsable --dependency=afterok:${jobid_0000}:${jobid_0005} frame0002.sh)
jobid_0001=$(/usr/local/bin/sbatch --parsable --dependency=afterok:${jobid_0000}:${jobid_0002} frame0001.sh)
jobid_0003=$(/usr/local/bin/sbatch --parsable --dependency=afterok:${jobid_0002}:${jobid_0005} frame0003.sh)
jobid_0004=$(/usr/local/bin/sbatch --parsable --dependency=afterok:${jobid_0003}:${jobid_0005} frame0004.sh)
Args:
coding_structure (CodingStructure): The coding structure for which we
want to list the dependencies.
Returns:
str: A string with a pseudo bash script listing all the dependencies.
See above for an example.
"""
SBATCH_PATH = "/usr/local/bin/sbatch"
s = ""
def get_display_idx_str(display_order: int) -> str:
return str(display_order).zfill(4)
# We **must** launch job in coding order to retrieve the job id for
# the dependencies. Otherwise, everything is in display order!
for coding_order in range(0, coding_structure.get_max_coding_order() + 1):
frame = coding_structure.get_frame_from_coding_order(coding_order)
frame_display_idx_str = get_display_idx_str(frame.display_order)
# No dependencies
if frame.index_references:
dependencies = []
for idx_ref_display_order in frame.index_references:
idx_ref_display_order = coding_structure.get_frame_from_display_order(
idx_ref_display_order
).display_order
dependencies.append(get_display_idx_str(idx_ref_display_order))
dependencies = ":".join(
[
"${" + f"jobid_{idx_ref_display_order}" + "}"
for idx_ref_display_order in dependencies
]
)
dependencies = f"--dependency=afterok:{dependencies}"
# No reference = no dependency
else:
dependencies = ""
s += (
f"jobid_{frame_display_idx_str}="
f"$({SBATCH_PATH} --parsable "
f"{dependencies} "
f"frame{frame_display_idx_str}.sh)"
"\n"
)
return s
def get_raw_coding_structure(coding_structure: CodingStructure) -> str:
"""Get an easily parsable string representing the coding structure.
Example
-------
With this coding structure,
I0 I5
\-------------------> B2 <-------------------------------/
\-------> B1 <-------/ \-------> B3 <-------------------/
\-------> B4 <-------/
it produces a string containing:
coding display type depth ref1 ref2
0 0 I 0
1 5 I 0
2 2 B 1 0 5
3 1 B 2 0 2
4 3 B 2 2 5
5 4 B 3 3 5
Args:
coding_structure (CodingStructure): The coding structure for which we
want to list the frames.
Returns:
str: Easily parsable string describing the coding structure. See above.
"""
s = "coding\tdisplay\ttype\tdepth\tref1\tref2\n"
for coding_order in range(0, coding_structure.get_max_coding_order() + 1):
frame = coding_structure.get_frame_from_coding_order(coding_order)
s += (
f"{frame.coding_order}\t"
f"{frame.display_order}\t"
f"{frame.frame_type}\t"
f"{frame.depth}\t"
f"{frame.index_references[0] if frame.index_references else ''}\t"
f"{frame.index_references[1] if len(frame.index_references) > 1 else ''}\t"
"\n"
)
return s
if __name__ == "__main__":
parser = argparse.ArgumentParser()
# ----- CodingStructure parameters, copy of cc_encode.py args
parser.add_argument(
"--intra_pos",
help="Display index of the intra frames. "
"Format is 0,4,7 if you want the frame 0, 4 and 7 to be intra frames. "
"-1 can be used to denote the last frame, -2 the 2nd to last etc. "
"x-y is a range from x (included) to y (included). This does not work "
"with the negative indexing. "
"0,4-7,-2 ==> Intra for the frame 0, 4, 5, 6, 7 and the 2nd to last."
"Frame 0 must be an intra frame.",
type=str,
default="0",
)
parser.add_argument(
"--p_pos",
help="Display index of the P frames. Same format than --intra_pos ",
type=str,
default="",
)
parser.add_argument(
"--n_frames",
help="How many frames to code",
type=int,
default=1,
)
parser.add_argument(
"--frame_offset",
help="Shift the position of the 0-th frame of the video. "
"If --frame_offset=15 skip the first 15 frames of the video.",
type=int,
default=0,
)
# ----- What do we want to generate
parser.add_argument(
"--job_dependency", help="Print slurm job dependencies", action="store_true"
)
parser.add_argument(
"--raw_coding_struct",
help="Print raw coding structure in a tsv-like format",
action="store_true",
)
args = parser.parse_args()
if args.job_dependency and args.raw_coding_struct:
print(
"It is not possible to use --job_dependency and --raw_coding_struct"
" at the same time. Use one or the other"
)
sys.exit(1)
coding_structure = CodingStructure(**get_coding_structure_from_args(args))
if args.job_dependency:
print(get_job_dependencies(coding_structure))
if args.raw_coding_struct:
print(get_raw_coding_structure(coding_structure))