Python Forum
saving each iteration of a loop - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: saving each iteration of a loop (/thread-34380.html)



saving each iteration of a loop - sgcgrif3 - Jul-26-2021

I have a code which is converting between file types. I have 10 .vtp files and I am converting these to .mat files.
My code works, but it only saves the .mat file of the last iteration of the loop, so instead of having 10 files I just get one.

Could anybody help me with this?
import vtk
from vtk.util.numpy_support  import vtk_to_numpy
import numpy as np
import scipy.io as sio

c = 0

out = {}

starts = 0
ends = 11

for i in range(starts,ends):

# File to read
	fname = 'RP_DATA_%04.0f.vtp'%i

	# --- read a vtp file ---
	points = vtk.vtkXMLPolyDataReader() 
	points.SetFileName(fname)
	points.Update()

	# print the arrays out
	data = points.GetOutput()
	point_data = data.GetPointData()

import inspect
print(inspect.getdoc(point_data))

vel = vtk_to_numpy(point_data.GetAbstractArray('Velocity'))
points = vtk_to_numpy(data.GetPoints().GetData())
	
c = c+1;
	
name = f"out_{c}.mat"
sio.savemat(name, {'vel':vel, 'points':points})
print(i)



RE: saving each iteration of a loop - Larz60+ - Jul-26-2021

your file save is outside of loop, in fact everything after line 25 is outside of loop.

FYI: move all imports to top of file, it looks better, and this is where programmers expect to find them


RE: saving each iteration of a loop - sgcgrif3 - Jul-27-2021

(Jul-26-2021, 04:39 PM)Larz60+ Wrote: your file save is outside of loop, in fact everything after line 25 is outside of loop.

FYI: move all imports to top of file, it looks better, and this is where programmers expect to find them

Please could you tell me where to move this to? I realised it was outside of the loop but when I try to adapt this it gives me errors.


RE: saving each iteration of a loop - DeaD_EyE - Jul-27-2021

import vtk
from vtk.util.numpy_support import vtk_to_numpy
import numpy as np
import scipy.io as sio
 

files = 10

for index in range(files):

    file_in = f"RP_DATA_{index:04d}.vtp"
    # RP_DATA_0000.vtp, RP_DATA_0001.vtp, ..., RP_DATA_0009.vtp | END
    
    file_out = f"out_{index}.mat"
    # out_0.mat, out_1.mat, ..., out_9.mat | END  

    points = vtk.vtkXMLPolyDataReader() 
    points.SetFileName(file_in)
    points.Update()
    
    data = points.GetOutput()
    point_data = data.GetPointData()

    vel = vtk_to_numpy(point_data.GetAbstractArray('Velocity'))
    points = vtk_to_numpy(data.GetPoints().GetData())

    sio.savemat(file_out, {'vel':vel, 'points':points})
I can't test the code.


Use string formatting or f-strings to substitute text and not the old % formatting. It's still supported, but should not used.
If you want to convert all existing files of a directory:

import time
from pathlib import Path


def convert_vtk(source, target):
    """
    your code here to:

    1) Load the source
       Here is maybe a conversion of source required.
       Not all libraries can handle Path objects, so they must
       converted to a str()

       Example: points.SetFileName(str(source))

    2) Get Data out

    3) Save data to target
       scipy.io.savemat should handle the Path object right
       so there is no conversion to a str required
    """
    time.sleep(0.5)
    print(f"Converting {source.name} to {target.name}")
    # simulating the creation of target file
    target.touch()


def convert_all(root):
    for source in Path(root).glob("*.vtk"):
        if not source.is_file():
            # skipping it it's not a regular file e.g. a directory
            continue

        # create a Path instance with the same name, but different suffix
        target = source.with_suffix(".mat")

        # Condition to skip a source, if target already exists
        if target.exists():
            continue

        convert_vtk(source, target)


convert_all(Path.home() / "test_vtk")
Docs of Pathlib: https://docs.python.org/3/library/pathlib.html