6
0

ProjectManager.py 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106
  1. from glob import glob
  2. from json import load, dump
  3. from os import path, mkdir
  4. from shutil import rmtree, copytree
  5. from time import time
  6. from uuid import uuid1
  7. from pycs import ApplicationStatus
  8. from pycs.observable import ObservableDict
  9. from pycs.projects.Project import Project
  10. class ProjectManager(ObservableDict):
  11. def __init__(self, app_status: ApplicationStatus):
  12. # TODO create projects folder if it does not exist
  13. self.app_status = app_status
  14. # initialize observable dict with no keys and
  15. # app_status object as parent
  16. super().__init__({}, app_status)
  17. app_status['projects'] = self
  18. # find projects
  19. for folder in glob('projects/*'):
  20. # load project.json
  21. with open(path.join(folder, 'project.json'), 'r') as file:
  22. project = Project(load(file), self)
  23. self[project['id']] = project
  24. def write_project(self, uuid):
  25. copy = self[uuid].copy()
  26. del copy['jobs']
  27. del copy['model']
  28. with open(path.join('projects', uuid, 'project.json'), 'w') as file:
  29. dump(copy, file, indent=4)
  30. def create_project(self, name, description, model):
  31. # create dict representation
  32. uuid = str(uuid1())
  33. # create project directory
  34. folder = path.join('projects', uuid)
  35. mkdir(folder)
  36. # copy model to project directory
  37. copytree(self.parent['models'][model]['path'], path.join(folder, 'model'))
  38. # create project object
  39. self[uuid] = Project({
  40. 'id': uuid,
  41. 'name': name,
  42. 'description': description,
  43. 'created': int(time()),
  44. 'data': {},
  45. 'labels': {},
  46. 'jobs': {}
  47. }, self)
  48. # create project.json
  49. self.write_project(uuid)
  50. def update_project(self, uuid, update):
  51. # abort if uuid is no valid key
  52. if uuid not in self.keys():
  53. return
  54. # set values and write to disk
  55. self[uuid].update_properties(update)
  56. self.write_project(uuid)
  57. def delete_project(self, uuid):
  58. # abort if uuid is no valid key
  59. if uuid not in self.keys():
  60. return
  61. # delete project folder
  62. folder = path.join('projects', uuid)
  63. rmtree(folder)
  64. # delete project data
  65. del self[uuid]
  66. def predict(self, uuid, identifiers=None):
  67. # abort if uuid is no valid key
  68. if uuid not in self.keys():
  69. return
  70. project = self[uuid]
  71. # get identifiers
  72. if identifiers is None:
  73. identifiers = list(project['data'].keys())
  74. # run prediction
  75. project.predict(identifiers)
  76. def fit(self, uuid):
  77. # abort if uuid is no valid key
  78. if uuid not in self.keys():
  79. return
  80. project = self[uuid]
  81. # run fit
  82. project.fit()