Blame view

src/core/pyros_django/scheduling/models.py 1.65 KB
e5e21954   Alexis Koralewski   Add Scheduler model
1
2
3
4
5
6
7
8
9
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
a8cce62b   Alain Klotz   Json bdd and sche...
10
import ast
e5e21954   Alexis Koralewski   Add Scheduler model
11
12
13
14
15
16
17
18

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

a8cce62b   Alain Klotz   Json bdd and sche...
19
    
e5e21954   Alexis Koralewski   Add Scheduler model
20

9a587b42   Alexis Koralewski   Renaming attribut...
21
22
class EffectiveSchedule(models.Model):
    scheduler_matrix = JSONField(blank=True, null=True)    
a8cce62b   Alain Klotz   Json bdd and sche...
23
24
    
    
e5e21954   Alexis Koralewski   Add Scheduler model
25
26
    def save(self, *args, **kwargs):
        # Transform numpy matrix to JSON
def6f098   Alain Klotz   Fix bug in the bd...
27
        scheduler_matrix_as_json = json.dumps(self.scheduler_matrix, cls=NumpyArrayEncoder)
9a587b42   Alexis Koralewski   Renaming attribut...
28
29
        self.scheduler_matrix = scheduler_matrix_as_json
        super(EffectiveSchedule, self).save(*args, **kwargs)
e5e21954   Alexis Koralewski   Add Scheduler model
30

a8cce62b   Alain Klotz   Json bdd and sche...
31
    def conv_numpy(self):
e5e21954   Alexis Koralewski   Add Scheduler model
32
        # Transform JSON to numpy matrix 
9a587b42   Alexis Koralewski   Renaming attribut...
33
        matrix_as_json = json.loads(self.scheduler_matrix)
a8cce62b   Alain Klotz   Json bdd and sche...
34
35
36
      
        return numpy.array(matrix_as_json)
        
9a587b42   Alexis Koralewski   Renaming attribut...
37
38
class PredictiveSchedule(models.Model):
    scheduler_matrix = JSONField(blank=True, null=True)    
a8cce62b   Alain Klotz   Json bdd and sche...
39
    
9a587b42   Alexis Koralewski   Renaming attribut...
40
41
42
43
    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...
44
        super(PredictiveSchedule, self).save(*args, **kwargs)
9a587b42   Alexis Koralewski   Renaming attribut...
45

a8cce62b   Alain Klotz   Json bdd and sche...
46
    def conv_numpy(self, *args, **kwargs):
9a587b42   Alexis Koralewski   Renaming attribut...
47
48
        # Transform JSON to numpy matrix 
        matrix_as_json = json.loads(self.scheduler_matrix)
a8cce62b   Alain Klotz   Json bdd and sche...
49
50
51
     
        return numpy.array(matrix_as_json) 
         
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