Tools for simulating and fitting magnetic field maps from magnetic dipoles
Project description
pypole
pypole is a Python library for simulating and fitting magnetic field maps from magnetic dipole sources. It provides tools for generating synthetic dipole fields, fitting dipoles to measured maps, and processing magnetic field data using FFT-based methods.
Uses NumPy and Numba for fast numerical computation, with JIT-compiled kernels for pixel-level parallelism.
Installation
pypole uses uv for package management.
git clone https://github.com/MikeVolk/pypole.git
cd pypole
uv sync
Quick Start
import numpy as np
from pypole import maps, dipole, fit
# Generate observation grid (100x100 pixels, 1 um pixel size)
x_grid, y_grid = maps.get_grid(pixels=(100, 100), pixel_size=1e-6)
# Generate a random single-dipole source
locations, moments = maps.get_random_sources(n_sources=1)
# Compute the Bz field map (sensor 5 um above sample)
field_map = dipole.calculate_map(x_grid, y_grid, locations, moments, sensor_distance=5e-6)
# Fit a single dipole to the map
p0 = (*locations[0], *moments[0]) # initial guess: (x, y, z, mx, my, mz)
result = fit.fit_dipole(field_map, p0, pixel_size=1e-6)
# Access optimised parameters
print(result.x) # (x_source, y_source, z_source, mx, my, mz)
print(result.success)
Modules
| Module | Role |
|---|---|
dipole |
Core physics: dipole_field() computes Bz from a single dipole (T). calculate_map() sums contributions from all sources. synthetic_map() generates a complete random map. |
maps |
Grid and source generation: get_grid(), get_random_sources(), get_random_dim(), get_random_locations(). |
compute |
Signal processing: FFT-based upward_continue(), vectorized rms(), dipolarity_param(), pad_map(). |
fit |
Dipole fitting: fit_dipole() uses scipy.optimize.least_squares with Huber loss + TRF method. fit_dipole_n_maps() fits a batch of maps in parallel via Numba. |
convert |
Coordinate conversion between Cartesian (x, y, z) and geomagnetic polar (declination, inclination, magnitude). |
plotting |
Matplotlib helpers for field maps and fit results. |
Conventions
- Dipole parameters are always 6-tuples ordered
(x_source, y_source, z_source, mx, my, mz)— positions in metres, moments in Am². - Coordinate system: NED (North-East-Down). North = -Y axis, +Z points down. Declination is measured CCW from North; 0° = -Y, 90° = +X. Inclination > 0 means pointing downward (+Z).
fit_dipolereturns ascipy.optimize.OptimizeResult— access optimised parameters via.x.
Signal Processing
from pypole import compute
# Upward continue a map by 2 um
continued = compute.upward_continue(field_map, distance=2e-6, pixel_size=1e-6)
# Compute RMS (works on 2D or batched 3D arrays)
rms_value = compute.rms(field_map)
# Compute dipolarity parameter (1 = perfect dipole)
dp = compute.dipolarity_param(field_map, fitted_map)
Coordinate Conversion
from pypole import convert
import numpy as np
# Convert declination/inclination/moment -> x/y/z
dim = np.array([[45.0, 30.0, 1e-14]]) # (dec deg, inc deg, moment Am^2)
xyz = np.zeros((1, 3))
convert.dim2xyz(dim, xyz)
# Convert x/y/z -> declination/inclination/moment
xyz_in = np.array([[1e-14, 0.0, 0.0]])
dim_out = np.zeros((1, 3))
convert.xyz2dim(xyz_in, dim_out)
Development
uv sync # install all dependencies (including dev group)
uv run pytest # run all tests + doctests
make codestyle # format with ruff (format + fix)
make check-codestyle # lint check without modifying
make ty # type-check with ty
make lint # test + check-codestyle + ty
make pre-commit # run all pre-commit hooks
License
MIT License. See the LICENSE file for details.
Contact
- Email: michaelvolk1979@gmail.com
- GitHub: @MikeVolk
Project details
Release history Release notifications | RSS feed
Download files
Download the file for your platform. If you're not sure which to choose, learn more about installing packages.
Source Distribution
Built Distribution
Filter files by name, interpreter, ABI, and platform.
If you're not sure about the file name format, learn more about wheel file names.
Copy a direct link to the current filters
File details
Details for the file pypole-0.2.0.tar.gz.
File metadata
- Download URL: pypole-0.2.0.tar.gz
- Upload date:
- Size: 7.5 MB
- Tags: Source
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
a4c28cb70354e381cf66fc470150cdf83f6045f53f657735af2eb2e22e7869a4
|
|
| MD5 |
01f1f848900b3fc307cf93a5214e0c81
|
|
| BLAKE2b-256 |
f2ea89c3b78e7c464ad53ded9d3b28ac722808036a652353c66a0393d19f2514
|
Provenance
The following attestation bundles were made for pypole-0.2.0.tar.gz:
Publisher:
python-publish.yml on MikeVolk/pypole
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pypole-0.2.0.tar.gz -
Subject digest:
a4c28cb70354e381cf66fc470150cdf83f6045f53f657735af2eb2e22e7869a4 - Sigstore transparency entry: 1044563727
- Sigstore integration time:
-
Permalink:
MikeVolk/pypole@10f90b71f225e01a0e0a9abe5c7f2ef73b554256 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/MikeVolk
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@10f90b71f225e01a0e0a9abe5c7f2ef73b554256 -
Trigger Event:
release
-
Statement type:
File details
Details for the file pypole-0.2.0-py3-none-any.whl.
File metadata
- Download URL: pypole-0.2.0-py3-none-any.whl
- Upload date:
- Size: 16.1 kB
- Tags: Python 3
- Uploaded using Trusted Publishing? Yes
- Uploaded via: twine/6.1.0 CPython/3.13.7
File hashes
| Algorithm | Hash digest | |
|---|---|---|
| SHA256 |
4e464faf6d1ca981991817d2442644901f708498b466cb084970bc513b6dd0a0
|
|
| MD5 |
73b0365a7552dcfe0b87d84c42fc23e2
|
|
| BLAKE2b-256 |
45ea98a28e08242623fa47a1cb4d8a649e6ae4afa29f1a6a542ee10d1240f755
|
Provenance
The following attestation bundles were made for pypole-0.2.0-py3-none-any.whl:
Publisher:
python-publish.yml on MikeVolk/pypole
-
Statement:
-
Statement type:
https://in-toto.io/Statement/v1 -
Predicate type:
https://docs.pypi.org/attestations/publish/v1 -
Subject name:
pypole-0.2.0-py3-none-any.whl -
Subject digest:
4e464faf6d1ca981991817d2442644901f708498b466cb084970bc513b6dd0a0 - Sigstore transparency entry: 1044563771
- Sigstore integration time:
-
Permalink:
MikeVolk/pypole@10f90b71f225e01a0e0a9abe5c7f2ef73b554256 -
Branch / Tag:
refs/tags/v0.2.0 - Owner: https://github.com/MikeVolk
-
Access:
public
-
Token Issuer:
https://token.actions.githubusercontent.com -
Runner Environment:
github-hosted -
Publication workflow:
python-publish.yml@10f90b71f225e01a0e0a9abe5c7f2ef73b554256 -
Trigger Event:
release
-
Statement type: