How to create a binary window for an event in Python - pandas

I have some sensor data and I know when the main event happens, but there's some noise before and after this event, and so I want to create a binary window where I can then just through away all the non important information.
Here is a toy example.
eg=pd.DataFrame({"Seconds":[0,0.5,1,1.5,2,2.5,3,3.5,4,4.5,5,5.5],
"Event":[0,0,1,0,0,0,0,1,0,0,0,1],
"Start":[0,1,0,0,0,0,1,0,0,0,1,0],
"End": [0,0,0,1,0,0,0,0,1,0,0,0],
"Want": [0,1,1,1,0,0,1,1,1,0,1,1]})
I have generated my start and end with shifting. What I cannot seem to create in Python is my Want. The start and the end are more than 1 row away from the event, albeit they are fixed intervals.
In excel I am able to create a 3 nested that effectively does the following:
fup_bottom["Want"]=np.where(fup_bottom.End==1,0,
np.where(fup_bottom.Start==1,1,
np.where(fup_bottom.Want.shift(1)==1,1,0)))
In python this doesn't work because the "Want" doesn't exist yet.
I have tried recording the seconds where Start is 1 and forward filling, and then similarly where End is one and backward filling. However, this leads to Want always being 1 after the first event.
I greatly appreciate any help on this. I've been trying to figure this out for a couple of hours and am stuck.
J

Related

Elm: avoiding a Maybe check each time

I am building a work-logging app which starts by showing a list of projects that I can select, and then when one is selected you get a collection of other buttons, to log data related to that selected project.
I decided to have a selected_project : Maybe Int in my model (projects are keyed off an integer id), which gets filled with Just 2 if you select project 2, for example.
The buttons that appear when a project is selected send messages like AddMinutes 10 (i.e. log 10 minutes of work to the selected project).
Obviously the update function will receive one of these types of messages only if a project has been selected but I still have to keep checking that selected_project is a Just p.
Is there any way to avoid this?
One idea I had was to have the buttons send a message which contains the project id, such as AddMinutes 2 10 (i.e. log 10 minutes of work to project 2). To some extent this works, but I now get a duplication -- the Just 2 in the model.selected_project and the AddMinutes 2 ... message that the button emits.
Update
As Simon notes, the repeated check that model.selected_project is a Just p has its upside: the model stays relatively more decoupled from the UI. For example, there might be other UI ways to update the projects and you might not need to have first selected a project.
To avoid having to check the Maybe each time you need a function which puts you into a context wherein the value "wrapped" by the Maybe is available. That function is Maybe.map.
In your case, to handle the AddMinutes Int message you can simply call: Maybe.map (functionWhichAddsMinutes minutes) model.selected_project.
Clearly, there's a little bit more to it since you have to produce a model, but the point is you can use Maybe.map to perform an operation if the value is available in the Maybe. And to handle the Maybe.Nothing case, you can use Maybe.withDefault.
At the end of the day is this any better than using a case expression? Maybe, maybe not (pun intended).
Personally, I have used the technique of providing the ID along with the message and I was satisfied with the result.

Pinescript tradingview example needed

I am a novice at using TradingView's Pinescript and having a hard time finding an easy to understand example of a script. I am used to Java/C++ and Pinescript is very different. I am trying to build a script that will scan a stock chart and look for gaps of over 5%. Here is psuedocode for what I am trying to create:
if(difference between open of current day and previous day close > 5%) {
plot a green circle or red circle, depending on if gap was up or down
}
Thank you in advance!
You're best bet would be to go through their tutorial
There's some odds choices in this language if you have any programming background so it's probably a good idea to read it all (it's not that much). E.g.
open is the current bars open price, but open[1] is the previous bar open price (so should be read as open[current_index-1])
you can't use the plot calls inside function bodies
as for you question (not tested, but should be close enough to give the right idea):
study(title='gap detector', overlay=true)
//plotshape(<condition>, <options>) // condition must be true to plot something
is_percentage_increase = if (close-close[1])/close[1] > 0.05
true
plotshape(is_percentage_increase, style=shape.circle, color=green)
Pine scripting is easy to use; Initially it was bit hard to understand, Once started using it it becomes so useful to strategize the logic.
In your case you can use conditional operator as well to detect this.This will work in Version 2 .The version 3 is bit differrent
//version =2
study(title ="Experementing the code ",overlay =true ,shorttitle ="testing") //overlay=false to get this down of the chart as seperate layout
plotchar( (close-close[1])/close[1] >0.05 ? 1:na ,char =' ',text ="plot\nTest",textcolor=red,size.huge)
Instead of if the condition you can use ?: operator to do this job.
Please make sure plotchar(.....) coming in the same line, not in separate line.
Pine has lot of cool features to use and helped me to derive my own strategy. The tutorial is really good.
Note if you don't put char='' above it will print STAR as the default character. And in the character even if you put char='testtest' it will print the only t .

What does In [*] at the upper left-hand of the cell mean when running a jupyter notebook?

What does In [*] at the upper left-hand of the cell mean when running a jupyter notebook and how I can resolve this?
Please any one can help me.
It means that the cell is running, or in queue to be run. You don't solve it really, you just have to wait for the cell to finish executing. If you think the cell has gotten stuck for some reason, you can try to interrupt it by going to "Kernel" in the menu bar and selecting "Interrupt". When the cell is finished running, a number will appear instead of a star. The number is the order in which that cell was executed in this session.
If you have a cell that takes a really long time to execute, I suggest outputting some periodic status to the user. I like to do something like:
update_freq = 100 # or however often you want it to update you
print('Running', end='', flush=True)
for iter in range(a_whole_lot_of_iterations):
# ... code that runs for a long time or for a ton of iterations ...
if iter >= update_freq and iter % summary_freq == 0:
print('.', end='', flush=True) # periodically print something
print('done!')
You can get as fancy as you want with this approach, printing all kinds of cool stuff in your first line as a header, controlling how many updates are printed total to the screen so you can create a sort of ASCII status bar, etc. I found this guide to the Python string formatting language helpful when figuring out how to make nicely formatted status updates.

misunderstanding how the threads work

I have a problem, big problem with threads in vb.net
First of all I want to tell that I didn't work with threads before (just on the school), I read lot of pages about it, but none of them could help me for my problem.
My main question here is understand the logic, and after if is possible, solve the problem that I have, both are related, then I going to explain the problem.
The code hasn't comments and/or documentation related, and is a program developed lot of years ago and the guy that did this is not working on the office, nobody knows how it works :S
I have a list called listOfProccess, and when is only 1 works fine.
in the callback function in QueueUserWorkItem fill the information about p, then execute the thread, I suppose
That list contain an array with information type
listOfProccess[].type = 'a/b/c/d/e/f/g/'
also the list include an ID.
Code:
If listOfProccess.Count > 0 Then
Threading.ThreadPool.SetMinThreads(1, 1)
Threading.ThreadPool.SetMaxThreads(4, 4)
For Each p In listOfProccess
Try
Threading.ThreadPool.QueueUserWorkItem(New Threading.WaitCallback(Object p.function))
Catch e As Exception
sendMail("mail#mail", "alerts#mail.ie", "", e.StackTrace)
End Try
Next
Problems:
I have two problems here:
Sometimes execute an item in the list i.e. 'a' in a infinite loop, and expends all resources of the machine, but if i close and restart again, works, I didn't know if is a problem with the threads or not, sincerely I think that is other thing, because this problem starts two or tree weeks ago, and the program still running during a year.
This one i think is related about threads, if I have two (or more) p on the list like this:
p[1].type = 'a/b/c/d/e/f/g/'
p[1].ID = 1
p[2].type = 'ww/xx/ff/yy/aa/rr/'
p[2].ID =2
when the system execute something like this, the way that it follows is 'random' ie. takes for the first one, a, b,c and after this it does ww, and come back to the first. the problem is bigger if I have more items in the list, like 4 or 5; this is not a very big problem, because the program works, it not works 100% fine, but works, is more to try the understand why it works on this way.
Any help is welcome.
The second problem is a race condition problem, as you can't guarantee the order of the execution for the threads, there is a non-zero probability that your threads will replace each other's values. There are a lot of wayss to solve this issue: algorythms (either lock-oriented and lock-free), synchronization techniques and so on, and there is no silver-bullet solution for this.
First problem is unclear for me, as I can't understand what exactly do you mean by a infinite loop - may be this can happen if you link items to deleted (from other thread) ones and there is no way to go out of your task, as the links in a list are broken. This still should be solved with sychronization.
I think your should start with MSDN articles or some book about multithreading in general, and after that you should split your program step by step for a small parts you understand.
Update:
p.function - about the infinite loop you should consider the code of this delegate. If there is a condition to restart work, you should check a recursion limit. For example, if there is an optimistic locking algorythm, your code can find out that the update it tried isn't valid, and restart it. And if the links are broken, it will never end it's task, and will stay forever in an infinite loop.

Unable to interrupt psychopy script, event.getKeys() always empty

I'm new to psychopy and python. I'm trying to program a way to quit a script (that I didn't write), by pressing a key for example. I've added this to the while loop:
while n < total
start=time.clock()
if len(event.getKeys()) > 0:
break
# Another while loop here that ends when time is past a certain duration after 'start'.
And it's not working, it doesn't register any key presses. So I'm guessing key presses are only registered during specific times. What are those times? What is required to register key presses? That loop is extremely fast, sending signals every few milliseconds, so I can't just add wait commands in the loop.
If I could just have a parallel thread checking for a key press that would be good too, but that sounds complicated to learn.
Thanks!
Edits: The code runs as expected otherwise (in particular no errors). "core" and "event" are included. There aren't any other "event" command of any kind that would affect the "key press log".
Changing the rest of the loop content to something that includes core.wait statements makes it work. So for anybody else having this difficulty, my original guess was correct: key presses are not registered during busy times (i.e. in my case a while statement that constantly checks the time), or possibly only during specific busy times... Perhaps someone with more knowledge can clarify.
....So I'm guessing key presses are only registered during specific
times. What are those times? What is required to register key
presses?....
To try and answer your specific question, the psychopy api functions/methods that cause keyboard events to be registered are ( now updated to be literally every psychopy 1.81 API function to do this):
event.waitKeys()[1]
event.clearEvents()[1]
event.getKeys()[2]
event.Mouse.getPressed()
win.flip()
core.wait()
visual.Window.dispatchAllWindowEvents()
1: These functions also remove all existing keyboard events from the event list. This means that any future call to a function like getKeys() will only return a keyboard event if it occurred after the last time one of these functions was called.
2: If keyList=None, does the same as *, else removes keys from the key event list that are within the keyList kwarg.
Note that one of the times keyboard events are 'dispatched' is in the event.getKeys() call itself. By default, this function also removes any existing key events.
So, without being seeing the full source of the inner loop that you mention, it seems highly likely that the event.getKeys() is never returning a key event because key events are being consumed by some other call within the inner loop. So the chance that an event is in the key list when the outer getKeys() is called is very very low.
Update in response to OP's comment on Jonas' test script ( I do not have enough rep to add comments to answers yet):
... Strange that you say this ..[jonas example code].. works
and from Sol's answer it would seem it shouldn't. – zorgkang
Perhaps my answer is giving the wrong understanding, as it is intended to provide information that shows exactly why Jonas' example should, and does, work. Jonas' example code works because the only time key events are being removed from the event buffer is when getKeys() is called, and any events that are removed are also returned by the function, causing the loop to break.
This is not really an answer. Here's an attempt to minimally reproduce the error. If the window closes on keypress, it's a success. It works for me, so I failed to reproduce it. Does it work for you?
from psychopy import event, visual, core
win = visual.Window()
clock = core.Clock()
while True:
clock.reset()
if event.getKeys():
break
while clock.getTime() < 1:
pass
I don't have the time module installed, so I used psychopy.core.Clock() instead but it shouldn't make a difference, unless your time-code ends up in an infinite loop, thus only running event.getKeys() once after a few microseconds.