Example #1
0
 def log_model(self, model, **keywords):
     params = model.get_params()
     name = get_model_name(model)
     dt = datetime.datetime.utcnow() 
     model = {'name': name, 'parameters': params, 'datetime': dt}
     model.update(keywords)
     inserted_id = self.collection.insert_one(model).inserted_id
     return str(inserted_id)
 def send_event(self, event):
     data = {}
     data.update({'item': utils.get_serializer_class(
         self.__class__.__name__)(instance=self).data})
     data.update(
         {'model_name': utils.get_model_name(self.__class__.__name__)})
     send_event(event, UnicodeJSONRenderer()
                .render(data=data), channel="stream")
Example #3
0
    def add (self, index_model, force=False) :
        if self.is_lock() : return

        name = utils.get_model_name(index_model.Meta.model)
        if not force and self.has_key(name) :
            return

        self.candidate.setdefault(name, list())
        self.candidate[name].append(index_model)
Example #4
0
    def post_save (self, instance=None, sender=None, created=False, **kwargs) :
        import core
        core.initialize()

        index_models = sys.MODELS_REGISTERED.get(utils.get_model_name(instance), None)
        for index_model in index_models :
            if created :
                sys.INDEX_MANAGER.index(
                    instance,
                    analyzer=index_model._meta.analyzer,
                    field_analyzers=index_model._meta.field_analyzers,
                )
            else :
                sys.INDEX_MANAGER.index_update(
                    instance,
                    analyzer=index_model._meta.analyzer, 
                    field_analyzers=index_model._meta.field_analyzers,
                )
Example #5
0
def get_tag(cls):
    return "/%s/"%get_model_name(cls)
 def __init__(self, *args, **kwargs):
     scope_prefix = utils.get_model_name(self._meta.model.__name__)
     kwargs.update({'scope_prefix': scope_prefix})
     super(FormHelperMixin, self).__init__(*args, **kwargs)
     self.helper = TweetHelper(form=self)
     self.helper.form_id = utils.get_form_id(self)
Example #7
0
    def is_added (self, f) :
        name = utils.get_model_name(f.Meta.model)
        if not self.has_key(name) :
            return False

        return self.get(name).count(f) > 0
Example #8
0
    def __str__(self):
        return self.archive_string

    @classmethod
    def from_ini(cls, archive, ini):
        model = super(ArchivedDAQModel, cls).from_ini(archive.modelname, ini)
        model._archive_string = str(archive)
        return model


if __name__ == '__main__':
    import sys
    import os
    if len(sys.argv) > 1:
        filename = sys.argv[1]
    else:
        filename = 'examples/G2SSM.ini'

    with open(filename) as ini:
        dm = DAQModel.from_ini(utils.get_model_name(filename), ini)
    
    for chan in dm.channels:
        print "{c.name}: {c.datarate}Hz, enabled={c.enabled}, acquire={c.acquire}".format(c=chan)

    chan = dm.find_channel('G2:SSM-AUXSUS_LEFT_EXC_DQ')
    print chan.name

    with open('out.ini', 'wb') as ini:
        dm.to_ini(ini)
Example #9
0
    def __call__(self, trained_model):
        #Read md template and compile to html
        pkg = os.path.dirname(os.path.abspath(__file__))
        filepath = os.path.join(pkg, 'templates', 'classification_default.md')
        f = open(filepath, 'r')
        md = f.read()
        markdown = mistune.Markdown()
        html = markdown(md)
    
        #Add css
        filepath = os.path.join(pkg, 'templates', 'github.css')
        f = open(filepath, 'r')
        css = f.read()
        html = '<style>'+css+'</style>'+html
        t = Template(html)

        #Get the placeholders in the template
        #so things that the user do not want are not computed

        #Get name for the model
        model_class_name = get_model_name(trained_model.model)
        #If trained_model does not have a model_name, use model_class_name
        model_name = model_class_name if trained_model.model_name is None else trained_model.model_name
    
        #Feature importance
        try:
            fi = p.feature_importance(trained_model.model, trained_model.feature_names)
            fi_content = figure2html(fi)
        except AttributeError:
            fi_content = '%s does not support feature importances' % (model_class_name)
        except TypeError:
            fi_content = 'To compute this plot you need to provide a model and a feature list'
        except:
            fi_content = 'An unkwown error happened while computing feature importances plot'

        #Confusion matrix
        try:
            cm = p.confusion_matrix(trained_model.y_true, trained_model.y_pred,
                trained_model.target_names)
            cm_content = figure2html(cm)
        except TypeError:
            cm_content = 'To compute this plot you need to provide y_true, y_pred and target_names'
        except:
            cm_content = 'An unkwown error happened while computing the confusion matrix'

        #ROC
        roc = p.roc(trained_model.y_true, trained_model.y_score)
        roc_base64 = figure2base64(roc)
        #Precision-Recall
        pr = p.precision_recall(trained_model.y_true, trained_model.y_score)
        pr_base64 = figure2base64(pr)
    
        d = {'model_name': model_name,
             'date': datetime.now().strftime('%B %d %Y %H:%M'),
             'model_properties': prettify_dict(trained_model.model.get_params()),
             'feature_names':  prettify_list(trained_model.feature_names),
             'feature_importance_plot':  fi_content,
             'confusion_matrix': cm_content,
             'roc': base64_2_html(roc_base64),
             'precision_recall': base64_2_html(pr_base64),
             }
    
        #Replace values in template
        t = t.substitute(d)
        #If path is provided, save report to disk
        if self.savepath is not None:
            path_to_report = os.path.join(self.savepath, model_name+'.html')
            report_file = open(path_to_report, 'w')
            report_file.write(t)
            report_file.close()
        else:
            return t
Example #10
0
    def post_delete (self, instance=None, sender=None, **kwargs) :
        index_models = sys.MODELS_REGISTERED.get(utils.get_model_name(instance), None)

        for index_model in index_models :
            if index_model._meta.casecade_delete :
                sys.INDEX_MANAGER.unindex(instance)
Example #11
0
    def generate_report(self, model, y_true, y_pred, y_score, feature_list, target_names, name):
        # Read md template and compile to html
        pkg = os.path.dirname(os.path.abspath(__file__))
        filepath = os.path.join(pkg, "templates", "classification_default.md")
        f = open(filepath, "r")
        md = f.read()
        markdown = mistune.Markdown()
        html = markdown(md)

        # Add css
        filepath = os.path.join(pkg, "templates", "github.css")
        f = open(filepath, "r")
        css = f.read()
        html = "<style>" + css + "</style>" + html
        t = Template(html)

        # Get the placeholders in the template
        # so things that the user do not want are not computed

        # Get name for the model
        model_name = get_model_name(model)

        # Feature importance
        try:
            fi = p.feature_importance(model, feature_list)
            fi_content = figure2html(fi)
        except AttributeError:
            fi_content = "%s does not support feature importances" % (model_name)
        except TypeError:
            fi_content = "To compute this plot you need to provide a model and a feature list"
        except:
            fi_content = "An unkwown error happened while computing feature importances plot"

        # Confusion matrix
        try:
            cm = p.confusion_matrix_(y_true, y_pred, target_names)
            cm_content = figure2html(cm)
        except TypeError:
            cm_content = "To compute this plot you need to provide y_true, y_pred and target_names"
        except:
            cm_content = "An unkwown error happened while computing the confusion matrix"

        # ROC
        roc = p.roc(y_true, y_score)
        roc_base64 = figure2base64(roc)
        # Precision-Recall
        pr = p.precision_recall(y_true, y_score)
        pr_base64 = figure2base64(pr)

        d = {
            "model_name": model_name,
            "date": datetime.now().strftime("%B %d %Y %H:%M"),
            "model_properties": prettify_dict(model.get_params()),
            "feature_list": prettify_list(feature_list),
            "feature_importance_plot": fi_content,
            "confusion_matrix": cm_content,
            "roc": base64_2_html(roc_base64),
            "precision_recall": base64_2_html(pr_base64),
        }

        # Replace values in template
        t = t.substitute(d)
        # If path is provided, save report to disk
        if self.savepath is not None:
            name = d["model_name"] + ".html" if name == None else name
            report_file = open(self.savepath + name, "w")
            report_file.write(t)
            report_file.close()
        else:
            return t