Exemplo n.º 1
0
def motif_jumper(functionNode):
    """
        jump from one result the the next/prev
        if the increase/decrese is zero, we jump to the motif instead
    """

    logger = functionNode.get_logger()
    logger.info("==>>>> in motif_jumper " + functionNode.get_browse_path())

    minerNode = functionNode.get_child("miner").get_targets()[0]
    widgetNode = minerNode.get_child("widget").get_targets()[0]

    dates = minerNode.get_child("peaks").get_value()
    if type(dates) is not list:
        dates = []
    currentIndex = functionNode.get_child("jumpPos").get_value()
    inc = functionNode.get_child("jumpInc").get_value()

    if inc != 0:
        nextIndex = currentIndex + inc
        if nextIndex >= len(dates):
            nextIndex = 0
        functionNode.get_child("jumpPos").set_value(nextIndex)

        logger.debug(f"jump to index {nextIndex}  : {dates[nextIndex]}")

    motif = minerNode.get_child("motif").get_targets()[0]
    motifStart = motif.get_child("startTime").get_value()
    motifEnd = motif.get_child("endTime").get_value()
    motifWidth = date2secs(motifEnd) - date2secs(motifStart)
    currentViewStart = widgetNode.get_child("startTime").get_value()
    currentViewEnd = widgetNode.get_child("endTime").get_value()
    currentViewWidth = date2secs(currentViewEnd) - date2secs(currentViewStart)

    if inc == 0:
        center = date2secs(motifStart) + 0.5 * (motifWidth)
        windowStart = center - currentViewWidth / 2
        windowEnd = center + currentViewWidth / 2
        widgetNode.get_child("startTime").set_value(
            epochToIsoString(windowStart, zone=timezone('Europe/Berlin')))
        widgetNode.get_child("endTime").set_value(
            epochToIsoString(windowEnd, zone=timezone('Europe/Berlin')))

    else:
        if len(dates):
            center = date2secs(dates[nextIndex]) - 0.5 * motifWidth
            windowStart = center - currentViewWidth / 2
            windowEnd = center + currentViewWidth / 2
            widgetNode.get_child("startTime").set_value(
                epochToIsoString(windowStart, zone=timezone('Europe/Berlin')))
            widgetNode.get_child("endTime").set_value(
                epochToIsoString(windowEnd, zone=timezone('Europe/Berlin')))

    return True
Exemplo n.º 2
0
def generate_peaks(resultVector,functionNode,logger,timeNode,MotifLen,MotifStart,MotifEnd):
    v = resultVector.copy()
    motifTimeLen = MotifEnd-MotifStart
    v[False == numpy.isfinite(v)] = 0  # pad inf as zero for the peak detector
    level = numpy.float(functionNode.get_parent().get_child("peakSearch").get_child("threshold").get_value())
    peakIndices = detect_peaks(v, min_dist=3 * MotifLen, min_level = level)
    logger.debug(f"found {len(peakIndices)} peaks")
    peaksValues = v[peakIndices]
    if 0: #sort
        sortIndices = numpy.argsort(peaksValues)[::-1]
        sortedPeakIndices = peakIndices[sortIndices]
        sortedPeakIndices = sortedPeakIndices[0:min(len(sortedPeakIndices), 10)]  # max 10 peaks
    else:
        sortedPeakIndices = peakIndices
    peakepochs = timeNode.get_value()[sortedPeakIndices]


    peakDates = [epochToIsoString(peak, zone=timezone('Europe/Berlin')) for peak in peakepochs]
    if peakDates:
        print(f"peakDATES {peakDates}")
        current = functionNode.get_child("peaks").get_value()
        if not type(current) is list:
            current = []
        current.extend(peakDates)
        print(f"update current {current}")
        functionNode.get_child("peaks").set_value(current)

    #now generate annotations
    annotations = functionNode.get_child("annotations")
    myModel = functionNode.get_model()

    newAnnotations=[]
    for peak in peakepochs:
        if peak > (MotifEnd+motifTimeLen/2) or peak < (MotifStart-motifTimeLen/2):
            anno={"type":"time","startTime":"","endTime":"","tags":["pattern_match"]}
            anno["startTime"]=epochToIsoString(peak, zone=timezone('Europe/Berlin'))
            anno["endTime"] = epochToIsoString(peak+motifTimeLen, zone=timezone('Europe/Berlin'))
            newAnnotations.append(anno)

    myModel.disable_observers()
    for anno in newAnnotations:
        # create the annotation in the model
        newAnno = annotations.create_child(type="annotation")
        for k, v in anno.items():
            newAnno.create_child(properties={"name": k, "value": v, "type": "const"})



    myModel.enable_observers()
    myModel.notify_observers(annotations.get_id(), "children")
Exemplo n.º 3
0
def motif_jumper(functionNode):

    logger = functionNode.get_logger()
    logger.info("==>>>> in motif_jumper " + functionNode.get_browse_path())

    minerNode = functionNode.get_child("miner").get_targets()[0]
    widgetNode = minerNode.get_child("widget").get_targets()[0]

    dates = minerNode.get_child("peaks").get_value()
    if type(dates) is not list:
        dates = []
    currentIndex = functionNode.get_child("jumpPos").get_value()
    inc = functionNode.get_child("jumpInc").get_value()

    nextIndex = currentIndex+inc
    if nextIndex>=len(dates):
        nextIndex=0
    functionNode.get_child("jumpPos").set_value(nextIndex)

    logger.debug(f"jump to index {nextIndex}  : {dates[nextIndex]}")

    if len(dates):
        motif = minerNode.get_child("motif").get_targets()[0]
        motifStart = motif.get_child("startTime").get_value()
        motifEnd = motif.get_child("endTime").get_value()

        currentViewStart = widgetNode.get_child("startTime").get_value()
        currentViewEnd = widgetNode.get_child("endTime").get_value()
        currentViewWidth = date2secs(currentViewEnd)- date2secs(currentViewStart)

        windowStart = date2secs(dates[nextIndex])-currentViewWidth/2
        windowEnd = date2secs(dates[nextIndex])+currentViewWidth/2



        widgetNode.get_child("startTime").set_value(epochToIsoString(windowStart,zone=timezone('Europe/Berlin')))
        widgetNode.get_child("endTime").set_value(epochToIsoString(windowEnd, zone=timezone('Europe/Berlin')))

    return True
Exemplo n.º 4
0
def pps_miner(functionNode):

    logger = functionNode.get_logger()
    logger.info("==>>>> in pps_miner " + functionNode.get_browse_path())

    myWidget = functionNode.get_child("widget").get_target()
    myModel = functionNode.get_model()
    motifs = myWidget.get_child("hasAnnotation").get_child(
        "selectedAnnotations").get_leaves()
    annotations = functionNode.get_child("annotations")
    progressNode = functionNode.get_child("control").get_child("progress")
    progressNode.set_value(0)
    """
        preparation:
            - find the current widget, take the current selected selection for my motif
            - if not available
            - connect the result to the table
    """

    motif = None
    if motifs:
        motif = motifs[0]
        functionNode.get_child("motif").add_references(
            motif, deleteAll=True)  # take this annotation
    else:
        #there is currently no selection, we take the motif from last time if we have one
        motifs = functionNode.get_child("motif").get_targets()
        if motifs:
            motif = motifs[0]
    if not motif:
        logger.error("have no motif")
        return False

    # prepare the result: delete previous annotations
    functionNode.get_child("peaks").set_value([])
    try:
        myModel.disable_observers()
        annos = annotations.get_children()
        if annos:
            for anno in annos:
                anno.delete()
    except:
        myModel.enable_observers()
        return False

    myModel.enable_observers()
    myModel.notify_observers(
        annotations.get_id(),
        "children")  # trigger the widgets to delete the annotations

    ####################################
    ## get the settings
    ####################################
    preFilter = functionNode.get_child("preFilter").get_value()
    postFilter = functionNode.get_child("postFilter").get_value()
    subtractPolynomOrder = functionNode.get_child(
        "subtractPolynomOrder").get_value()
    differentiate = functionNode.get_child("differentiate").get_value()
    timeRanges = functionNode.get_child("timeRanges").get_value()
    timeRanges = {int(k): v for k, v in timeRanges.items()}
    valueRanges = functionNode.get_child("valueRanges").get_value()
    valueRanges = {int(k): v for k, v in valueRanges.items()}
    typeFilter = functionNode.get_child("typeFilter").get_value()

    ####################################
    #get the motif
    ####################################
    motifStartTime = date2secs(motif.get_child("startTime").get_value())
    motifEndTime = date2secs(motif.get_child("endTime").get_value())
    motifVariable = motif.get_child("variable").get_targets()[0]

    #motifStartTime = motifStartTime+0.2*(motifEndTime-motifStartTime)
    #print(f"{vars[varName].get_name()},{motifStartTime}")
    motif = motifVariable.get_time_series(start=motifStartTime,
                                          end=motifEndTime)
    #print(motif)
    motifX = motif["__time"]
    motifY0 = motif["values"]
    motifY1 = mnh.pps_prep(motifY0,
                           filter=preFilter,
                           poly=subtractPolynomOrder,
                           diff=differentiate,
                           postFilter=postFilter)
    motifPPS = mnh.prominent_points(motifY1, motifX)

    ####################################
    #get the time series
    ####################################
    # make the time series data and pps
    series = motifVariable.get_time_series()  # the full time
    x = series["__time"]
    t0 = series["__time"][0]
    x = x - t0  # start time from 0
    y0 = series["values"]
    y1 = mnh.pps_prep(y0,
                      filter=preFilter,
                      poly=subtractPolynomOrder,
                      diff=differentiate,
                      postFilter=postFilter)
    pps = mnh.prominent_points(y1, x)

    ####################################
    # MINING
    ####################################
    progressNode.set_value(0.3)
    matches = mnh.pps_mining(motifPPS['pps'],
                             pps['pps'],
                             timeRanges=timeRanges,
                             valueRanges=valueRanges,
                             typeFilter=typeFilter,
                             motifStartIndex=0,
                             debug=False)
    print(
        f"{len(matches)} matches: {[secs2date(t0+m['time']).isoformat() for m in matches]}"
    )

    ####################################
    # create result Annotations
    ####################################

    annoTimeLen = motifEndTime - motifStartTime
    newAnnotations = []
    for m in matches:
        anno = {
            "type": "time",
            "startTime": "",
            "endTime": "",
            "tags": ["pattern_match"]
        }
        anno["startTime"] = epochToIsoString(m["time"] + t0 - annoTimeLen,
                                             zone=timezone('Europe/Berlin'))
        anno["endTime"] = epochToIsoString(m["time"] + t0,
                                           zone=timezone('Europe/Berlin'))
        newAnnotations.append(anno)

    #remove trivial matches (inside a guard area around the motif)
    guard = (motifEndTime - motifStartTime) / 2
    newAnnotations = [
        anno for anno in newAnnotations
        if date2secs(anno["startTime"]) < (motifStartTime - guard)
        or date2secs(anno["startTime"]) > motifEndTime + guard
    ]

    progressNode.set_value(0.6)
    myModel.disable_observers()
    for anno in newAnnotations:
        # create the annotation in the model
        newAnno = annotations.create_child(type="annotation")
        for k, v in anno.items():
            newAnno.create_child(properties={
                "name": k,
                "value": v,
                "type": "const"
            })
    myModel.enable_observers()
    myModel.notify_observers(annotations.get_id(), "children")

    #also write the peaks
    peaks = [
        epochToIsoString(m["time"] + t0, zone=timezone('Europe/Berlin'))
        for m in matches
    ]
    functionNode.get_child("peaks").set_value(peaks)
    progressNode.set_value(1)

    return True