window

Calculating Moving (Average) ++

If you have time series data and want to calculate Moving-function like Moving Average you can use a Rolling window like shown below … enjoy

import numpy as np

def rolling_window(a, window):
    shape = a.shape[:-1] + (a.shape[-1] - window + 1, window)
    strides = a.strides + (a.strides[-1],)
    return np.lib.stride_tricks.as_strided(a, shape=shape, strides=strides)

a = np.arange(0,10)

print(rolling_window(a,5))

print(np.mean(rolling_window(a,5), axis=1))

-----
[[0 1 2 3 4]
 [1 2 3 4 5]
 [2 3 4 5 6]
 [3 4 5 6 7]
 [4 5 6 7 8]
 [5 6 7 8 9]]

[2. 3. 4. 5. 6. 7.]


b = np.random.randint(0,100,10)

print(rolling_window(b,5))

print(np.mean(rolling_window(b,5), axis=1))

-----
[[42 93 30 69 53]
 [93 30 69 53 93]
 [30 69 53 93 61]
 [69 53 93 61 22]
 [53 93 61 22 53]
 [93 61 22 53 71]]

[57.4 67.6 61.2 59.6 56.4 60. ]

or here is another way to do moving average :

import numpy as np

def moving_average(a, n=3) :
    ret = np.cumsum(a, dtype=float)
    ret[n:] = ret[n:] - ret[:-n]
    return ret[n - 1:] / n

print(moving_average([1,2,3,4,5,4,3,2,1]))

-------

[2.0 3.0 4.0 4.33 4.0 3.0 2.0]

Create Popup window

Here is how to create a POPUP window in WordPress with a content of your choosing.
For the popup we need a Page w/o a theme, so let’s do this :

  • Install the “Blank Slate” plugin.
  • Create New Page
    • Leave the Title empty
    • Set in Page property Template=”Blank Slate”
    • Add whatever content you want
    • Because there is no Title, we need set a Permalink so that it is human readable, let say : your-site/blah
    • Publish the page
  • Go to the page you want to have the popup window show up when you click a link
  • Add the following code :
<a href='' onclick='window.open("https://your-site/blah/","window-name","width=400,height=320");' target='blank'>Popup window</a>

If you have an original element to hide use this, so that it looks like the element pop up from the page :

<a href='' onclick='window.open("https://your-site/blah/","window-name","width=400,height=320");document.getElementById("orig-blah").style.display="none";' target='blank'>Popup window</a>

Here is it in action : [Learn Python]