Coverage for sparkle/CLI/add_feature_extractor.py: 0%

52 statements  

« prev     ^ index     » next       coverage.py v7.6.1, created at 2024-09-27 09:10 +0000

1#!/usr/bin/env python3 

2"""Sparkle command to add a feature extractor to the Sparkle platform.""" 

3import os 

4import sys 

5import shutil 

6import argparse 

7from pathlib import Path 

8 

9from sparkle.platform import file_help as sfh 

10from sparkle.CLI.help import global_variables as gv 

11from sparkle.structures import FeatureDataFrame 

12from sparkle.CLI.compute_features import compute_features 

13from sparkle.CLI.help import logging as sl 

14from sparkle.platform import CommandName, COMMAND_DEPENDENCIES 

15from sparkle.CLI.initialise import check_for_initialise 

16from sparkle.CLI.help import argparse_custom as ac 

17from sparkle.solver import Extractor 

18 

19 

20def parser_function() -> argparse.ArgumentParser: 

21 """Define the command line arguments.""" 

22 # Define command line arguments 

23 parser = argparse.ArgumentParser() 

24 parser.add_argument(*ac.ExtractorPathArgument.names, 

25 **ac.ExtractorPathArgument.kwargs) 

26 group_extractor_run = parser.add_mutually_exclusive_group() 

27 group_extractor_run.add_argument(*ac.RunExtractorNowArgument.names, 

28 **ac.RunExtractorNowArgument.kwargs) 

29 parser.add_argument(*ac.NicknameFeatureExtractorArgument.names, 

30 **ac.NicknameFeatureExtractorArgument.kwargs) 

31 return parser 

32 

33 

34if __name__ == "__main__": 

35 # Log command call 

36 sl.log_command(sys.argv) 

37 

38 parser = parser_function() 

39 

40 # Process command line arguments 

41 args = parser.parse_args() 

42 

43 check_for_initialise(COMMAND_DEPENDENCIES[CommandName.ADD_FEATURE_EXTRACTOR]) 

44 

45 extractor_source = Path(args.extractor_path) 

46 if not extractor_source.exists(): 

47 print(f'Feature extractor path "{extractor_source}" does not exist!') 

48 sys.exit(-1) 

49 

50 nickname_str = args.nickname 

51 

52 # Start add feature extractor 

53 extractor_target_path = gv.settings().DEFAULT_extractor_dir / extractor_source.name 

54 

55 if extractor_target_path.exists(): 

56 print(f"Feature extractor {extractor_source.name} already exists! " 

57 "Can not add feature extractor.") 

58 sys.exit(-1) 

59 extractor_target_path.mkdir() 

60 shutil.copytree(extractor_source, extractor_target_path, dirs_exist_ok=True) 

61 

62 # Check execution permissions for wrapper 

63 extractor_wrapper = extractor_target_path / Extractor.wrapper 

64 if not extractor_wrapper.is_file() or not os.access(extractor_wrapper, os.X_OK): 

65 print(f"The file {extractor_wrapper} does not exist or is \ 

66 not executable.") 

67 sys.exit(-1) 

68 

69 # Get the extractor features groups and names from the wrapper 

70 extractor = Extractor(extractor_target_path) 

71 feature_dataframe = FeatureDataFrame(gv.settings().DEFAULT_feature_data_path) 

72 feature_dataframe.add_extractor(extractor.name, extractor.features) 

73 feature_dataframe.save_csv() 

74 

75 print(f"Adding feature extractor {extractor_target_path.name} done!") 

76 

77 if nickname_str is not None: 

78 sfh.add_remove_platform_item( 

79 extractor_target_path, 

80 gv.extractor_nickname_list_path, 

81 gv.file_storage_data_mapping[gv.extractor_nickname_list_path], 

82 key=nickname_str) 

83 

84 if args.run_extractor_now: 

85 print("Start computing features ...") 

86 compute_features(gv.settings().DEFAULT_feature_data_path, False) 

87 

88 # Write used settings to file 

89 gv.settings().write_used_settings()