Coverage for /home/runner/work/torchcvnn/torchcvnn/src/torchcvnn/datasets/sample/download_filelist.py: 0%
25 statements
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-13 08:53 +0000
« prev ^ index » next coverage.py v7.8.0, created at 2025-04-13 08:53 +0000
1# MIT License
3# Copyright (c) 2024 Jeremy Fix
5# Permission is hereby granted, free of charge, to any person obtaining a copy
6# of this software and associated documentation files (the "Software"), to deal
7# in the Software without restriction, including without limitation the rights
8# to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
9# copies of the Software, and to permit persons to whom the Software is
10# furnished to do so, subject to the following conditions:
12# The above copyright notice and this permission notice shall be included in
13# all copies or substantial portions of the Software.
15# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
18# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
20# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
21# SOFTWARE.
23# Standard imports
24import requests
25from bs4 import BeautifulSoup
26import re
28SAMPLE_base_link = "https://github.com/benjaminlewis-afrl/SAMPLE_dataset_public/blob/refs/heads/master/mat_files/"
30SAMPLE_classes = [
31 "2s1",
32 "bmp2",
33 "btr70",
34 "m1",
35 "m2",
36 "m35",
37 "m548",
38 "m60",
39 "t72",
40 "zsu23",
41]
44def generate_file_list():
45 """
46 Download the list of mat files available for every mode and every class
47 and save them as a dictionnary to be loaded by torchcvnn
49 We do this "asynchronously" because it happens, from time to time, that
50 requests is returning an empty list of files. Requesting and parsing is anyway
51 some overhead we can avoid at runtime.
52 """
53 with open("filelist.py", "w") as fh:
54 fh.write("filelist = {\n")
55 for mode in ["real", "synth"]:
56 fh.write(f'"{mode}": {{\n')
57 for cl in SAMPLE_classes:
58 url = f"{SAMPLE_base_link}{mode}/{cl}/"
59 result = requests.get(url)
60 soup = BeautifulSoup(result.text, "html.parser")
61 matfiles = soup.find_all(title=re.compile("\.mat$"))
63 fh.write(f'"{cl}": [\n')
64 fh.write(",\n".join([f'\t"{f.get("title")}"' for f in matfiles]))
65 fh.write("],\n")
67 if len(matfiles) == 0:
68 # print("=====================================\n")
69 print(f"Empty list of files for {cl} in {mode}")
70 # print(result.text)
71 print(
72 "============================================================\n"
73 )
75 fh.write("},\n")
76 fh.write("}\n")
79if __name__ == "__main__":
80 generate_file_list()