ProjectManager.py 2.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. from glob import glob
  2. from json import load, dump
  3. from os import path, mkdir
  4. from shutil import rmtree
  5. from time import time
  6. from uuid import uuid1
  7. from pycs import ApplicationStatus
  8. from pycs.observable import ObservableDict
  9. from pycs.pipeline.PipelineManager import PipelineManager
  10. from pycs.projects.Project import Project
  11. class ProjectManager(ObservableDict):
  12. def __init__(self, app_status: ApplicationStatus):
  13. # TODO create projects folder if it does not exist
  14. self.app_status = app_status
  15. # initialize observable dict with no keys and
  16. # app_status object as parent
  17. super().__init__({}, app_status)
  18. app_status['projects'] = self
  19. # find projects
  20. for folder in glob('projects/*'):
  21. # load project.json
  22. with open(path.join(folder, 'project.json'), 'r') as file:
  23. project = Project(load(file), self)
  24. self[project['id']] = project
  25. def write_project(self, uuid):
  26. with open(path.join('projects', uuid, 'project.json'), 'w') as file:
  27. copy = self[uuid].copy()
  28. del copy['jobs']
  29. dump(copy, file, indent=4)
  30. def create_project(self, name, description, model):
  31. # create dict representation
  32. uuid = str(uuid1())
  33. self[uuid] = Project({
  34. 'id': uuid,
  35. 'name': name,
  36. 'description': description,
  37. 'created': int(time()),
  38. 'pipeline': {
  39. 'model-distribution': model
  40. },
  41. 'data': {},
  42. 'labels': {},
  43. 'jobs': {}
  44. }, self)
  45. # create project directory
  46. folder = path.join('projects', uuid)
  47. mkdir(folder)
  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):
  67. # abort if uuid is no valid key
  68. if uuid not in self.keys():
  69. return
  70. project = self[uuid]
  71. # load pipeline
  72. with PipelineManager(project) as pm:
  73. # TODO add jobs to list
  74. # run predictions
  75. for file_id in identifiers:
  76. if file_id in project['data'].keys():
  77. pm.run(project['data'][file_id])