Gentoo Forums
Gentoo Forums
Gentoo Forums
Quick Search: in
python bug (non-gentoo) [FIXED]
View unanswered posts
View posts from last 24 hours

 
Reply to topic    Gentoo Forums Forum Index Portage & Programming
View previous topic :: View next topic  
Author Message
dimitrimemet
Tux's lil' helper
Tux's lil' helper


Joined: 22 Jun 2023
Posts: 84

PostPosted: Sun Aug 25, 2024 6:54 pm    Post subject: python bug (non-gentoo) [FIXED] Reply with quote

I do not like to ask non-gentoo questions here even tho i know its okay but i am going NUTZ :oops: I found my kryptonite when it comes to learning coding. So the code works however when it comes to 359.xxxx or 0.xxxx longitude is not finding the date and the time, Mind you it works fine with the rest
Code:
import swisseph as swe
import datetime

def normalize_longitude(longitude):
    """Normalize longitude to the range 0° to 360°."""
    return longitude % 360

def calculate_chiron_return(birth_year, birth_month, birth_day, birth_time):
    """Calculates the precise Chiron return date for the given birth data.

    Args:
        birth_year: Birth year.
        birth_month: Birth month (1-12).
        birth_day: Birth day.
        birth_time: Birth time in decimal hours (e.g., 5.0834 for 5:08:20).

    Returns:
        A tuple containing:
        - A datetime object representing the exact Chiron return date.
        - The longitude of Chiron at the time of birth.
        - The longitude of Chiron at the return date.
    """

    swe.set_ephe_path('/home/ethan/Downloads/swisseph-devel/ephe')

    # Calculate Julian day for birth time
    birth_jd = swe.julday(birth_year, birth_month, birth_day, birth_time)

    # Calculate Chiron's longitude at birth
    flag = swe.FLG_SWIEPH
    birth_chiron_long, _ = swe.calc_ut(birth_jd, swe.CHIRON, flag)
    birth_chiron_long = normalize_longitude(birth_chiron_long[0])  # Normalize the longitude value

    # Estimate Chiron return date with a 5-year window (2 years before and 2 years after)
    chiron_return_year = birth_year + 49  # Approximate Chiron return period

    # Adjust the start and end dates to cover 2 years before and after the estimated return year
    start_date = datetime.datetime(chiron_return_year - 2, 1, 1)
    end_date = datetime.datetime(chiron_return_year + 3, 1, 1)

    tolerance = 0.00001  # Tighter tolerance for degrees

    while (end_date - start_date).total_seconds() > 1:
        mid_date = start_date + (end_date - start_date) / 2
        mid_jd = swe.julday(mid_date.year, mid_date.month, mid_date.day,
                            mid_date.hour + mid_date.minute / 60.0 +
                            mid_date.second / 3600.0 +
                            mid_date.microsecond / 3600000000.0)
        mid_chiron_long, _ = swe.calc_ut(mid_jd, swe.CHIRON, flag)
        mid_chiron_long = normalize_longitude(mid_chiron_long[0])  # Normalize the longitude value

        if abs(birth_chiron_long - mid_chiron_long) < tolerance:
            return mid_date, birth_chiron_long, mid_chiron_long
        elif mid_chiron_long < birth_chiron_long:
            start_date = mid_date
        else:
            end_date = mid_date

    # Final refinement step for exact seconds/milliseconds
    best_date = start_date
    best_diff = abs(birth_chiron_long - mid_chiron_long)
    for millisecond in range(1000):
        try:
            precise_time = start_date + datetime.timedelta(milliseconds=millisecond)
            precise_jd = swe.julday(precise_time.year, precise_time.month, precise_time.day,
                                    precise_time.hour + precise_time.minute / 60.0 +
                                    precise_time.second / 3600.0 +
                                    precise_time.microsecond / 3600000000.0)
            precise_chiron_long, _ = swe.calc_ut(precise_jd, swe.CHIRON, flag)
            precise_chiron_long = normalize_longitude(precise_chiron_long[0])
            diff = abs(birth_chiron_long - precise_chiron_long)
            if diff < best_diff:
                best_diff = diff
                best_date = precise_time
        except ValueError:
            pass  # Handle invalid dates

    return best_date, birth_chiron_long, precise_chiron_long

# Example usage
birth_year = 1969
birth_month = 2
birth_day = 5
birth_time = 9.6166
chiron_return_date, birth_chiron_long, return_chiron_long = calculate_chiron_return(birth_year, birth_month, birth_day, birth_time)
print(f"Chiron return date: {chiron_return_date}")
print(f"Chiron longitude at birth: {birth_chiron_long}")
print(f"Chiron longitude at return: {return_chiron_long}")

this is the output
Code:
/home/ethan/pycodes/venv/bin/python /home/ethan/pycodes/gemini_chiron_return.py
Chiron return date: 2016-01-01 00:00:00
Chiron longitude at birth: 0.2670171204241596
Chiron longitude at return: 251.1426886628303
i tried everything and my head is about to explode , please help me understand what i am doing wrong ..

p.s. if you want to see the code working properly change the month number. It should look like this
Code:
Chiron return date: 2020-02-22 13:17:56.632690
Chiron longitude at birth: 3.5432579782070928
Chiron longitude at return: 3.5432527419255027


Last edited by dimitrimemet on Wed Aug 28, 2024 7:12 pm; edited 1 time in total
Back to top
View user's profile Send private message
halcon
l33t
l33t


Joined: 15 Dec 2019
Posts: 649

PostPosted: Mon Aug 26, 2024 2:57 pm    Post subject: Reply with quote

Hi dimitrimemet,

I code perl, not python, but, as I see the algorithm, I could tell you what I would do. I would try to *bisect* where exactly the wrong value is appearing with *debugging prints* during the loop *for millisecond in range* where, I think, it appears...

EDIT
Or before the loop, wherever. The same bisecting.
_________________
A wife asks her husband, a programmer:
- Could you please go shopping for me and buy one carton of milk, and if they have eggs, get 6?
He comes back with 6 cartons of milk.
- Why did you buy 6 cartons of milk?
- They had eggs.
Back to top
View user's profile Send private message
Hu
Administrator
Administrator


Joined: 06 Mar 2007
Posts: 22608

PostPosted: Mon Aug 26, 2024 3:20 pm    Post subject: Re: python bug (non-gentoo) Reply with quote

dimitrimemet wrote:
Code:
import swisseph as swe
This module does not appear to be part of the Python standard library, so the code as shown is not a Minimal Reproducible Example, because we cannot run it using only the standard Python modules. Therefore, our ability to help you is limited to inspecting the code or deleting blocks of code until it works, and hoping we do not delete the bug in the process. I suggest providing a MRE that demonstrates the bug.
Back to top
View user's profile Send private message
dimitrimemet
Tux's lil' helper
Tux's lil' helper


Joined: 22 Jun 2023
Posts: 84

PostPosted: Mon Aug 26, 2024 4:29 pm    Post subject: Re: python bug (non-gentoo) Reply with quote

Hu wrote:
dimitrimemet wrote:
Code:
import swisseph as swe
This module does not appear to be part of the Python standard library, so the code as shown is not a Minimal Reproducible Example, because we cannot run it using only the standard Python modules. Therefore, our ability to help you is limited to inspecting the code or deleting blocks of code until it works, and hoping we do not delete the bug in the process. I suggest providing a MRE that demonstrates the bug.

Code:
 pip install swisseph
should do the trick and even tho one may need to have the swe.set_ephe_path, the code should work without it just fine( if you get some sort of error from it, its safe to comment it or delete it). Thank you tho for trying to help
p.s. i might've jumped the gun with calling it a bug, instead just trying to figure out why the code does not work when the "longitudes" are 359.xxxx or 0.xxxx since the code does not produce a error , rather just gives out the wrong info and the only way to know its correct is if we have a match with "longitude at birth and longitude at return"


Last edited by dimitrimemet on Mon Aug 26, 2024 4:45 pm; edited 3 times in total
Back to top
View user's profile Send private message
dimitrimemet
Tux's lil' helper
Tux's lil' helper


Joined: 22 Jun 2023
Posts: 84

PostPosted: Mon Aug 26, 2024 4:31 pm    Post subject: Reply with quote

halcon wrote:
Hi dimitrimemet,

I code perl, not python, but, as I see the algorithm, I could tell you what I would do. I would try to *bisect* where exactly the wrong value is appearing with *debugging prints* during the loop *for millisecond in range* where, I think, it appears...

EDIT
Or before the loop, wherever. The same bisecting.

thats exactly what I've been squeezing my brains with :cry:
Back to top
View user's profile Send private message
halcon
l33t
l33t


Joined: 15 Dec 2019
Posts: 649

PostPosted: Mon Aug 26, 2024 4:44 pm    Post subject: Reply with quote

dimitrimemet wrote:
halcon wrote:
Hi dimitrimemet,

I code perl, not python, but, as I see the algorithm, I could tell you what I would do. I would try to *bisect* where exactly the wrong value is appearing with *debugging prints* during the loop *for millisecond in range* where, I think, it appears...

EDIT
Or before the loop, wherever. The same bisecting.

thats exactly what I've been squeezing my brains with :cry:

I would start with printing birth_chiron_long and precise_chiron_long values as the first line of the for-loop. If precise_chiron_long re-declaring before the loop is needed, I would do that (e.g. as 0).
_________________
A wife asks her husband, a programmer:
- Could you please go shopping for me and buy one carton of milk, and if they have eggs, get 6?
He comes back with 6 cartons of milk.
- Why did you buy 6 cartons of milk?
- They had eggs.
Back to top
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 9605
Location: beyond the rim

PostPosted: Mon Aug 26, 2024 4:51 pm    Post subject: Reply with quote

Well, if you've already analyzed this then you should have a clue at which point the values do become bogus. Mind we don't have any clue what this module does or even what a "Chiron return date" is, so also no clue if a value is legit or bogus (e.g. which parts of your example exactly do you think are wrong).
Back to top
View user's profile Send private message
halcon
l33t
l33t


Joined: 15 Dec 2019
Posts: 649

PostPosted: Mon Aug 26, 2024 6:44 pm    Post subject: Reply with quote

Genone wrote:
we don't have any clue what this module does or even what a "Chiron return date" is

It's astrology (interesting :) )
Chiron return date means when it has the same ecliptic coordinates (orbit position) as it was at the moment of birth :)
I calculated these values manually nearly 30 years ago 8O ))

EDIT

So, this:

dimitrimemet wrote:
Code:
Chiron longitude at birth: 0.2670171204241596
Chiron longitude at return: 251.1426886628303

is totally wrong. These values must be equal, like here:

dimitrimemet wrote:
Code:
Chiron longitude at birth: 3.5432579782070928
Chiron longitude at return: 3.5432527419255027

So... I supposed that precise_chiron_long is becoming bogus during some iteration of the loop.
_________________
A wife asks her husband, a programmer:
- Could you please go shopping for me and buy one carton of milk, and if they have eggs, get 6?
He comes back with 6 cartons of milk.
- Why did you buy 6 cartons of milk?
- They had eggs.
Back to top
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 9605
Location: beyond the rim

PostPosted: Tue Aug 27, 2024 9:05 am    Post subject: Reply with quote

halcon wrote:
So, this:

dimitrimemet wrote:
Code:
Chiron longitude at birth: 0.2670171204241596
Chiron longitude at return: 251.1426886628303

is totally wrong. These values must be equal, like here:


I see. But are both values bogus, or is at least one correct?

As for debugging, just add a
Code:
print(precise_chiron_long, birth_chiron_long, diff, best_diff, diff < best_diff, best_date)

before
Code:
if diff < best_diff

That should give you enough data to see if the loop is working as intended and in which exact case it doesn't behave as expected.

My suspicion is that the problem is caused due to you not treating the overflow from 360 to 0 properly in your delta calculations: If you have value A = 0.001 and value B = 359.999 your code will handle those value as being very different while they are actually almost equal (360 being equal to 0). Typical solution to that is
Code:
if delta > 180:
    delta = 360 - delta


But that's just an educated guess.
Back to top
View user's profile Send private message
halcon
l33t
l33t


Joined: 15 Dec 2019
Posts: 649

PostPosted: Tue Aug 27, 2024 12:32 pm    Post subject: Reply with quote

Genone wrote:
But are both values bogus, or is at least one correct?

A good question.
_________________
A wife asks her husband, a programmer:
- Could you please go shopping for me and buy one carton of milk, and if they have eggs, get 6?
He comes back with 6 cartons of milk.
- Why did you buy 6 cartons of milk?
- They had eggs.
Back to top
View user's profile Send private message
dimitrimemet
Tux's lil' helper
Tux's lil' helper


Joined: 22 Jun 2023
Posts: 84

PostPosted: Tue Aug 27, 2024 5:54 pm    Post subject: Reply with quote

halcon wrote:
Genone wrote:
But are both values bogus, or is at least one correct?

A good question.

It's correct when we have a match or as close as possible. What I mean by match is whatever swe.calc_ut determines the longitude at birth and for the return. Meaning it takes more or less 50 years for "it" to reach the same longitude. Btw all this data comes from NASA JPL and there are ways to double check and see if the code is actually correct or not either on JPL site or thru other ways. The trick is 359.xxxx and 0.xxxx ( 359 and 0 represent the actual degree longitude and the xxxx is decimal minutes and seconds)
I am sure you have different ways to get this, I am not there yet as I am learning
Back to top
View user's profile Send private message
dimitrimemet
Tux's lil' helper
Tux's lil' helper


Joined: 22 Jun 2023
Posts: 84

PostPosted: Tue Aug 27, 2024 6:04 pm    Post subject: Reply with quote

Genone wrote:
halcon wrote:
So, this:

dimitrimemet wrote:
Code:
Chiron longitude at birth: 0.2670171204241596
Chiron longitude at return: 251.1426886628303

is totally wrong. These values must be equal, like here:


I see. But are both values bogus, or is at least one correct?

As for debugging, just add a
Code:
print(precise_chiron_long, birth_chiron_long, diff, best_diff, diff < best_diff, best_date)

before
Code:
if diff < best_diff

That should give you enough data to see if the loop is working as intended and in which exact case it doesn't behave as expected.

My suspicion is that the problem is caused due to you not treating the overflow from 360 to 0 properly in your delta calculations: If you have value A = 0.001 and value B = 359.999 your code will handle those value as being very different while they are actually almost equal (360 being equal to 0). Typical solution to that is
Code:
if delta > 180:
    delta = 360 - delta


But that's just an educated guess.


https://pastebin.com/8Q2CS6F9
Back to top
View user's profile Send private message
Genone
Retired Dev
Retired Dev


Joined: 14 Mar 2003
Posts: 9605
Location: beyond the rim

PostPosted: Wed Aug 28, 2024 7:58 am    Post subject: Reply with quote

As suspected: Your precise_chiron_long is "on the other end" of the longitude scale than birth_chiron_long and always increasing, so best_diff is never updated. There will be a similar issue in the loop calculating mid_chiron_long.
Back to top
View user's profile Send private message
halcon
l33t
l33t


Joined: 15 Dec 2019
Posts: 649

PostPosted: Wed Aug 28, 2024 10:22 am    Post subject: Reply with quote

dimitrimemet wrote:
https://pastebin.com/8Q2CS6F9

precise_chiron_long is bogus already after the first iteration.

Genone wrote:
As suspected: Your precise_chiron_long is "on the other end" of the longitude scale than birth_chiron_long and always increasing, so best_diff is never updated. There will be a similar issue in the loop calculating mid_chiron_long.

Do you think, the issue will be fixed by replacing abs() with a function calling abs() after that correction:

Code:
if delta > 180:
    delta = 360 - delta

?
_________________
A wife asks her husband, a programmer:
- Could you please go shopping for me and buy one carton of milk, and if they have eggs, get 6?
He comes back with 6 cartons of milk.
- Why did you buy 6 cartons of milk?
- They had eggs.
Back to top
View user's profile Send private message
dimitrimemet
Tux's lil' helper
Tux's lil' helper


Joined: 22 Jun 2023
Posts: 84

PostPosted: Wed Aug 28, 2024 6:01 pm    Post subject: Reply with quote

Genone wrote:
As suspected: Your precise_chiron_long is "on the other end" of the longitude scale than birth_chiron_long and always increasing, so best_diff is never updated. There will be a similar issue in the loop calculating mid_chiron_long.


https://pastebin.com/8hT3RKN0

Code:
python chiron_test_return.py
Chiron return date: 2016-01-01 00:00:00.999000
Chiron longitude at birth: 0.2670171204241596
Chiron longitude at return: 347.4674799688618
Back to top
View user's profile Send private message
dimitrimemet
Tux's lil' helper
Tux's lil' helper


Joined: 22 Jun 2023
Posts: 84

PostPosted: Wed Aug 28, 2024 6:02 pm    Post subject: Reply with quote

halcon wrote:
dimitrimemet wrote:
https://pastebin.com/8Q2CS6F9

precise_chiron_long is bogus already after the first iteration.

Genone wrote:
As suspected: Your precise_chiron_long is "on the other end" of the longitude scale than birth_chiron_long and always increasing, so best_diff is never updated. There will be a similar issue in the loop calculating mid_chiron_long.

Do you think, the issue will be fixed by replacing abs() with a function calling abs() after that correction:

Code:
if delta > 180:
    delta = 360 - delta

?


try this also, same result :?
Back to top
View user's profile Send private message
dimitrimemet
Tux's lil' helper
Tux's lil' helper


Joined: 22 Jun 2023
Posts: 84

PostPosted: Wed Aug 28, 2024 7:09 pm    Post subject: Reply with quote

I knew i was over complicating things and got lost in the details ... so I simplified everything and it worked :D posting this here since i know google picks this up to share :)
Code:
import swisseph as swe
from datetime import datetime, timedelta
import numpy as np  # Importing NumPy

# Set the ephemeris path
swe.set_ephe_path('/home/ethan/Downloads/swisseph-devel/ephe')

# Birth data
birth_year = 1969
birth_month = 2
birth_day = 5
birth_time = 9.6166

# Calculate birth Julian day
jd_birth = swe.julday(birth_year, birth_month, birth_day, birth_time)

# Get Chiron's longitude at birth
chiron_birth = swe.calc_ut(jd_birth, swe.CHIRON)[0][0]
print(f"Chiron's longitude at birth: {chiron_birth:.2f} degrees")

# Function to find Chiron return
def find_chiron_return(jd_start, jd_end):
    step = 1 / 24 / 60  # 1 minute step
    for jd in np.arange(jd_start, jd_end, step):
        chiron_pos = swe.calc_ut(jd, swe.CHIRON)[0][0]
        if abs(chiron_pos - chiron_birth) < 0.0001:  # Within 0.0001 degree
            return jd
    return None

# Calculate approximate Chiron return (49 years +/- 3 years)
jd_start = swe.julday(birth_year + 46, birth_month, birth_day, birth_time)
jd_end = swe.julday(birth_year + 52, birth_month, birth_day, birth_time)

chiron_return_jd = find_chiron_return(jd_start, jd_end)

if chiron_return_jd:
    utc = swe.jdut1_to_utc(chiron_return_jd)
    chiron_return_date = datetime(utc[0], utc[1], utc[2], utc[3], utc[4], int(utc[5]))
    print(f"Chiron return date and time: {chiron_return_date.strftime('%Y-%m-%d %H:%M:%S')}")
   
    birth_date = datetime(birth_year, birth_month, birth_day, int(birth_time), int((birth_time % 1) * 60))
    age_at_return = (chiron_return_date - birth_date) / timedelta(days=365.25)
    print(f"Age at Chiron return: {age_at_return:.2f} years")
else:
    print("Chiron return not found within the specified range.")

# Clean up
swe.close()
thank you everyone for your help <3
Back to top
View user's profile Send private message
dimitrimemet
Tux's lil' helper
Tux's lil' helper


Joined: 22 Jun 2023
Posts: 84

PostPosted: Thu Aug 29, 2024 2:31 am    Post subject: Reply with quote

halcon wrote:
Genone wrote:
we don't have any clue what this module does or even what a "Chiron return date" is

It's astrology (interesting :) )
Chiron return date means when it has the same ecliptic coordinates (orbit position) as it was at the moment of birth :)
I calculated these values manually nearly 30 years ago 8O ))

How did you do it, by using some sort pdf/book ephemeris. A couple of months ago I've learned how to run all the planets manually myself but it takes over an hour to do so :roll: ... btw I do not understand astrology all that much... this is human design which could be interesting depending on where you get your info from 8O
Back to top
View user's profile Send private message
halcon
l33t
l33t


Joined: 15 Dec 2019
Posts: 649

PostPosted: Thu Aug 29, 2024 9:04 pm    Post subject: Reply with quote

dimitrimemet wrote:
How did you do it, by using some sort pdf/book ephemeris.

Yes, I used printed booklets with ephemeris of all the planets, including "minor" ones (like Chiron).

dimitrimemet wrote:
I've learned how to run all the planets manually myself but it takes over an hour to do so

I had a large notebook (paper one; I didn't get my first computer yet by that time), of A4 format, squared sheets, and during, hmm, let's say, a couple of months I filled it with calculations completely :D But I calculated everything I wanted for me and my close people, including rectification and synastry.

dimitrimemet wrote:
I do not understand astrology all that much...

Everyone of us gets exactly that he is looking for. I was looking for a kind of science in astrology and the results of my calculations satisfied me. I've been using them for years. And I am still using some of them now. :D

Seek and ye shall find. :roll:
_________________
A wife asks her husband, a programmer:
- Could you please go shopping for me and buy one carton of milk, and if they have eggs, get 6?
He comes back with 6 cartons of milk.
- Why did you buy 6 cartons of milk?
- They had eggs.
Back to top
View user's profile Send private message
Display posts from previous:   
Reply to topic    Gentoo Forums Forum Index Portage & Programming All times are GMT
Page 1 of 1

 
Jump to:  
You cannot post new topics in this forum
You cannot reply to topics in this forum
You cannot edit your posts in this forum
You cannot delete your posts in this forum
You cannot vote in polls in this forum