Skip to content

Proposal: periodic #1160

Open
Open

Description

@wmrowan

stream.periodic(n) returns a stream of every nth element of the original stream. I can't think of a way to do this with existing primitives. This could be useful in a number of contexts but I'm primarily thinking that it will be useful with triggers (#997).

Imagine you want to keep a log of the frequency of table inserts but don't need to log every insert. With triggers and periodic you can do this easily with the following script:

import rethinkdb as r
from datetime import datetime

log = open('/tmp/insert_log.txt', 'w+')

c = r.connect(host, port)
for event in r.table('foo').on_insert().periodic(100).run(c):
   log.write("[%s] 100 inserts on 'foo'\n" % datetime.utcnow())

Activity

  1. coffeemug commented on Jul 11, 2013

    @coffeemug
    Contributor

    Intriguing. Moving to backlog since we're past debating timebox for this release, but we'll come back to this.

  2. jdoliner commented on Jul 11, 2013

    @jdoliner
    Contributor

    Psh, just use mapreduce:

    r.expr([{"id" : i} for i in xrange(100)]) \
        .map(lambda x: {"c" : 1, "v" : x, "r" : []}) \
        .reduce(lambda x, y: r.branch(x["c"] + y["c"] >= 4, \
                    {"c" : x["c"] + y["c"] - 4, "v" : x["v"], "r" : x["r"] + y["r"] + [y["v"]]}, \
                    {"c" : x["c"] + y["c"], "v" : y["v"], "r" : x["r"] + y["r"]}))["r"]

    c here stands for counter, v for value and r for result.

  3. jdoliner commented on Jul 11, 2013

    @jdoliner
    Contributor

    (This does every fourth element.)

  4. wmrowan commented on Jul 11, 2013

    @wmrowan
    ContributorAuthor

    Oh I somehow didn't think of that myself. I didn't realize this could be done with existing primitives. I withdraw my proposal then.

  5. jdoliner commented on Jul 11, 2013

    @jdoliner
    Contributor

    I still think this is a totally legit idea, that mapreduce is none too user friendly and it's sinfully inefficient.

  6. coffeemug commented on Jul 11, 2013

    @coffeemug
    Contributor

    Reopening because .periodic(100) is just slightly more user friendly than... that.

  7. mlucy commented on Jul 11, 2013

    @mlucy
    Member

    Also, if we implemented periodic correctly you could call it on a large stream without loading the whole thing into memory.

  8. hardkrash commented on Jul 11, 2014

    @hardkrash

    My signals and systems brain hurts with the term periodic. I think of it as a generator of data like a periodic signal e.g. sin(x).

    The python folks call it a step, it could also could be called a stride.

    This is related to #2664 as these could be considered time series.
    As a you would have these functions.

    • .downsample(M)
      • Taking every Mth sample.
    • .decimate(M)
      • Taking every Mth sample after digital filtering of all intermediary values.

    Arguably you could also add the opposites.

  9. srh commented on Jul 11, 2014

    @srh
    Contributor

    @hardkrash: Consider the possibility of having a data set with points at times ..., t_i, t_{i+1}, t_{i+2}, ... where t_{i+1} - t_i != t_{i+2} - t_{i+1}.

    Do you find that this is common (in your experience, I guess)? Based on my vague memory of another post you've made, I guess maybe not, (edit: never mind, now I read another post, and maybe so) but:

    If so, and you wanted interpolation to fill in data, how would you want it to happen: exactly X samples between each original data point? Enough points between each original data point such that the difference between successive times is bounded above? That, with the stipulation that the query errors if the distance between two successive time points is above some stipulated value (as a safety valve if one of the datums has a garbage time value)? (Also, would you want some kind of downsample operation that takes a sample every u seconds, say, the first sample after t_0 + k*u for k = 0,1,2,...?)

  10. hardkrash commented on Jul 12, 2014

    @hardkrash

    TLDR

    • Most of data logging is done at fixed sampling rates, but often there are better ways depending on signal type.
    • Storing data is where you pull out the tricks to save space and bring out details.
    • Getting data out of the database is different then how the data is stored and manipulated internally during queries.
    • Interpolation should be optional and configurable.
    • Interpolation should make queries more effective.
    • Advanced stuff should be left to the client.

    /TLDR

    I think your asking me about varying sample rates. Where some data may be collected with varying time between samples.

    There are some techniques where this method works great. Imagine a slowly varying signal you could in the data collection phase monitor it once a second. It might not move more that one value quanta per few seconds/minutes/hours. You are better off recording that it took t amount of time to move to a new value.

    There are other techniques that solve a different problem, sporadic events. Save a low data rate signal for most of the time from a high speed data converter. Then when an interesting event comes around save it in full resolution for the duration of the interesting event. you have the background trend, but also that interesting tidbit of time when there was a spike.

    A desirable time series interpolation performed in a query would be like the following scenario.

    I'm collecting the temperature of my pool every hour on the hour, but I want to know what temperature it was at when I was swimming. So my application queries for when I was swimming over the past month. We have data samples that are before and after a swim time. So, what was the temperature for my swim. A linear interpolation would say average the before and after data point. Then that linear interpolated data point is joined with the swim event data Then it returns what it expects the swimming pool temperature to be and date time of when I was swimming over the last month.

    Other interpolations exist to make a better guess. A curve fit of a few data points before and after would be another option.

    There are various ways to cope with the swimming pool temperature time series data log, both in a lossy and non lossy methods.

    Lets look at this from how we could make it better. First remove the once an hour sampling rate, I bet that it is not recording interesting data. Let's make some rules for storage. Run length encoding can help but recording the time and new value after some delta change in temperature. My thermometer has a resolution of 0.5°C and a accuracy of 2°C.

    Store it with a value resolution of 1°C we might be able to get away with a 2°C resolution later. (old data could be downgraded.) Then record the time when the value would change to a new value. Be sure to keep a hysteresis in play to prevent storing many irrelevant perturbations around the change point. then look at the effective resolution of time. It might be that having 1 second resolution vs 1 minute resolution is no true concern. i.e. The pool can't raise a single degree in less than that one minute.

  11. hardkrash commented on Jul 12, 2014

    @hardkrash

    (Also, would you want some kind of downsample operation that takes a sample every u seconds, say, the first sample after t_0 + k*u for k = 0,1,2,...?)

    That kinda interesting resample to a seconds (time). I could see a use for some folks. Take the once a second temperature of my house and give the temperature every hour.

    With time series try NOT to think of time in only integer seconds, it can lead to some odd decisions and missed opportunities. Try to think of them as either a frequency (10 samples per second) or as an integer multiple of a time interval.

    Think of time series as one of two general classes.

    • Samples at a fixed sampling rate.
    • Samples recorded with a specific time.

    Missing values are an interesting issue, error out or interpolate. Not sure, it depends on the scenario.

  12. thelinuxlich commented on Oct 1, 2014

    @thelinuxlich

    +1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions