Skip to content

Object3d.flatten() flattens in the opposite order from numpy #595

@argerlt

Description

@argerlt

Messing around with #588 and #593, I've realized Object3d.flatten() flattens in column-major (Fortran-style) order, as opposed to the numpy default of row-major (C-style). more details here:

The initial fix is easy, just replace the existing function with the following:

    def flatten(self) -> Object3d:
        """Return a new object with the same data in a single column."""
        obj = self.__class__(self.data.reshape(self.size, self.dim))
        return obj

However, this breaks 120-ish tests, and likely some downstream applications as well.

Is this worth fixing? I'm down to take care of it, I just want to check in with others in case there is a good reason to leave this as is.

I'll also note, this ONLY is an issue when you have paired data in ORIX objects and non-orix arrays, as all ORIX objects inherit from Object3d, and are thus self-consistent

Example of the flattening issue in action:

from orix.vector import Vector3d
import matplotlib.pyplot as plt
import numpy as np

x, y = np.meshgrid(np.linspace(0, 1, 100), np.linspace(0, 1, 100))
v = Vector3d(np.stack([x, y, y*0], axis=2))
fig, ax = plt.subplots(1, 2)

# When flattened by ORIX, colum-major
ax[0].imshow(v.flatten().reshape(100, 100).x)
ax[0].set_title("Column-major flattening via ORIX")

# When flattened by numpy, row-major, which flips the axes.
ax[1].imshow(v.x.flatten().reshape(100, 100))
ax[1].set_title("Row-major flattening via numpy")
plt.tight_layout()
Image

Metadata

Metadata

Assignees

No one assigned

    Labels

    theoryThis issue has a non-technical element

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions