#๐Ÿ”’ python microsecond problem

101 messages ยท Page 1 of 1 (latest)

mellow sierraBOT
#

@white musk

Python help channel opened

Remember to:

  • Ask your Python question, not if you can ask or if there's an expert who can help.
  • Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
  • Explain what you expect to happen and what actually happens.

:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.

white musk
#

my program I shared a part of the program with you below. The program generally takes the local time, local seconds and local milliseconds and matches them with the specified time, seconds and milliseconds and runs the execute_action function in real time. However, when the millisecond match is added to the program as follows, the program cannot make this match. What is the reason? How can I solve it?

dark sable
#

bro i gave u a working solution yesterday xD

white musk
#

I did not understand your suggestion and could not apply it. I had to recreate the channel because it was closed.

dark sable
#

!e

from datetime import datetime
import numpy as np

def generate_times(current_time, second_increments, microsecond_increments):
    times = []
    
    for second_increment in second_increments:
        for microsecond_increment in microsecond_increments:
            new_time = current_time.replace(second=second_increment % 60, microsecond=microsecond_increment)
            times.append(new_time)
    
    return times

executed_times = {}
ct = datetime.now()
tt = generate_times(ct, [0, 10, 20, 30, 40, 50], [0, 500])
np_tt = np.array(tt).astype(np.datetime64)
print(len(np_tt))
mask = np_tt > np.datetime64(ct)
np_tt = np_tt[mask]
print(len(np_tt))

while len(executed_times.keys()) != len(np_tt):
    ct = datetime.now()
    ct = ct.replace(microsecond=ct.microsecond // 1000)
    # if ct.microsecond % 100 == 0: # maybe check if u can pre-sample to reduce resources
    np_ct = np.array(ct).astype(np.datetime64)
    if np.array(np_ct).astype(np.datetime64) in np_tt:
        idx = int(np.where([np_ct == np_tt])[-1][0])
        if idx in executed_times.keys():
            continue
        else:
            executed_times[idx] = True

mellow sierraBOT
dark sable
#

if u got questions just ask

white musk
#

This code is too complicated for a beginner like me. I don't know if I can convert it from the code I sent to this.

dark sable
#

but thats how ull improve, also ppl will mostly not stick with ur code and fix faulty/poor code (no offense)

#

so basically i took ur code and generalized some things, also i used numpy as its superior in speed to ur previous comparision, as u stated performance problems

white musk
#

I'm trying to convert the code

dark sable
#

try to understand it first and not just copy/paste

white musk
#

What exactly does the code you posted do?

#

What does your outputs mean?

#

it can be milisecond btw

#

datetime just have microsecond parameters so I had to use microsecond

#

Can you check the code I posted to see if it's a correct implementation?

dark sable
#

check executed_times dict

#

the output is just the trget_time array len

#

will vary when u start the script

snow obsidian
#

ct = ct.replace(microsecond=ct.microsecond // 1000) is wrong

#

I believe

#

!e ```py
from datetime import datetime

print(datetime.now().microsecond)

mellow sierraBOT
snow obsidian
#

hm, no, never mind

#

lookin gat wrong code

dark sable
#

?

#

he was using // 1000 in an earlier version to get more matches i assume

#

but with this np version u can get really low on the ms sample rate

snow obsidian
#

!e ```py

import numpy as np

base_time = np.datetime64('2024-01-01T00:00:00.000')
target_times = np.array([
base_time + np.timedelta64(m * 60 * 1000 + s * 1000 + ms, 'ms') for m, s, ms in [
(2, 15, 0), (2, 20, 0), (2, 25, 0), (0, 0, 0), (6, 53, 0), (7, 5, 0), (7, 11, 0), (7, 23, 0),
(8, 23, 0), (8, 35, 0), (9, 5, 0), (9, 11, 0), (9, 41, 0), (9, 53, 0), (10, 5, 0),
(10, 41, 0), (10, 53, 0), (11, 35, 0), (11, 53, 0), (12, 23, 0), (12, 35, 0),
(13, 5, 0), (13, 11, 0), (13, 23, 0), (13, 35, 0), (13, 41, 0),
(14, 23, 0), (14, 35, 0), (14, 41, 0), (15, 53, 0), (16, 5, 0), (16, 11, 0), (17, 5, 0), (17, 23, 0),
(18, 41, 0), (18, 53, 0), (19, 5, 0), (19, 11, 0), (19, 53, 0), (20, 5, 0), (20, 11, 0),
(21, 11, 0), (21, 23, 0), (21, 41, 0), (22, 23, 0), (22, 35, 0), (22, 41, 0),
(23, 41, 0), (23, 53, 0), (24, 23, 0), (24, 35, 0), (24, 41, 0)
]])
print(target_times)

dark sable
#

why would u run this list of tuples instead of a function for it Q_Q

mellow sierraBOT
# snow obsidian !e ```py import numpy as np base_time = np.datetime64('2024-01-01T00:00:00.00...

:white_check_mark: Your 3.12 eval job has completed with return code 0.

001 | ['2024-01-01T00:02:15.000' '2024-01-01T00:02:20.000'
002 |  '2024-01-01T00:02:25.000' '2024-01-01T00:00:00.000'
003 |  '2024-01-01T00:06:53.000' '2024-01-01T00:07:05.000'
004 |  '2024-01-01T00:07:11.000' '2024-01-01T00:07:23.000'
005 |  '2024-01-01T00:08:23.000' '2024-01-01T00:08:35.000'
006 |  '2024-01-01T00:09:05.000' '2024-01-01T00:09:11.000'
007 |  '2024-01-01T00:09:41.000' '2024-01-01T00:09:53.000'
008 |  '2024-01-01T00:10:05.000' '2024-01-01T00:10:41.000'
009 |  '2024-01-01T00:10:53.000' '2024-01-01T00:11:35.000'
010 |  '2024-01-01T00:11:53.000' '2024-01-01T00:12:23.000'
... (truncated - too many lines)

Full output: https://paste.pythondiscord.com/PJLYXENXZABUYXD4AAKLMWSYXA

snow obsidian
#

I'm copy pasting from the pastebin?

#

the parens are broken in it apparently

white musk
#

i know

#

I noticed but it's late, sorry

dark sable
#

the pastebin code is slow and inefficient also the if statements are unnecessary

snow obsidian
#

!e ```py
import numpy as np

a = np.datetime64('2024-01-01T00:00:00.000')
b = np.datetime64('2024-01-01T00:00:00.250')
print(np.isclose(a, b, atol=np.timedelta64(500, 'ms')))

mellow sierraBOT
# snow obsidian !e ```py import numpy as np a = np.datetime64('2024-01-01T00:00:00.000') b = np...

:x: Your 3.12 eval job has completed with return code 1.

001 | Traceback (most recent call last):
002 |   File "/home/main.py", line 5, in <module>
003 |     print(np.isclose(a, b, atol=np.timedelta64(500, 'ms')))
004 |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
005 |   File "/snekbox/user_base/lib/python3.12/site-packages/numpy/core/numeric.py", line 2345, in isclose
006 |     dt = multiarray.result_type(y, 1.)
007 |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
008 | numpy.exceptions.DTypePromotionError: The DType <class 'numpy._FloatAbstractDType'> could not be promoted by <class 'numpy.dtypes.DateTime64DType'>. This means that no common DType exists for the given inputs. For example they cannot be stored in a single array unless the dtype is `object`. The full list of DTypes is: (<class 'numpy.dtypes.DateTime64DType'>, <class 'numpy._FloatAbstractDType'>)
snow obsidian
#

what

#

um

#

does anyone see what I fucked up?

#

!e ```py
import numpy as np

a = np.datetime64('2024-01-01T00:00:00.000')
b = np.datetime64('2024-01-01T00:00:00.250')
atol = np.timedelta64(500, 'ms')

print(a, b, atol)

print(np.isclose(a, b, atol=atol))

mellow sierraBOT
# snow obsidian !e ```py import numpy as np a = np.datetime64('2024-01-01T00:00:00.000') b = np...

:x: Your 3.12 eval job has completed with return code 1.

001 | 2024-01-01T00:00:00.000 2024-01-01T00:00:00.250 500 milliseconds
002 | Traceback (most recent call last):
003 |   File "/home/main.py", line 9, in <module>
004 |     print(np.isclose(a, b, atol=atol))
005 |           ^^^^^^^^^^^^^^^^^^^^^^^^^^^
006 |   File "/snekbox/user_base/lib/python3.12/site-packages/numpy/core/numeric.py", line 2345, in isclose
007 |     dt = multiarray.result_type(y, 1.)
008 |          ^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
009 | numpy.exceptions.DTypePromotionError: The DType <class 'numpy._FloatAbstractDType'> could not be promoted by <class 'numpy.dtypes.DateTime64DType'>. This means that no common DType exists for the given inputs. For example they cannot be stored in a single array unless the dtype is `object`. The full list of DTypes is: (<class 'numpy.dtypes.DateTime64DType'>, <class 'numpy._FloatAbstractDType'>)
white musk
#

I think I can't do this conversion as it is too advanced

snow obsidian
#

I think youre mistake is using isclose

#

Dont poll the current time and check if it's close enough

white musk
#

i tried this

snow obsidian
#

sort your times

#

then wait till the next measure time

white musk
#

I tried rounding but the program continued to ignore those parameters.

dark sable
#

@snow obsidian is he even using .isclose?

white musk
#

At the beginning of the program, for example, I rounded the received microseconds 345345 to 300000, but the result still did not change.

snow obsidian
dark sable
#

.isclose uses arrays i assume thats why it wont work

snow obsidian
#

It works on just numbers:

#

!e ```py
import numpy as np

print(np.isclose(3, 5, atol=3))

mellow sierraBOT
snow obsidian
#

I still don't like it

#

the solution is wrong

dark sable
#

uh didnt see the new pastebin thanks

snow obsidian
#

You're losing your measure

#

Sort your samples by time

#

so you can measure them in order

#

then wait until you can take a sample. Take the sample and grab the current time. Store both (as a time + your data shared coordinate) as a complete sample. Then, wait the remaining time till the next sample and repeat

dark sable
#

@white musk why now use .isclose and not the .where?

snow obsidian
#

if you are worried about jittery, cap how long you wait or wait until a second before, then wait a shorter time

white musk
#

like this

#

guys i know you are right but i am beginner

#

i dont understand all things

dark sable
#

try not to use chatgpt

#

try to ask better questions

#

we here to help u got 2 pleps which are willing to help u

white musk
#

Let's forget all this talk and focus on this code, shall we?

dark sable
#

sure

#

start line by line of my suggested code

white musk
#

the outputs what does it means

dark sable
#

and ask me what u dont get

#
def generate_times(current_time, second_increments, microsecond_increments):
    times = []
    
    for second_increment in second_increments:
        for microsecond_increment in microsecond_increments:
            new_time = current_time.replace(second=second_increment % 60, microsecond=microsecond_increment)
            times.append(new_time)
    
    return times

executed_times = {}
ct = datetime.now()
tt = generate_times(ct, [0, 10, 20, 30, 40, 50], [0, 500])

instead of using ur list of tuples with manual inputs and missing timestamps i create the function to generate a times_list with given increments for seconds and microseconds.
the returned list is then "tt"

#

if u return tt ull see that [0, 10, 20, 30, 40, 50] are the seconds for the current_time (ct) and [0, 500] the ms making it 6x2 values -> len(tt) = 12

#

as we can start the script at a random time it can happen that we wont be able to sample all times [0] sec for example is "impossible" in this script as even when i start at xx:xx:00 until the first operations are finished it wont be xx:xx:00 anymore

#

so when ran at xx:xx:01 ill get a tt list of len() -> 10

#

got that?

white musk
#

I understood what you did, though not all of it.

dark sable
#

simply put i reduce the tt list size to only sample times in the future so my while loop will end and not run 24h to match

white musk
#

I understand but it is still very complicated and it seems very difficult to adapt it to the program I want to write.

#

Thanks for everything though

dark sable
#

its not

#

u can adjust tt to ur needs

#

ur target_times was a list of datetime obj in which u replaced min, sec and ms

#

u can take that vary list and use it in my script

white musk
#

i'll try

#

thank you

dark sable
#

note u need to do np.array(target_times).astype(np.datetime64)

white musk
#

I will pay attention, thanks

mellow sierraBOT
#
Python help channel closed

This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.