-
Notifications
You must be signed in to change notification settings - Fork 18
Complete cosmo_array numpy support and add cosmo_quantity
#219
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
Merged
Merged
Changes from 107 commits
Commits
Show all changes
130 commits
Select commit
Hold shift + click to select a range
6a34c84
Implement tests for cosmo_quantity.
kyleaoman 07aac86
Initial cosmo_quantity implementation.
kyleaoman 648e177
Merge branch 'master' into cosmo_quantity
kyleaoman 6eede33
First rough working implementation of a numpy func overload.
kyleaoman 32f9a2e
Start trudging through list of functions to wrap.
kyleaoman c9b8b6b
Work out an approach for concatenate-like functions.
kyleaoman 37dd6af
Implement a few more funcs, rough in all the rest.
kyleaoman fd255dc
Fill in FFT functions, continue roughing in.
kyleaoman 960bdbb
Implement block, around.
kyleaoman 77f1d27
Implement a few more funcs.
kyleaoman 914910a
Implement a few more numpy funcs.
kyleaoman a30350a
Start implementing histograms.
kyleaoman c51cd0b
Implement a few more numpy funcs.
kyleaoman f27c527
Implement linalg.det.
kyleaoman 21585c5
Attempt histogramdd implementation.
kyleaoman daa2196
Wrap up histogram testing.
kyleaoman 9f5f6e5
Amend physical conversion test because array_equal is now cosmo_array…
kyleaoman 16e40cc
Implement a couple more numpy functions.
kyleaoman 240b991
Check cosmo_factor for consistent scale_factor when preparing arguments.
kyleaoman 8880a13
Implement a few more numpy functions.
kyleaoman 30f4e35
Implement a few more numpy functions.
kyleaoman cf11cfd
Implement a few more numpy functions.
kyleaoman dd106fd
Implement more numpy functions.
kyleaoman 77db41e
Finish implementing numpy functions, immediate tests pass.
kyleaoman 922de1b
Overhaul initialisation to allow list of cosmo_arrays as input.
kyleaoman ec9e932
Cleanup some caught warnings that no longer happen :)
kyleaoman 44a6f59
Remove some unused imports and variables.
kyleaoman 3d87708
Add reshape to cosmo_quantity to enable some functions like meshgrid.
kyleaoman 165f422
Implement numpy take function.
kyleaoman ef43675
Wrap np.ndarray.take properly.
kyleaoman c86b9c9
Replace some internal uses of unyt_array with cosmo_array.
kyleaoman 59594ff
Replace unyt array/quantity with cosmo array/quantity in tests.
kyleaoman 791b24a
Cleanup some unused imports and unused variables.
kyleaoman 6501b3f
Remove duplicate comoving=... in __repr__.
kyleaoman efa66d5
Prepare to handle np functions that 'just work' in unyt but don't for…
kyleaoman 75078d2
Make use of cosmo_array being supported in numpy in power_spectrum fu…
kyleaoman cb08f9d
Make checks on numpy functions more stringent and fix revealed bugs.
kyleaoman 7de44a7
Simplify implementation of amin, amax, and implement meshgrid.
kyleaoman 63f4557
Split out cosmo attribute copying into separate function & fix some b…
kyleaoman 38c49d8
Check against unyt warning when running unyt version of function.
kyleaoman 4452fa7
Add a few more functions and a check that we're covering all function…
kyleaoman 453ef89
Check many more array funcs.
kyleaoman f21f1b7
Keep cosmo_array to cosmo_quantity conversion in one location.
kyleaoman 5dfcb02
Simplify wrapping some methods and properties.
kyleaoman 9e46bf7
Simplify array function checks and finish checking all unyt-supported…
kyleaoman 5836650
Remove some redundant wrappers.
kyleaoman e687143
Greatly simplify array function handling with wrapper functions.
kyleaoman 37870ce
Remove version guard since we're going to pin a higher unyt version a…
kyleaoman 218b156
Cleanup another import guard since we'll require newer numpy now.
kyleaoman 5bb02fb
Move array_func helper functions to _array_functions.py.
kyleaoman f891b85
Replace awkward unyt usage with new numpy supported cosmo versions.
kyleaoman d120fdb
Point requirements to unyt github main branch (temporarily).
kyleaoman 44477fb
Handle comoving is None in str representation
kyleaoman 04e522a
Refactor to keep code for casting return types in one place.
kyleaoman 65cd3fa
Cleanup while checking unyt usage.
kyleaoman 0d9a1c8
Add a test for cosmo_quantity conversion.
kyleaoman 0657ba9
Cleanup while checking unyt usage.
kyleaoman 9289480
Refactor ca_cfs to just use cf directly.
kyleaoman 303668d
Troubleshooting after refactor.
kyleaoman 448ba7d
Remove unused import.
kyleaoman 85f3900
Replace some unyt usage with cosmo versions.
kyleaoman 0f80416
Some cleanup of array constructors.
kyleaoman b2ce2df
Cleanup and document _array_functions.py
kyleaoman 3b0fc84
Start reviewing objects.py docstrings.
kyleaoman 51e5f2f
Merge branch 'master' into cosmo_quantity
kyleaoman d9fe006
Merge branch 'master' into cosmo_quantity
kyleaoman a396554
Run black.
kyleaoman 1b89f1c
Edit a few more docstrings.
kyleaoman d9fe3c9
Fix warnings from neutrino cosmology.
kyleaoman 489d60c
Use squeeze safely.
kyleaoman 9c8eb0a
Review type hints and docstrings in objects.py.
kyleaoman a1689ae
Fix some sphinx issues.
kyleaoman 2b46c57
Remove a and cosmo_factor from global namespace (was temporarily adde…
kyleaoman 95704e0
Fill in descriptions of all cosmo_array tests.
kyleaoman 7cc732e
Remove some more unused imports.
kyleaoman de7ffad
Add now-redundant checks on rotation_center argument.
kyleaoman d3dcdff
Cleanup more imports.
kyleaoman 8e9052b
Refactor try/except into getattr for fetching hsml.
kyleaoman 36a4ba2
Remove stray print statement from debugging.
kyleaoman 5871b1e
Gradually coercing vis functions to share code.
kyleaoman 4274798
Remove redundant boxsize argument, get from metadata.
kyleaoman c80267e
Remove argument from tests, too.
kyleaoman fc2177d
Cleanup more imports.
kyleaoman 20534b4
Prepared to create helper function in vis functions.
kyleaoman 9530290
Start refactoring visualisation functions with helpers.
kyleaoman 63bfb25
Move more to helper functions and cleanup.
kyleaoman 530c78f
Continue cleanup of visualisation functions.
kyleaoman 79780f9
Consolidate handling cosmo_arrays in visualisation and big namespace …
kyleaoman e85741c
Run black.
kyleaoman 399bec6
Run CI black version.
kyleaoman c4b5795
Move wrapper from backends to be called in frontend functions.
kyleaoman 06cc3ca
Move norm argument onto wrapper function.
kyleaoman d784b29
Update tests with norm arg removed from backend functions.
kyleaoman bccf1ca
Run black.
kyleaoman 1b3feae
Update some vis docstrings.
kyleaoman 71c9212
Support __round__ for cosmo_quantity and dot for cosmo_array.
kyleaoman 7b765b7
Make a pass over the visualisation docs.
kyleaoman 4f7d3d7
Switch on intersphinx, why not.
kyleaoman bc3ff27
Reorganize docs a little bit and flesh out the cosmo_array description.
kyleaoman 53d6fe1
Run black.
kyleaoman 34d9497
Add new vis backend submodules (from reorganising) to installed modules.
kyleaoman 14a0f73
Unyt fixed ftt->fft typo, update to match.
kyleaoman feae367
cache@v2 in workflows deprecated, update so that we can run CI.
kyleaoman 44483fc
Merge branch 'cache_v4' into cosmo_quantity
kyleaoman fdd0c76
Point to unyt on pypi now that 3.0.4 is released.
kyleaoman db688b0
Add support for multiplying a cosmo_array by a bare unit object from …
kyleaoman ab72215
Re-point unyt to pypi (tests will fail).
kyleaoman f843827
Point unyt dependency to fork.
kyleaoman 8036099
Remove accidentally added png files.
kyleaoman 17a384a
Rename attr copy function for clarity.
kyleaoman b130975
Add a constructor to initialize a cosmo_factor without import .
kyleaoman b91c1c5
Refactor accumulation of preservation and multiplication of cosmo_fac…
kyleaoman ab739fb
Close if statement with else instead of elif.
kyleaoman 0088892
Remove unused 'inputs' argument to _power_cosmo_factor.
kyleaoman 2fb63a9
Restore inputs argument to _power_cosmo_factor, it's used after all.
kyleaoman a82ffc3
Merge branch 'cosmo_quantity' of github.com:SWIFTSIM/swiftsimio into …
kyleaoman fcf3c28
Add a new way to construct cosmo_array with scale factor and exponent.
kyleaoman c204234
Make use of new tidier constructors for arrays and quantities.
kyleaoman 404c071
Remember to run the autoformatter.
kyleaoman 26636eb
Update docs for new constructors.
kyleaoman 6ff3c74
Add some tests for initializing with new constructors.
kyleaoman be9d2e7
Rename cosmo_array ufunc tests file descriptively.
kyleaoman e834562
Add tests for new constructors for cosmo_array.
kyleaoman 6956553
Tidy up tests to flag non-critical unresolved issues in unyt.
kyleaoman dbf8135
Re-point dependency at latest unyt release.
kyleaoman e605a53
Update visualisation example.
kyleaoman e4372ab
Revisions to visualisation examples.
kyleaoman 2709f01
Run black.
kyleaoman 2109da2
We do actually need numpy>=2.1.0, and pin unyt>=3.0.4
kyleaoman 5b89f62
Corrections to unit conversion in test.
kyleaoman File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,82 @@ | ||
| The ``cosmo_array`` | ||
| =================== | ||
|
|
||
| :mod:`swiftsimio` uses a customized class based on the :class:`~unyt.array.unyt_array` | ||
| to store data arrays. The :class:`~swiftsimio.objects.cosmo_array` has all of the same | ||
| functionality as the :class:`~unyt.array.unyt_array`, but also adds information about | ||
| data transformation between physical and comoving coordinates, and descriptive metadata. | ||
|
|
||
| For instance, should you ever need to know what a dataset represents, you can | ||
| ask for a description by accessing the ``name`` attribute: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| print(rho_gas.name) | ||
|
|
||
| which will output ``Co-moving mass densities of the particles``. | ||
|
|
||
| The cosmology information is stored in three attributes: | ||
|
|
||
| + ``comoving`` | ||
| + ``cosmo_factor`` | ||
| + ``valid_transform`` | ||
|
|
||
| The ``comoving`` attribute specifies whether the array is a physical (``True``) or | ||
| comoving (``False``) quantity, while the ``cosmo_factor`` stores the expression needed | ||
| to convert back and forth between comoving and physical quantities and the value of | ||
| the scale factor. The conversion factors can be accessed like this: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| # Conversion factor to make the densities a physical quantity | ||
| print(rho_gas.cosmo_factor.a_factor) | ||
| physical_rho_gas = rho_gas.cosmo_factor.a_factor * rho_gas | ||
|
|
||
| # Symbolic scale-factor expression | ||
| print(rho_gas.cosmo_factor.expr) | ||
|
|
||
| which will output ``132651.002785671`` and ``a**(-3.0)``. Converting an array to/from physical/comoving | ||
| is done with the :meth:`~swiftsimio.objects.cosmo_array.to_physical`, :meth:`~swiftsimio.objects.cosmo_array.to_comoving`, :meth:`~swiftsimio.objects.cosmo_array.convert_to_physical` and :meth:`~swiftsimio.objects.cosmo_array.to_comoving` methods, for instance: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| physical_rho_gas = rho_gas.to_physical() | ||
|
|
||
| # Convert in-place | ||
| rho_gas.convert_to_physical() | ||
|
|
||
| The ``valid_transform`` is a boolean flag that is set to ``False`` for some arrays that don't make sense to convert to comoving. | ||
|
|
||
| :class:`~swiftsimio.objects.cosmo_array` supports array arithmetic and the entire :mod:`numpy` range of functions. Attempting to combine arrays (e.g. by addition) will validate the cosmology information first. The implementation is designed to be permissive: it will only raise exceptions when a genuinely invalid combination is encountered, but is tolerant of missing cosmology information. When one argument in a relevant operation (like addition) is not a :class:`~swiftsimio.objects.cosmo_array` the attributes of the :class:`~swiftsimio.objects.cosmo_array` will be assumed for both arguments. In such cases a warning is produced stating that this assumption has been made. | ||
|
|
||
| .. note:: | ||
|
|
||
| :class:`~swiftsimio.objects.cosmo_array` and the related :class:`~swiftsimio.objects.cosmo_quantity` are now intended to support all :mod:`numpy` functions, propagating units and cosmology information correctly through mathematical operations. Try making a histogram with weights and ``density=True`` with :func:`numpy.histogram`! There are a large number of functions and a very large number of possible parameter combinations, so some corner cases may have been missed in development. Please report any errors or unexpected results using github issues or other channels so that they can be fixed. Currently :mod:`scipy` functions are not supported (although some might "just work"). Requests to support specific functions can be accommodated. | ||
|
|
||
| To make the most of the utility offered by the :class:`~swiftsimio.objects.cosmo_array` class, it is helpful to know how to create your own. A good template for this looks like: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| import unyt as u | ||
| from swiftsimio.objects import cosmo_array, cosmo_factor | ||
| from swiftsimio.objects import a # the scale factor represented by a sympy expression | ||
|
|
||
| my_cosmo_array = cosmo_array( | ||
| [1, 2, 3], | ||
| u.Mpc, | ||
| comoving=True, | ||
| cosmo_factor=cosmo_factor(a**1, 1.0) # 1.0 is the current scale factor | ||
| ) | ||
| # consider getting the scale factor from metadata when applicable, like: | ||
| # data.metadata.a | ||
|
|
||
| There is also a very similar :class:`~swiftsimio.objects.cosmo_quantity` class designed for scalar values, | ||
| analogous to the :class:`~unyt.array.unyt_quantity`. You may encounter this being returned by :mod:`numpy` functions. Cosmology-aware scalar values can be initialized similarly: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| import unyt as u | ||
| from swiftsimio.objects import cosmo_quantity, cosmo_factor, a | ||
|
|
||
| my_cosmo_quantity = cosmo_quantity(2, u.Mpc, comoving=False, cosmo_factor=cosmo_factor(a**1, 0.5)) | ||
|
|
||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,41 @@ | ||
| Halo Catalogues & SOAP integration | ||
| ================================== | ||
|
|
||
| SWIFT-compatible halo catalogues, such as those written with SOAP, can be | ||
| loaded entirely transparently with ``swiftsimio``. It is generally possible | ||
| to use all of the functionality (masking, visualisation, etc.) that is used | ||
| with snapshots with these files, assuming the files conform to the | ||
| correct metadata standard. | ||
|
|
||
| An example SOAP file is available at | ||
| ``http://virgodb.cosma.dur.ac.uk/swift-webstorage/IOExamples/soap_example.hdf5`` | ||
|
|
||
| You can load SOAP files as follows: | ||
|
|
||
| .. code-block:: python | ||
|
|
||
| from swiftsimio import load | ||
|
|
||
| catalogue = load("soap_example.hdf5") | ||
|
|
||
| print(catalogue.spherical_overdensity_200_mean.total_mass) | ||
|
|
||
| # >>> [ 591. 328.5 361. 553. 530. 507. 795. | ||
| # 574. 489.5 233.75 0. 1406. 367.5 2308. | ||
| # ... | ||
| # 0. 534. 0. 191.75 1450. 600. 290. ] 10000000000.0*Msun (Physical) | ||
|
|
||
| What's going on here? Under the hood, ``swiftsimio`` has a discrimination function | ||
| between different metadata types, based upon a property stored in the HDF5 file, | ||
| ``Header/OutputType``. If this is set to ``FullVolume``, we have a snapshot, | ||
| and use the :obj:`swiftsimio.metadata.objects.SWIFTSnapshotMetadata` | ||
| class. If it is ``SOAP``, we use | ||
| :obj:`swiftsimio.metadata.objects.SWIFTSOAPMetadata`, which instructs | ||
| ``swiftsimio`` to read slightly different properties from the HDF5 file. | ||
|
|
||
| swiftgalaxy | ||
| ----------- | ||
|
|
||
| The :mod:`swiftgalaxy` companion package to :mod:`swiftsimio` offers further integration with halo catalogues in SOAP, Caesar and Velociraptor formats (so far). It greatly simplifies efficient loading of particles belonging to an object from a catalogue, and additional tools that are useful when working with a galaxy or other localized collection of particles. Refer to the `swiftgalaxy documentation`_ for details. | ||
|
|
||
| .. _swiftgalaxy documentation: https://swiftgalaxy.readthedocs.io/en/latest/ |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.