-
Notifications
You must be signed in to change notification settings - Fork 8
Add deprecation and gce integration for Conditional Entropy #48
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: v1.0
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change | ||
|---|---|---|---|---|
| @@ -0,0 +1,281 @@ | ||||
| """ | ||||
| Optional wrapper for the gce package (Conditional Entropy with period derivatives). | ||||
|
|
||||
| This module provides a cuvarbase-compatible interface to the gce package | ||||
| by Katz et al. (2020), which implements a more efficient Conditional Entropy | ||||
| algorithm that enables period derivative searches. | ||||
|
|
||||
| **This is NOT a reimplementation** - it requires gce to be installed separately. | ||||
| We provide this wrapper only for API convenience and integration with the | ||||
| cuvarbase ecosystem. | ||||
|
|
||||
| Installation | ||||
| ------------ | ||||
| To use this module, install gce separately:: | ||||
|
|
||||
| pip install gce-search | ||||
|
|
||||
| Or from source:: | ||||
|
|
||||
| pip install git+https://github.com/mikekatz04/gce.git | ||||
|
|
||||
| Credits | ||||
| ------- | ||||
| All credit for the CE algorithm implementation goes to: | ||||
|
|
||||
| Katz, M. L., Larson, S. L., Cohn, J., Vallisneri, M., & Graff, P. B. (2020). | ||||
| "Efficient computation of the Conditional Entropy period search." | ||||
| arXiv:2006.06866 | ||||
|
|
||||
| gce package: https://github.com/mikekatz04/gce | ||||
|
|
||||
| Examples | ||||
| -------- | ||||
| Simple period search (no period derivatives):: | ||||
|
|
||||
| from cuvarbase.ce_gce import ce_gce | ||||
| import numpy as np | ||||
|
|
||||
| t = np.sort(np.random.uniform(0, 100, 1000)) | ||||
| y = 12 + 0.01 * np.cos(2 * np.pi * t / 5.0) | ||||
| y += 0.01 * np.random.randn(len(t)) | ||||
|
|
||||
| freqs = np.linspace(0.1, 1.0, 1000) | ||||
| ce_values = ce_gce(t, y, freqs) | ||||
|
|
||||
| Period derivative search:: | ||||
|
|
||||
| freqs = np.linspace(0.1, 1.0, 100) | ||||
| fdots = np.linspace(-1e-6, 1e-6, 50) | ||||
| ce_values = ce_gce(t, y, freqs, fdots=fdots) # Returns 2D array | ||||
|
|
||||
| With magnitude information for better CE:: | ||||
|
|
||||
| mag = y # Can use magnitudes instead of fluxes | ||||
| ce_values = ce_gce(t, mag, freqs, use_mag=True) | ||||
| """ | ||||
|
|
||||
| import numpy as np | ||||
| import warnings | ||||
|
|
||||
| __all__ = ['ce_gce', 'CE_GCE_AVAILABLE'] | ||||
|
|
||||
| # Check if gce is available | ||||
| try: | ||||
| from gce import ConditionalEntropy | ||||
| CE_GCE_AVAILABLE = True | ||||
| except ImportError: | ||||
| CE_GCE_AVAILABLE = False | ||||
| ConditionalEntropy = None | ||||
|
|
||||
|
|
||||
| def ce_gce(t, y, freqs, fdots=None, phase_bins=10, mag_bins=5, | ||||
| use_mag=False, return_best=False, **kwargs): | ||||
| """ | ||||
| Conditional Entropy period search using the gce package backend. | ||||
|
|
||||
| This function wraps the gce package (Katz et al. 2020) to provide | ||||
| a cuvarbase-compatible API for Conditional Entropy searches, including | ||||
| optional period derivative (Pdot) searches. | ||||
|
|
||||
| Parameters | ||||
| ---------- | ||||
| t : array-like | ||||
| Observation times | ||||
| y : array-like | ||||
| Observed values (fluxes or magnitudes) | ||||
| freqs : array-like | ||||
| Frequencies to search (1D array) | ||||
| fdots : array-like, optional | ||||
| Frequency derivatives to search. If provided, performs a 2D search | ||||
| over (frequency, frequency_derivative) space. Default: None (1D search) | ||||
| phase_bins : int, optional | ||||
| Number of phase bins for the CE calculation. Default: 10 | ||||
| mag_bins : int, optional | ||||
| Number of magnitude bins for the CE calculation. Default: 5 | ||||
| use_mag : bool, optional | ||||
| If True, treats y as magnitudes. If False, treats as fluxes. | ||||
| Default: False | ||||
| return_best : bool, optional | ||||
| If True, returns only the minimum CE value and corresponding parameters. | ||||
| If False, returns the full CE grid. Default: False | ||||
| **kwargs : dict | ||||
| Additional keyword arguments passed to gce.ConditionalEntropy | ||||
|
|
||||
| Returns | ||||
| ------- | ||||
| ce_values : ndarray | ||||
| Conditional entropy values. Shape is (len(freqs),) for 1D search | ||||
| or (len(freqs), len(fdots)) for 2D search. | ||||
|
|
||||
| best_params : dict, optional | ||||
| Only returned if return_best=True. Contains: | ||||
| - 'ce_min': minimum CE value | ||||
| - 'freq_best': best frequency | ||||
| - 'fdot_best': best frequency derivative (if fdots provided) | ||||
| - 'period_best': best period (1/freq_best) | ||||
|
|
||||
| Raises | ||||
| ------ | ||||
| ImportError | ||||
| If gce package is not installed | ||||
|
|
||||
| Notes | ||||
| ----- | ||||
| This is a thin wrapper around gce.ConditionalEntropy. All credit for | ||||
| the algorithm and implementation goes to Katz et al. (2020). | ||||
|
|
||||
| The gce implementation is significantly more efficient than cuvarbase's | ||||
| native CE implementation, especially for: | ||||
| - Period derivative searches (not tractable with cuvarbase) | ||||
| - Large frequency grids | ||||
| - Better GPU utilization even for simple period searches | ||||
|
|
||||
| References | ||||
| ---------- | ||||
| Katz, M. L., Larson, S. L., Cohn, J., Vallisneri, M., & Graff, P. B. (2020). | ||||
| arXiv:2006.06866 | ||||
|
|
||||
| Examples | ||||
| -------- | ||||
| >>> from cuvarbase.ce_gce import ce_gce | ||||
| >>> import numpy as np | ||||
| >>> t = np.linspace(0, 100, 1000) | ||||
| >>> y = 12 + 0.01 * np.cos(2 * np.pi * t / 5.0) + 0.01 * np.random.randn(1000) | ||||
| >>> freqs = np.linspace(0.1, 1.0, 100) | ||||
| >>> ce = ce_gce(t, y, freqs) | ||||
| >>> best_freq_idx = np.argmin(ce) | ||||
| >>> print(f"Best period: {1/freqs[best_freq_idx]:.2f} days") | ||||
| """ | ||||
| if not CE_GCE_AVAILABLE: | ||||
| raise ImportError( | ||||
| "This function requires the gce package.\n" | ||||
| "Install it with: pip install gce-search\n" | ||||
| "Or from source: pip install git+https://github.com/mikekatz04/gce.git\n" | ||||
| "\n" | ||||
| "For simple period searches without gce, you can use " | ||||
| "cuvarbase.ce.ConditionalEntropyAsyncProcess, though it is less efficient." | ||||
| ) | ||||
|
|
||||
| # Convert inputs to numpy arrays | ||||
| t = np.asarray(t, dtype=np.float64) | ||||
| y = np.asarray(y, dtype=np.float64) | ||||
| freqs = np.asarray(freqs, dtype=np.float64) | ||||
|
|
||||
| if fdots is not None: | ||||
| fdots = np.asarray(fdots, dtype=np.float64) | ||||
|
|
||||
| # Prepare data for gce | ||||
| # gce expects times, magnitudes/fluxes, and optionally errors | ||||
| if 'dy' in kwargs: | ||||
| dy = np.asarray(kwargs.pop('dy'), dtype=np.float64) | ||||
| else: | ||||
| dy = None | ||||
|
|
||||
| # Convert frequencies to periods for gce if needed | ||||
| # (gce may expect periods - check their API) | ||||
| periods = 1.0 / freqs | ||||
|
|
||||
| if fdots is not None: | ||||
| # Convert fdot to pdot: pdot = -fdot / f^2 | ||||
| pdots = -fdots / (freqs[:, None] ** 2) | ||||
| pdots = pdots[0, :] # Extract 1D pdot array | ||||
|
||||
| pdots = pdots[0, :] # Extract 1D pdot array |
Uh oh!
There was an error while loading. Please reload this page.