Skip to content

Commit

Permalink
Add preliminary radial profile.
Browse files Browse the repository at this point in the history
  • Loading branch information
cmeyer committed Nov 8, 2023
1 parent 8ee6da6 commit 724c011
Show file tree
Hide file tree
Showing 3 changed files with 49 additions and 0 deletions.
4 changes: 4 additions & 0 deletions CHANGES.rst
Original file line number Diff line number Diff line change
@@ -1,6 +1,10 @@
Changelog (niondata)
====================

15.6.2 (UNRELEASED)
-------------------
- Add radial profile function.

15.6.1 (2023-10-23)
-------------------
- Minor update for typing compatibility.
Expand Down
42 changes: 42 additions & 0 deletions nion/data/Core.py
Original file line number Diff line number Diff line change
Expand Up @@ -1927,6 +1927,48 @@ def calculate_data(data: _ImageDataType) -> _ImageDataType:
dimensional_calibrations=dimensional_calibrations)


def function_radial_profile(data_and_metadata_in: _DataAndMetadataLike, center: typing.Optional[NormPointType] = None) -> DataAndMetadata.DataAndMetadata:
data_and_metadata = DataAndMetadata.promote_ndarray(data_and_metadata_in)

if not Image.is_data_valid(data_and_metadata.data):
raise ValueError("Radial profile: invalid data")

if not Image.is_data_2d(data_and_metadata.data):
raise ValueError("Radial profile: data must be 2D")

if data_and_metadata.is_data_complex_type:
raise ValueError("Radial profile: data must be scalar (not complex)")

dimensional_calibrations = data_and_metadata.dimensional_calibrations
is_uniform_calibration = dimensional_calibrations[0].units == dimensional_calibrations[1].units

if center:
center_point = Geometry.FloatPoint.make(center)
elif is_uniform_calibration:
center_point = Geometry.FloatPoint(y=dimensional_calibrations[0].convert_from_calibrated_value(0.0), x=dimensional_calibrations[1].convert_from_calibrated_value(0.0))
else:
center_point = Geometry.FloatPoint(y=data_and_metadata.data_shape[0] / 2.0, x=data_and_metadata.data_shape[1] / 2.0)

# see https://stackoverflow.com/questions/21242011/most-efficient-way-to-calculate-radial-profile
y, x = numpy.indices((data_and_metadata.data_shape))

This comment has been minimized.

Copy link
@tylerharvey

tylerharvey Nov 9, 2023

This line should be

y, x = numpy.indices((data_and_metadata.data_shape),sparse=True)

for speed (behavior is identical).

r = (numpy.sqrt((x - center_point.x) ** 2 + (y - center_point.y) ** 2)).astype(int)
total_binned = numpy.bincount(r.ravel(), data_and_metadata.data.ravel())
radial_count = numpy.bincount(r.ravel())
result_data = total_binned / radial_count

if is_uniform_calibration:
dimensional_calibrations = [Calibration.Calibration(0.0, dimensional_calibrations[1].scale, dimensional_calibrations[1].units)]
else:
dimensional_calibrations = [Calibration.Calibration()]

return DataAndMetadata.new_data_and_metadata(result_data,
intensity_calibration=data_and_metadata.intensity_calibration,
dimensional_calibrations=dimensional_calibrations,
timestamp=data_and_metadata.timestamp,
timezone=data_and_metadata.timezone,
timezone_offset=data_and_metadata.timezone_offset)


def function_make_point(y: float, x: float) -> NormPointType:
return y, x

Expand Down
3 changes: 3 additions & 0 deletions nion/data/xdata_1_0.py
Original file line number Diff line number Diff line change
Expand Up @@ -269,6 +269,9 @@ def line_profile(data_and_metadata: _DataAndMetadataLike, vector: Core.NormVecto
def invert(data_and_metadata: _DataAndMetadataLike) -> DataAndMetadata.DataAndMetadata:
return Core.function_invert(data_and_metadata)

def radial_profile(data_and_metadata: _DataAndMetadataLike, center: typing.Optional[Core.NormPointType] = None) -> DataAndMetadata.DataAndMetadata:
return Core.function_radial_profile(data_and_metadata, center)

# sequences: registration, shifting, alignment, integrate, trim, insert, concat, extract

def register_translation(xdata1: _DataAndMetadataLike, xdata2: _DataAndMetadataLike, upsample_factor: typing.Optional[int] = None, subtract_means: bool = True) -> typing.Tuple[float, ...]:
Expand Down

0 comments on commit 724c011

Please sign in to comment.