Python Forum
saving each iteration of a loop
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
saving each iteration of a loop
#4
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
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Messages In This Thread
saving each iteration of a loop - by sgcgrif3 - Jul-26-2021, 03:55 PM
RE: saving each iteration of a loop - by Larz60+ - Jul-26-2021, 04:39 PM
RE: saving each iteration of a loop - by sgcgrif3 - Jul-27-2021, 11:06 AM
RE: saving each iteration of a loop - by DeaD_EyE - Jul-27-2021, 01:02 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Inconsistent loop iteration behavior JonWayn 2 997 Dec-10-2022, 06:49 AM
Last Post: JonWayn
  Simple Variable Saving in Loop DevDev 3 3,009 Mar-09-2021, 07:17 PM
Last Post: Fre3k
  String slicing and loop iteration divyansh 9 4,723 Jun-07-2020, 10:29 PM
Last Post: divyansh
  Changing a variable's name on each iteration of a loop rix 6 84,655 Jan-03-2020, 07:06 AM
Last Post: perfringo
  Parallel iteration with for loop Josh_Python890 1 2,166 Jul-19-2019, 11:50 PM
Last Post: metulburr
  Multiprocessing my Loop/Iteration (Try...Except) Jompie96 7 4,579 Jun-19-2019, 12:59 PM
Last Post: noisefloor
  First for loop stops after first iteration Divanova94 10 8,870 May-01-2019, 04:27 PM
Last Post: buran
  issue with updating list every iteration of a loop ftrillaudp 2 3,057 Oct-29-2018, 03:23 AM
Last Post: ftrillaudp
  exception during iteration loop Skaperen 6 3,879 Oct-24-2018, 12:35 AM
Last Post: Skaperen
  For Loop, execute one time for every loop iteration dragan979 2 4,428 Feb-20-2018, 12:02 PM
Last Post: dragan979

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020