Skip to content

Dev/simplify imports#138

Open
astrolamb wants to merge 9 commits into
nanograv:devfrom
astrolamb:dev/simplify_imports
Open

Dev/simplify imports#138
astrolamb wants to merge 9 commits into
nanograv:devfrom
astrolamb:dev/simplify_imports

Conversation

@astrolamb
Copy link
Copy Markdown
Collaborator

Description

In most of holodeck's modules, it imported ALL other modules with

import holodeck as holo

This isn't ideal -- especially when only one variable is required from another module. Some egrecious examples I have seen include importing all modules to only require holo.__version__, and importing specific modules such as utils.py only to access its subroutines via holo.utils. It's inefficients, uses up memory, and its messy.

Todos

Notable points that this PR has either accomplished or will accomplish.

  • Reduce memory usage
  • Tidy up code
  • Update tests

Notes

In order to make this work, I have had to change the import to Semi_Analytical_Models, which will be a change for most users. I had to make this change because it resulted in a circular import, which caused a failure.

In the previous version, one could import this class using

from holodeck import sams
sam = sams.Semi_Analytical_Model()

This no longer works.

In this new version, I recommend the following usage:

from holodeck.sams.sam import Semi_Analytical_Model
sam = Semi_Analytical_Model()

This has the advantage of also being neater.

Status

  • Ready to go

Shreyas Tiruvaskar and others added 9 commits February 26, 2025 14:19
…urce strains in gravwaves._gws_harmonics_at_evo_fobs()
minor bugfix to add missing factor of 1/dlnf in definition of loud so…
…ces-classic

Fixing errors in param_spaces_classic.py

I believe the code in question was created to create a user-friendly way of reproducing the NANOGrav 15yr Astro Interpretation paper. It was not the code that we actually used for that analysis. This fix corrects a mistake introduced in an attempt to do "open science".
* Fixing errors in param_spaces_classic.py

* Fix bug in sams/components.py

---------

Co-authored-by: Shreyas Tiruvaskar <sti50@bohr.canterbury.ac.nz>
@astrolamb
Copy link
Copy Markdown
Collaborator Author

It appears that previous pull requests have made it into this pull request too...

@kayhangultekin
Copy link
Copy Markdown
Collaborator

@astrolamb , some of the changes seem to be different from the PR title. For example, in ‎holodeck/librarian/tests/test_lib_tools__param_space.py‎ around line 272 or so. I'm a little confused about the diff here.

@astrolamb
Copy link
Copy Markdown
Collaborator Author

@kayhangultekin yeah I messed up somewhere. Even though I branched from main, it appears to be attempting to push PRs from dev. Let me try rebasing to dev

@astrolamb astrolamb changed the base branch from main to dev April 21, 2026 18:05
@astrolamb
Copy link
Copy Markdown
Collaborator Author

@kayhangultekin which branch is THE branch to update? Because we have a main branch, a dev branch which is both ahead and behind the main branch, and whole bunch of other branches. This rebasing still has external diffs.

The purpose of this PR is to clean up imports and avoid circular importing. There'll be conflicts in the future though if we don't have a specific branch upon which we're basing/merging from, whether that is dev or main.

@kayhangultekin
Copy link
Copy Markdown
Collaborator

@astrolamb I'm sure I messed it up due to my lack of knowledge of things git and github.

By and large, dev is the branch to update. There were a few hotfixes and PRs that were pushed to main. I didn't want dev to be behind on these hotfixes so I pushed them to dev as well. I wasn't sure what the right way to do it was. I asked @davecwright3 , and he told me there was no perfect thing to do but that what I did was optimal for preventing one class of problems. I suppose this caused problems for you, though.

Any thoughts @davecwright3 ?

@astrolamb
Copy link
Copy Markdown
Collaborator Author

@kayhangultekin the dev branch is behind main by six commits. Syncing the six commits to dev will fix the above issues

@davecwright3
Copy link
Copy Markdown
Collaborator

@kayhangultekin I didn't realize that you had done a squash merge. This discards all commit history in the merge, but keeps the contents. This is fine for pull requests, but for long-term branches can cause the issues we're seeing here.

Essentially, main thinks dev is behind because it cannot find matching commits, even though the contents of those commits are in dev.

@astrolamb for now, you should base your feature branches on dev. You won't have any merge conflicts if you try to merge your feature branch to dev. Once the busy week is over, I'll rebase dev onto main so that they have a common history. I'll have to force-push dev, so I need to wait until all of the recent feature branches from dev are merged in.

At the end of the day, it's my fault for not looking closely enough at the original main -> dev merge. Sorry about that. I'll get this fixed.

@davecwright3
Copy link
Copy Markdown
Collaborator

davecwright3 commented Apr 22, 2026

@astrolamb do you know what is so slow in the holodeck import? The only thing I see in holodeck/__init__.py that could be slow is

# NOTE: Must load and initialize cosmology before importing other submodules!
import cosmopy   # noqa
cosmo = cosmopy.Cosmology(
    h=Parameters.HubbleParam, Om0=Parameters.Omega0, Ob0=Parameters.OmegaBaryon,
    size=200,
)
del cosmopy

and we could just cache that function call.

The only modules it imports are utils and constants

@kayhangultekin
Copy link
Copy Markdown
Collaborator

Okay. Is there a way to fix my mess up?

@astrolamb
Copy link
Copy Markdown
Collaborator Author

@astrolamb do you know what is so slow in the holodeck import? The only thing I see in holodeck/__init__.py that could be slow is

# NOTE: Must load and initialize cosmology before importing other submodules!
import cosmopy   # noqa
cosmo = cosmopy.Cosmology(
    h=Parameters.HubbleParam, Om0=Parameters.Omega0, Ob0=Parameters.OmegaBaryon,
    size=200,
)
del cosmopy

and we could just cache that function call.

The only modules it imports are utils and constants

@davecwright3 yeah that probably is it

@kayhangultekin
Copy link
Copy Markdown
Collaborator

@davecwright3 @astrolamb

Chatting with gemini tells me that we can do the following, which addresses @astrolamb's original issue with @davecwright3's altered strategy, if I understand correctly.

In __init__.py we replace the cosmopy import and calculation with:

class _CosmoProxy:
    def __init__(self):
        self._real_cosmo = None

    def _get_real_cosmo(self):
        if self._real_cosmo is None:
            # This is the "Lazy Trigger"
            print("Initializing slow cosmopy object...") 
            import cosmopy
            from . import Parameters  # Ensure Parameters is available
            self._real_cosmo = cosmopy.Cosmology(
                h=Parameters.HubbleParam, 
                Om0=Parameters.Omega0, 
                Ob0=Parameters.OmegaBaryon,
                size=200,
            )
        return self._real_cosmo

    # Forward all attribute lookups to the real object
    def __getattr__(self, name):
        return getattr(self._get_real_cosmo(), name)

    # If the real object is callable, forward that too
    def __call__(self, *args, **kwargs):
        return self._get_real_cosmo()(*args, **kwargs)

# Create the proxy in the holodeck namespace
cosmo = _CosmoProxy()

There are a couple of places (utils.py and simple_sam.py) where we might want to create a new helper function for _AGE_UNIVERSE_GYR, which is acting as a global variable.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

5 participants