Skip to content

write

format_path_properly(folder, file_name, extension)

Makes a valid recording path.

Parameters:

Name Type Description Default
folder Path or str

The folder where to record the file. Default is None.

required
file_name Path or str

The name to record the file under.

required
extension str

The file extension to use. Should be either .ply or .vtk.

required

Returns:

Type Description
Path or None

The proper path to use.

Source code in src/dxtr/io/write.py
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
def format_path_properly(folder: Optional[Path | str], file_name: Path | str, 
                         extension: str) -> Optional[Path]:
    """Makes a valid recording path.

    Parameters
    ----------
    folder : Path or str, optional
        The folder where to record the file. Default is None.
    file_name : Path or str
        The name to record the file under.
    extension : str
        The file extension to use. Should be either `.ply` or `.vtk`.

    Returns
    -------
    Path or None
        The proper path to use.
    """

    try:
        assert isinstance(extension, str), 'Extention must be given as strings.'
        assert extension[0] == '.', 'Extention must start with `.`.'
        assert extension in ['.ply','.vtk'], 'Only .ply & .vtk handled for now.'

    except AssertionError as msg:
        logger.warning(msg)
        return None

    if folder is None:
        folder = Path().cwd()
    elif isinstance(folder, str):
        folder = Path(folder)

    folder.mkdir(parents=True, exist_ok=True)

    if isinstance(file_name, str):
        file_name = Path(file_name)

    path = folder / file_name

    if path.suffix != extension:
        path = path.with_suffix(extension)

    return path

write_ply(top_simplices, points, path, property=None)

Saves computed data structures on disk in the .ply format.

Parameters:

Name Type Description Default
top_simplices list of list of int

The list of vertex indices forming the highest degree simplices.

required
points np.ndarray of float

The coordinates of the vertices.

required
path str or Path

The path where to save the file.

required
property np.array of float

Additional property data to save. Default is None.

None

Returns:

Type Description
None
Source code in src/dxtr/io/write.py
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
def write_ply(top_simplices: list[list[int]], points: np.ndarray[float], 
              path: str | Path, property: Optional[np.array[float]] = None) -> None:
    """Saves computed data structures on disk in the .ply format.

    Parameters
    ----------
    top_simplices : list of list of int
        The list of vertex indices forming the highest degree simplices.
    points : np.ndarray of float
        The coordinates of the vertices.
    path : str or Path
        The path where to save the file.
    property : np.array of float, optional
        Additional property data to save. Default is None.

    Returns
    -------
    None
    """

    top_splcs = top_simplices
    pprty = property

    with open(path, 'w') as file:
        file.write('ply\n')
        file.write('format ascii 1.0\n')
        file.write(f'element vertex {len(points)}\n')
        file.write('property float x\n')
        file.write('property float y\n')
        file.write('property float z\n')
        file.write(f'element face {len(top_splcs)}\n')
        file.write('property list int int vertex_indices\n')

        if pprty is not None: 
            if pprty.dim == 1:
                file.write(f'element edge {len(pprty.values)}\n')
                file.write(f'property int source\n')
                file.write(f'property int target\n')
            file.write(f'property {pprty.type} {pprty.name}\n')

        file.write('end_header\n')

        for pts in points:
            file.write(' '.join([str(x) for x in pts])+'\n')

        if (pprty is None) or (pprty.dim != 2):
            for splx in top_splcs:
                file.write(str(len(splx)) + ' '
                            + ' '.join([str(v) for v in splx]) + ' \n')
        else:
            for splx, pprty_value in zip(top_splcs, pprty.values):
                file.write(str(len(splx)) + ' '
    + ' '.join([str(v) for v in splx]) + ' ' + str(pprty_value) + ' \n')

        if (pprty is not None) and (pprty.dim == 1):
            for splx, pprty_value in enumerate(pprty.values):
                file.write(
        ' '.join([str(v) for v in splx]) + ' ' + str(pprty_value) + ' \n')