• Home
  • Features
  • Pricing
  • Docs
  • Announcements
  • Sign In

morganjwilliams / pyrolite / 11624563836

01 Nov 2024 04:58AM UTC coverage: 91.367% (-0.2%) from 91.538%
11624563836

push

github

morganjwilliams
Update get_ionic_radii test

6223 of 6811 relevant lines covered (91.37%)

10.95 hits per line

Source File
Press 'n' to go to next uncovered line, 'b' for previous

86.67
/pyrolite/util/plot/transform.py
1
"""
2
Transformation utilites for matplotlib.
3
"""
4

5
import numpy as np
12✔
6

7
from ...comp.codata import close
12✔
8
from ..log import Handle
12✔
9

10
logger = Handle(__name__)
12✔
11

12

13
def affine_transform(mtx=np.array([[1, 0, 0], [0, 1, 0], [0, 0, 1]])):
12✔
14
    """
15
    Construct a function which will perform a 2D affine transform based on
16
    a 3x3 affine matrix.
17

18
    Parameters
19
    -----------
20
    mtx : :class:`numpy.ndarray`
21
    """
22

23
    def tfm(data):
12✔
24
        xy = data[:, :2]
12✔
25
        return (mtx @ np.vstack((xy.T[:2], np.ones(xy.T.shape[1]))))[:2]
12✔
26

27
    return tfm
12✔
28

29

30
def tlr_to_xy(tlr):
12✔
31
    """
32
    Transform a ternary coordinate system (top-left-right) to an xy-cartesian
33
    coordinate system.
34

35
    Parameters
36
    ----------
37
    tlr : :class:`numpy.ndarray`
38
        Array of shape (n, 3) in the t-l-r coordinate system.
39

40
    Returns
41
    --------
42
    xy : :class:`numpy.ndarray`
43
        Array of shape (n, 2) in the x-y coordinate system.
44
    """
45
    shear = affine_transform(np.array([[1, 1 / 2, 0], [0, 1, 0], [0, 0, 1]]))
12✔
46
    return shear(close(np.array(tlr)[:, [2, 0, 1]])).T
12✔
47

48

49
def xy_to_tlr(xy):
12✔
50
    """
51

52
    Parameters
53
    -----------
54
    xy : :class:`numpy.ndarray`
55
        Array of shape (n, 2) in the x-y coordinate system.
56

57
    Returns
58
    --------
59
    tlr : :class:`numpy.ndarray`
60
        Array of shape (n, 3) in the t-l-r coordinate system.
61
    """
62
    shear = affine_transform(np.array([[1, -1 / 2, 0], [0, 1, 0], [0, 0, 1]]))
×
63
    r, t = shear(xy)
×
64
    l = 1.0 - (r + t)
×
65
    return np.vstack([t, l, r]).T
×
66

67

68
def ABC_to_xy(ABC, xscale=1.0, yscale=1.0):
12✔
69
    """
70
    Convert ternary compositional coordiantes to x-y coordinates
71
    for visualisation within a triangle.
72

73
    Parameters
74
    -----------
75
    ABC : :class:`numpy.ndarray`
76
        Ternary array (:code:`samples, 3`).
77
    xscale : :class:`float`
78
        Scale for x-axis.
79
    yscale : :class:`float`
80
        Scale for y-axis.
81

82
    Returns
83
    --------
84
    :class:`numpy.ndarray`
85
        Array of x-y coordinates (:code:`samples, 2`)
86
    """
87
    assert ABC.shape[-1] == 3
12✔
88
    # transform from ternary to xy cartesian
89
    scale = affine_transform(np.array([[xscale, 0, 0], [0, yscale, 0], [0, 0, 1]]))
12✔
90
    shear = affine_transform(np.array([[1, 1 / 2, 0], [0, 1, 0], [0, 0, 1]]))
12✔
91
    xy = scale(shear(close(ABC)).T)
12✔
92
    return xy.T
12✔
93

94

95
def xy_to_ABC(xy, xscale=1.0, yscale=1.0):
12✔
96
    """
97
    Convert x-y coordinates within a triangle to compositional ternary coordinates.
98

99
    Parameters
100
    -----------
101
    xy : :class:`numpy.ndarray`
102
        XY array (:code:`samples, 2`).
103
    xscale : :class:`float`
104
        Scale for x-axis.
105
    yscale : :class:`float`
106
        Scale for y-axis.
107

108
    Returns
109
    --------
110
    :class:`numpy.ndarray`
111
        Array of ternary coordinates (:code:`samples, 3`)
112
    """
113
    assert xy.shape[-1] == 2
12✔
114
    # transform from xy cartesian to ternary
115
    scale = affine_transform(
12✔
116
        np.array([[1 / xscale, 0, 0], [0, 1 / yscale, 0], [0, 0, 1]])
117
    )
118
    shear = affine_transform(np.array([[1, -1 / 2, 0], [0, 1, 0], [0, 0, 1]]))
12✔
119
    A, B = shear(scale(xy).T)
12✔
120
    C = 1.0 - (A + B)  # + (xscale-1) + (yscale-1)
12✔
121
    return np.vstack([A, B, C]).T
12✔
STATUS · Troubleshooting · Open an Issue · Sales · Support · CAREERS · ENTERPRISE · START FREE · SCHEDULE DEMO
ANNOUNCEMENTS · TWITTER · TOS & SLA · Supported CI Services · What's a CI service? · Automated Testing

© 2026 Coveralls, Inc