Blame view

src/core/pyros_django/scheduling/models.py 1.73 KB
e5e21954   Alexis Koralewski   Add Scheduler model
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
from django.db import models
from django.db.models import JSONField, DateField
import numpy as np
# Create your models here.


import json
from json import JSONEncoder
import numpy

class NumpyArrayEncoder(JSONEncoder):
    def default(self, obj):
        if isinstance(obj, numpy.ndarray):
            return obj.tolist()
        return JSONEncoder.default(self, obj)
    


9a587b42   Alexis Koralewski   Renaming attribut...
19
20
class EffectiveSchedule(models.Model):
    scheduler_matrix = JSONField(blank=True, null=True)    
e5e21954   Alexis Koralewski   Add Scheduler model
21
22
23

    def save(self, *args, **kwargs):
        # Transform numpy matrix to JSON
9a587b42   Alexis Koralewski   Renaming attribut...
24
25
26
        scheduler_matrix_as_json = json.dumps(self.scheduler_matrix, cls=NumpyArrayEncoder)
        self.scheduler_matrix = scheduler_matrix_as_json
        super(EffectiveSchedule, self).save(*args, **kwargs)
e5e21954   Alexis Koralewski   Add Scheduler model
27
28
29

    def get(self, *args, **kwargs):
        # Transform JSON to numpy matrix 
9a587b42   Alexis Koralewski   Renaming attribut...
30
        matrix_as_json = json.loads(self.scheduler_matrix)
e5e21954   Alexis Koralewski   Add Scheduler model
31

9a587b42   Alexis Koralewski   Renaming attribut...
32
        self.scheduler_matrix = numpy.asarray(matrix_as_json["array"]) 
e5e21954   Alexis Koralewski   Add Scheduler model
33
34
        return super().get(**kwargs)
    
9a587b42   Alexis Koralewski   Renaming attribut...
35
36
37
38
39
40
41
class PredictiveSchedule(models.Model):
    scheduler_matrix = JSONField(blank=True, null=True)    

    def save(self, *args, **kwargs):
        # Transform numpy matrix to JSON
        scheduler_matrix_as_json = json.dumps(self.scheduler_matrix, cls=NumpyArrayEncoder)
        self.scheduler_matrix = scheduler_matrix_as_json
d83ed1cb   Alexis Koralewski   Rename Name of ta...
42
        super(PredictiveSchedule, self).save(*args, **kwargs)
9a587b42   Alexis Koralewski   Renaming attribut...
43
44
45
46
47
48
49

    def get(self, *args, **kwargs):
        # Transform JSON to numpy matrix 
        matrix_as_json = json.loads(self.scheduler_matrix)

        self.scheduler_matrix = numpy.asarray(matrix_as_json["array"]) 
        return super().get(**kwargs)
e5e21954   Alexis Koralewski   Add Scheduler model
50
51
    

9a587b42   Alexis Koralewski   Renaming attribut...
52
class SchedulerHistory(EffectiveSchedule):
d83ed1cb   Alexis Koralewski   Rename Name of ta...
53
    night_datetime = models.DateTimeField(blank=True, null=True)
e5e21954   Alexis Koralewski   Add Scheduler model
54