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
#1
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)
Yoriz write Jul-26-2021, 04:05 PM:
Please post all code, output and errors (in their entirety) between their respective tags. Refer to BBCode help topic on how to post. Use the "Preview Post" button to make sure the code is presented as you expect before hitting the "Post Reply/Thread" button.
Reply
#2
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
Reply
#3
(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.
Reply
#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
My code examples are always for Python >=3.6.0
Almost dead, but too lazy to die: https://sourceserver.info
All humans together. We don't need politicians!
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Simple Variable Saving in Loop DevDev 3 877 Mar-09-2021, 07:17 PM
Last Post: Fre3k
  String slicing and loop iteration divyansh 9 2,017 Jun-07-2020, 10:29 PM
Last Post: divyansh
  Changing a variable's name on each iteration of a loop rix 6 27,754 Jan-03-2020, 07:06 AM
Last Post: perfringo
  Parallel iteration with for loop Josh_Python890 1 1,117 Jul-19-2019, 11:50 PM
Last Post: metulburr
  Multiprocessing my Loop/Iteration (Try...Except) Jompie96 7 2,144 Jun-19-2019, 12:59 PM
Last Post: noisefloor
  First for loop stops after first iteration Divanova94 10 4,279 May-01-2019, 04:27 PM
Last Post: buran
  issue with updating list every iteration of a loop ftrillaudp 2 1,534 Oct-29-2018, 03:23 AM
Last Post: ftrillaudp
  exception during iteration loop Skaperen 6 1,773 Oct-24-2018, 12:35 AM
Last Post: Skaperen
  For Loop, execute one time for every loop iteration dragan979 2 2,681 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