models.py 1.65 KB
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
import ast

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

    

class EffectiveSchedule(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
        super(EffectiveSchedule, self).save(*args, **kwargs)

    def conv_numpy(self):
        # Transform JSON to numpy matrix 
        matrix_as_json = json.loads(self.scheduler_matrix)
      
        return numpy.array(matrix_as_json)
        
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
        super(PredictiveSchedule, self).save(*args, **kwargs)

    def conv_numpy(self, *args, **kwargs):
        # Transform JSON to numpy matrix 
        matrix_as_json = json.loads(self.scheduler_matrix)
     
        return numpy.array(matrix_as_json) 
         
class SchedulerHistory(EffectiveSchedule):
    night_datetime = models.DateTimeField(blank=True, null=True)