#!/usr/bin/env python
# vim: set fileencoding=utf-8 :
###############################################################################
# #
# Copyright (c) 2017 Idiap Research Institute, http://www.idiap.ch/ #
# Contact: beat.support@idiap.ch #
# #
# This file is part of the beat.web module of the BEAT platform. #
# #
# Commercial License Usage #
# Licensees holding valid commercial BEAT licenses may use this file in #
# accordance with the terms contained in a written agreement between you #
# and Idiap. For further information contact tto@idiap.ch #
# #
# Alternatively, this file may be used under the terms of the GNU Affero #
# Public License version 3 as published by the Free Software and appearing #
# in the file LICENSE.AGPL included in the packaging of this file. #
# The BEAT platform is distributed in the hope that it will be useful, but #
# WITHOUT ANY WARRANTY; without even the implied warranty of MERCHANTABILITY #
# or FITNESS FOR A PARTICULAR PURPOSE. #
# #
# You should have received a copy of the GNU Affero Public License along #
# with the BEAT platform. If not, see http://www.gnu.org/licenses/. #
# #
###############################################################################
from django.db import models
from django.conf import settings
import simplejson
#----------------------------------------------------------
[docs]class ResultManager(models.Manager):
[docs] def get_by_natural_key(self, name, hash):
return self.get(name=name, cache__hash=hash)
#----------------------------------------------------------
[docs]class Result(models.Model):
SIMPLE_TYPE_NAMES = ('int32', 'float32', 'bool', 'string')
cache = models.ForeignKey('CachedFile', related_name='results', null=True,
on_delete=models.CASCADE)
name = models.CharField(max_length=200)
type = models.CharField(max_length=200)
primary = models.BooleanField(default=False)
data_value = models.TextField(null=True, blank=True)
objects = ResultManager()
#_____ Meta parameters __________
class Meta:
unique_together = ('cache', 'name')
def __str__(self):
return '%s - %s' % (self.cache, self.name)
[docs] def natural_key(self):
return (
self.name,
self.cache.hash,
)
natural_key.dependencies = ['experiments.cachedfile']
[docs] def value(self):
if self.data_value in ['+inf', '-inf', 'NaN']:
return self.data_value
elif self.type == 'int32':
return int(self.data_value)
elif self.type == 'float32':
return float(self.data_value)
elif self.type == 'bool':
return bool(self.data_value)
elif self.type == 'string':
return str(self.data_value)
elif self.type.startswith('%s/' % settings.PLOT_ACCOUNT) or (self.type in Result.SIMPLE_TYPE_NAMES):
return simplejson.loads(self.data_value)
return None
[docs] def is_chart(self):
return self.type not in Result.SIMPLE_TYPE_NAMES