Tuesday, 1 December 2020

How to build a hybrid_method that counts the number of records from the last X days?

I have a table of tennis matches and I would like to create a query field that counts the number of previous matches within a date range of the date of the focal match. For example:

+----------+------------+---------------------------+
| match_id |    date    | num_matches within 3 days |
+----------+------------+---------------------------+
|        1 | 01/01/2020 |                         0 |
|        2 | 02/01/2020 |                         1 |
|        3 | 03/01/2020 |                         2 |
|        4 | 05/01/2020 |                         2 |
|        5 | 05/01/2020 |                         3 |
|        6 | 10/01/2020 |                         0 |
+----------+------------+---------------------------+

I've tried using a correlated sub-query as set out here but I can't figure out how to create the conditions:

@hybrid_method
def match_count(self, timespan_days):
    cut_off = self.date_time_inferred - timedelta(days=timespan_days)
    return sum(x >= cut_off and <some_cond_for_less_than_current_match_date> for x in self.date_time_inferred)

@match_count.expression
def match_count(cls, timespan_days):
    cut_off = cls.date_time_inferred - timedelta(days=timespan_days)
    return (
        select(func.count(cls.date_time_inferred)).
        where(
            and_(
                cls.date_time_inferred.__ge__(cut_off),
                cls.date_time_inferred.__lt__(<not_sure_what_goes_here>),
            )
        ).label('match_count')
    )

In trialling simpler forms of the above I also kept getting the following error:

sqlalchemy.exc.ArgumentError: columns argument to select() must be a Python list or other iterable

On the code:

select(func.count(cls.date))

So I'm clearly doing a whole heap of stuff wrong.

Hopefully someone can help me understand how to fix this? Also very open to different ways to accomplish the same thing. I would like to stick with a hybrid attribute though...



from How to build a hybrid_method that counts the number of records from the last X days?

Are Android system widgets within app configurable?

When I press one of the volume hardware buttons, Android's system Android Volume is shown a depicted. Naturally this also happens when I press whithin my app.

Is it possible to configure the style of these Android system stuff like volume? Or at least when I open these system stuff in my app?



from Are Android system widgets within app configurable?

Randomly selected file take longer to load with numpy.load than sequential ones

Context

While training a neural network I realised the time spent per batch increased when I increased the size of my dataset (without changing the batch size). The important part is, I need to fetch 20 .npy files per data point, this number doesn't depend on the dataset size.

Problem

Training goes from 2s/iteration to 10s/iteration... There is no apparent reason why training would take longer. However, I managed to track down the bottleneck. It seems to have to do with the loading of the .npy files.


To reproduce this behavior, here's a small script you can run to generate 10,000 dummy .npy files:

def path(i):
    return os.sep.join(('../datasets/test', str(i)))

def create_dummy_files(N=10000):
    for i in range(N):
        x = np.random.random((100, 100))
        np.save(path(random.getrandbits(128)), x)

Then you can run the following two scripts and compare them yourself:

  • The first script where 20 .npy files are randomly selected and loaded:

    L = os.listdir('../datasets/test')
    S = random.sample(L, 20)
    for s in S:
        np.load(path(s)) # <- timed this
    
  • The second version where 20 .npy 'sequential' files are selected and loaded.

    L = os.listdir('../datasets/test')
    i = 100
    S = L[i: i + 20]
    for s in S:
        np.load(path(s)) # <- timed this
    

I tested both scripts and ran them 100 times each (in the 2nd script I used the iteration count as the value for i so the same files are not loaded twice). I wrapped the np.load(path(s)) line with time.time() calls. I'm not timing the sampling, only the loading. Here are the results:

  • Random loads (times roughly stay between 0.1s and 0.4s, average is 0.25s):

    enter image description here

  • Non random loads (times roughly stay between 0.010s and 0.014s, average is 0.01s):

    enter image description here


I'm assuming those times are related to the CPU's activity when the scripts are loaded. However it doesn't explain this gap. Why are these two results so different? Is there something to do with the way files are indexed?

Edit: I printed S in the random sample script, copied the list of 20 filenames then ran it again with S as an list literally defined. The time it took is comparable to the 'sequential' script. Which means it's not related to files not being sequential in the fs or anything. It seems the random sampling gets counted in the timer, yet timing is defined as:

t = time.time()
np.load(path(s))
print(time.time() - t)

I tried as well wrapping np.load (exclusively) with cProfile: same result.



from Randomly selected file take longer to load with numpy.load than sequential ones