Register
Login
Resources
Docs Blog Datasets Glossary Case Studies Tutorials & Webinars
Product
Data Engine LLMs Platform Enterprise
Pricing Explore
Connect to our Discord channel

gandlf_constructCSV 2.9 KB

You have to be logged in to leave a comment. Sign In
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
  1. #!usr/bin/env python
  2. # -*- coding: utf-8 -*-
  3. import os, argparse, sys, ast
  4. from datetime import date
  5. from GANDLF.utils import writeTrainingCSV
  6. from GANDLF.cli import copyrightMessage
  7. import yaml
  8. def main():
  9. parser = argparse.ArgumentParser(
  10. prog="GANDLF_ConstructCSV",
  11. formatter_class=argparse.RawTextHelpFormatter,
  12. description="Generate training/inference CSV from data directory.\n\n"
  13. + copyrightMessage,
  14. )
  15. parser.add_argument(
  16. "-i",
  17. "--inputDir",
  18. metavar="",
  19. type=str,
  20. help="Input data directory which contains images in specified format",
  21. )
  22. parser.add_argument(
  23. "-c",
  24. "--channelsID",
  25. metavar="",
  26. type=str,
  27. help="Channels/modalities identifier string to check for in all files in 'input_dir'; for example: --channelsID _t1.nii.gz,_t2.nii.gz",
  28. )
  29. parser.add_argument(
  30. "-l",
  31. "--labelID",
  32. default=None,
  33. type=str,
  34. help="Label/mask identifier string to check for in all files in 'input_dir'; for example: --labelID _seg.nii.gz",
  35. )
  36. parser.add_argument(
  37. "-o",
  38. "--outputFile",
  39. metavar="",
  40. type=str,
  41. help="Output CSV file",
  42. )
  43. parser.add_argument(
  44. "-r",
  45. "--relativizePaths",
  46. metavar="",
  47. type=ast.literal_eval,
  48. default=False,
  49. help="If True, paths in the output data CSV will always be relative to the location of the output data CSV itself.",
  50. )
  51. args = parser.parse_args()
  52. # check for required parameters - this is needed here to keep the cli clean
  53. for param_none_check in [
  54. args.inputDir,
  55. args.channelsID,
  56. args.outputFile,
  57. ]:
  58. if param_none_check is None:
  59. sys.exit("ERROR: Missing required parameter:", param_none_check)
  60. inputDir = os.path.normpath(args.inputDir)
  61. outputFile = os.path.normpath(args.outputFile)
  62. channelsID = args.channelsID
  63. labelID = args.labelID
  64. relativizePathsToOutput = args.relativizePaths
  65. # Do some special handling for if users pass a yml file for channel/label IDs
  66. # This is used for MLCube functionality because MLCube does not support plain string inputs.
  67. if channelsID.endswith(".yml") or channelsID.endswith(".yaml"):
  68. if os.path.isfile(channelsID):
  69. with open(channelsID, "r") as f:
  70. content = yaml.safe_load(f)
  71. channelsID = content["channels"]
  72. if isinstance(channelsID, list):
  73. channelsID = ",".join(channelsID)
  74. if "label" in content:
  75. labelID = content["label"]
  76. if isinstance(labelID, list):
  77. channelsID = ",".join(channelsID)
  78. writeTrainingCSV(inputDir, channelsID, labelID, outputFile, relativizePathsToOutput)
  79. # main function
  80. if __name__ == "__main__":
  81. main()
Tip!

Press p or to see the previous file or, n or to see the next file

Comments

Loading...