PQ - Function.InvokeAfter() - real delay - powerbi

Trying to calculate real delay between InvokeAfter-function's executions.
Function is supposed to fire five times a second
index delay now
0 0 18:47:33
1 0 18:47:33
2 0 18:47:33
3 0 18:47:33
4 0 18:47:33
5 1 18:47:34
6 1 18:47:34
7 1 18:47:34
8 1 18:47:34
9 1 18:47:34
10 2 18:47:35
11 2 18:47:35
12 2 18:47:35
13 2 18:47:35
14 2 18:47:35
...
But I get this
Column real_delay is a difference between this row and previous
CODE
let
t = Table.FromList({0..19}, Splitter.SplitByNothing()),
delay = Table.AddColumn(t, "delay", each Number.IntegerDivide([Column1], 5)),
InvokeAfter = Table.AddColumn(delay, "InvokeTimeNow", each Function.InvokeAfter(
()=>DateTime.Time(DateTime.LocalNow()), #duration(0,0,0,[delay]))
),
real_delay = Table.AddColumn(InvokeAfter, "real_delay", each try InvokeAfter{[Column1=[Column1]-1]}[InvokeTimeNow]-[InvokeTimeNow] otherwise "-")
in
real_delay
What's wrong with code? Or maybe with InvokeAfter-function???

5 times a second means you should be waiting (second / 5) = 0.2 fractional seconds each invocation.
If you run this code:
let
t = Table.FromList({0..19}, Splitter.SplitByNothing()),
delay = Table.AddColumn(t, "delay", each 0.2),
InvokeAfter = Table.AddColumn(delay, "InvokeTimeNow", each Function.InvokeAfter(
()=>DateTime.Time(DateTime.LocalNow()), #duration(0,0,0,[delay]))
),
real_delay = Table.AddColumn(InvokeAfter, "real_delay", each try InvokeAfter{[Column1=[Column1]-1]}[InvokeTimeNow]-[InvokeTimeNow] otherwise "-")
in
real_delay
you'll see the function was invoked about 5 times per second.

== SOLUTION ==
Here is my own solution. A surprising one, I think...
NEW CODE
let
threads=5,
t = Table.FromList({0..19}, Splitter.SplitByNothing()),
delay = Table.AddColumn(t, "delay", each if Number.Mod([Column1], threads)=0 and [Column1]>0 then 1 else 0),
InvokeAfter = Table.AddColumn(delay, "InvokeTimeNow", each Function.InvokeAfter(()=>DateTime.Time(DateTime.LocalNow()), #duration(0,0,0,[delay]))),
real_delay = Table.AddColumn(InvokeAfter, "real_delay", each try InvokeAfter{[Column1=[Column1]-1]}[InvokeTimeNow]-[InvokeTimeNow] otherwise "-")
in
real_delay
The original idea was the multithreading parsing. And since there were some limits for simultaneous connections, I had to adapt.
I thought there is a "null-zero-start" moment, after which function is invoked - the moment, when cell is calculated (all cells almost at the same time). And second parameter means a delay after this start point. But it appears to accumulate all the delays. Very strange behaviour, imho...
So I solved the problem, but still do not understand why =)

Related

Circular traverse of parameter values algorithm [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.
Want to improve this question? Update the question so it focuses on one problem only by editing this post.
Closed 8 days ago.
Improve this question
Could you pls. help with algorithm (I use C++) which seemed so simple at first glance.
I need a total enumeration of all combinations of N parameters to run some target function for all combination and choose one optimal combination.
Let say there are three parameters (it can be any number set by user but for this example let’s assume 3)
Possible values (can be any number for any parameter - NOT a fixed number - number of values set a user before program starts)
name
value
value
value
value
param1
1
2
..
param2
10
20
..
param3
100
200
300
..
So the number of combinations = 12 (2 * 2 * 3 = 12)
All combinations
#
param1
param2
param3
1
1
10
100
2
2
10
100
3
1
20
100
4
2
20
100
5
1
10
200
6
2
10
200
7
1
20
200
8
2
20
200
9
1
10
300
10
2
10
300
11
1
20
300
12
2
20
300
Ok – let say the order may be different
#
param1
param2
param3
1
1
10
100
2
1
20
100
3
1
10
200
4
1
20
200
5
1
10
300
6
1
20
300
7
2
10
100
8
2
20
100
9
2
10
200
10
2
20
200
11
2
10
300
12
2
20
300
However it’s obviously one counter should change while others fixed and after one counter finish increment next in the chain
This approach seems quite simple but I still can’t find an implementation. I thought use a list for the parameters and as one counter finish itereates it's values it call the next parameter and increment next parameter value and reset the first parameter counter. But how put it in couple of loops … ? I intuitively feels it should be quite simple solution.
The another approach I think – use all combinations to build a graph and after that traverse the whole graph and in the end get an optimal combination. But if I fill the graph it means I already solve this problem and building a graph just a waste of time and memory.
For now there is a sketch (in pseudo-code) like this:
std::list<param> params
bool isDone = false
func(node* n)
{
if(n->prev)
{
n->GetCurrentValue() // return current value and increment by one
n->prev->reset();
func(n->prev)
Run(); // target function
if(n->IsDone()) // finish end of the values
{
if(n->next)
func(n->next);
else
isDone = true;
}
}
else // first node in the list
{
while(n->IsDone()) // finish end of the values
{
n->GetCurrentValue() // return current value and increment by one
Run() // target function
}
n.reset() // set internal counter for the node to 0
func(n->next())
}
}
while(!isDone)
{
for(p : params)
{
func(p)
}
}

Can we really avoid extra space when all the values are non-negative?

This question is a follow-up of another one I had asked quite a while ago:
We have been given an array of integers and another number k and we need to find the total number of continuous subarrays whose sum equals to k. For e.g., for the input: [1,1,1] and k=2, the expected output is 2.
In the accepted answer, #talex says:
PS: BTW if all values are non-negative there is better algorithm. it doesn't require extra memory.
While I didn't think much about it then, I am curious about it now. IMHO, we will require extra memory. In the event that all the input values are non-negative, our running (prefix) sum will go on increasing, and as such, sure, we don't need an unordered_map to store the frequency of a particular sum. But, we will still need extra memory (perhaps an unordered_set) to store the running (prefix) sums that we get along the way. This obviously contradicts what #talex said.
Could someone please confirm if we absolutely do need extra memory or if it could be avoided?
Thanks!
Let's start with a slightly simpler problem: all values are positive (no zeros). In this case the sub arrays can overlap, but they cannot contain one another.
I.e.: arr = 2 1 5 1 1 5 1 2, Sum = 8
2 1 5 1 1 5 1 2
|---|
|-----|
|-----|
|---|
But this situation can never occur:
* * * * * * *
|-------|
|---|
With this in mind there is algorithm that doesn't require extra space (well.. O(1) space) and has O(n) time complexity. The ideea is to have left and right indexes indicating the current sequence and the sum of the current sequence.
if the sum is k increment the counter, advance left and right
if the sum is less than k then advance right
else advance left
Now if there are zeros the intervals can contain one another, but only if the zeros are on the margins of the interval.
To adapt to non-negative numbers:
Do as above, except:
skip zeros when advancing left
if sum is k:
count consecutive zeros to the right of right, lets say zeroes_right_count
count consecutive zeros to the left of left. lets say zeroes_left_count
instead of incrementing the count as before, increase the counter by: (zeroes_left_count + 1) * (zeroes_right_count + 1)
Example:
... 7 0 0 5 1 2 0 0 0 9 ...
^ ^
left right
Here we have 2 zeroes to the left and 3 zeros to the right. This makes (2 + 1) * (3 + 1) = 12 sequences with sum 8 here:
5 1 2
5 1 2 0
5 1 2 0 0
5 1 2 0 0 0
0 5 1 2
0 5 1 2 0
0 5 1 2 0 0
0 5 1 2 0 0 0
0 0 5 1 2
0 0 5 1 2 0
0 0 5 1 2 0 0
0 0 5 1 2 0 0 0
I think this algorithm would work, using O(1) space.
We maintain two pointers to the beginning and end of the current subsequence, as well as the sum of the current subsequence. Initially, both pointers point to array[0], and the sum is obviously set to array[0].
Advance the end pointer (thus extending the subsequence to the right), and increase the sum by the value it points to, until that sum exceeds k. Then advance the start pointer (thus shrinking the subsequence from the left), and decrease the sum, until that sum gets below k. Keep doing this until the end pointer reaches the end of the array. Keep track of the number of times the sum was exactly k.

What will be the output of the following pseudo code for input 7?

Please help me to understand the following code and what will be the possiable output.
What will be the output of the following pseudo code for input 7?
1.Input n
2.Set m = 1, T = 0
3.if (m > n)
Go to step 9
5.else
T = T + m
m = m + 1
8.Go to step 3
9.Print T
0
n is less than n so go to step 9 which is print T which is equal to 0 as set in step 2.
T should be 28. It will loop till m>7 (since n=7) and in each iteration T adds m to itself, since T is 0 initially it is only summing up m after incrementing it by 1 in each iteration.So if you add 1+2+3.....+7 you get 28 and that is when the loop breaks since m is now equal to 8.
for m = 1 2 3 4 5 6 7 and for 8 m>n will be true and it will go to step 9
T=(T+M)= 1 3 6 10 15 21 28 basically T is a series where next is added as 2,3,4,5,6,7 to prev number 2 3 4 5 6 7 if one look from other angle

shifting with re-sampling in time series data

assume that i have this time-series data:
A B
timestamp
1 1 2
2 1 2
3 1 1
4 0 1
5 1 0
6 0 1
7 1 0
8 1 1
i am looking for a re-sample value that would give me specific count of occurrences at least for some frequency
if I would use re sample for the data from 1 to 8 with 2S, i will get different maximum if i would start from 2 to 8 for the same window size (2S)
ds = series.resample( str(tries) +'S').sum()
for shift in range(1,100):
tries = 1
series = pd.read_csv("file.csv",index_col='timestamp') [shift:]
ds = series.resample( str(tries) +'S').sum()
while ( (ds.A.max + ds.B.max < 4) & (tries < len(ds))):
ds = series.resample( str(tries) +'S').sum()
tries = tries + 1
#other lines
i am looking for performance improvement as it takes prohibitively long to finish for large data

Formula that uses previous value

In Stata I want to have a variable calculated by a formula, which includes multiplying by the previous value, within blocks defined by a variable ID. I tried using a lag but that did not work for me.
In the formula below the Y-1 is intended to signify the value above (the lag).
gen Y = 0
replace Y = 1 if count == 1
sort ID
by ID: replace Y = (1+X)*Y-1 if count != 1
X Y count ID
. 1 1 1
2 3 2 1
1 6 3 1
3 24 4 1
2 72 5 1
. 1 1 2
1 2 2 2
7 16 3 2
Your code can be made a little more concise. Here's how:
input X count ID
. 1 1
2 2 1
1 3 1
3 4 1
2 5 1
. 1 2
1 2 2
7 3 2
end
gen Y = count == 1
bysort ID (count) : replace Y = (1 + X) * Y[_n-1] if count > 1
The creation of a dummy (indicator) variable can exploit the fact that true or false expressions are evaluated as 1 or 0.
Sorting before by and the subsequent by command can be condensed into one. Note that I spelled out that within blocks of ID, count should remain sorted.
This is really a comment, not another answer, but it would be less clear if presented as such.
Y-1, the lag in the formula would be translated as seen in the below.
gen Y = 0
replace Y = 1 if count == 1
sort ID
by ID: replace Y = (1+X)*Y[_n-1] if count != 1