Skip to main content

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_dipole returns a scipy.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

Project details


Download files

Download the file for your platform. If you're not sure which to choose, learn more about installing packages.

Source Distribution

pypole-0.2.0.tar.gz (7.5 MB view details)

Uploaded Source

Built Distribution

If you're not sure about the file name format, learn more about wheel file names.

pypole-0.2.0-py3-none-any.whl (16.1 kB view details)

Uploaded Python 3

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

Hashes for pypole-0.2.0.tar.gz
Algorithm Hash digest
SHA256 a4c28cb70354e381cf66fc470150cdf83f6045f53f657735af2eb2e22e7869a4
MD5 01f1f848900b3fc307cf93a5214e0c81
BLAKE2b-256 f2ea89c3b78e7c464ad53ded9d3b28ac722808036a652353c66a0393d19f2514

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypole-0.2.0.tar.gz:

Publisher: python-publish.yml on MikeVolk/pypole

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

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

Hashes for pypole-0.2.0-py3-none-any.whl
Algorithm Hash digest
SHA256 4e464faf6d1ca981991817d2442644901f708498b466cb084970bc513b6dd0a0
MD5 73b0365a7552dcfe0b87d84c42fc23e2
BLAKE2b-256 45ea98a28e08242623fa47a1cb4d8a649e6ae4afa29f1a6a542ee10d1240f755

See more details on using hashes here.

Provenance

The following attestation bundles were made for pypole-0.2.0-py3-none-any.whl:

Publisher: python-publish.yml on MikeVolk/pypole

Attestations: Values shown here reflect the state when the release was signed and may no longer be current.

Supported by

AWS Cloud computing and Security Sponsor Datadog Monitoring Depot Continuous Integration Fastly CDN Google Download Analytics Pingdom Monitoring Sentry Error logging StatusPage Status page