Skip to content
  • Search
Skins
  • Light
  • Cerulean
  • Cosmo
  • Flatly
  • Journal
  • Litera
  • Lumen
  • Lux
  • Materia
  • Minty
  • Morph
  • Pulse
  • Sandstone
  • Simplex
  • Sketchy
  • Spacelab
  • United
  • Yeti
  • Zephyr
  • Dark
  • Cyborg
  • Darkly
  • Quartz
  • Slate
  • Solar
  • Superhero
  • Vapor

  • Default (No Skin)
  • No Skin
Collapse

ZMT zurich med tech

  1. Home
  2. Sim4Life
  3. Python API
  4. Exporting data to excel (csv) or matlab (mat)

Exporting data to excel (csv) or matlab (mat)

Scheduled Pinned Locked Moved Python API
6 Posts 3 Posters 1.5k Views
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • R Offline
    R Offline
    rediponi
    wrote on last edited by
    #1

    I have generated a set of simulations and i would be interested to export some information like:
    1 - SAR slice : How to determine the slice number which is closer to a coordinate: lets say at 0cm but due to gridding it corresponds to 0.013 cm.
    2- In the extracted SAR slice I am interested in the SAR values in a line along for example Y-axis. Again i know the absolute value of the line but i dont know exactly to which coordinate would correspond due to gridding.

    I generated a python script of a manually extracted line, then i run the generated script, but it gives errors.

    Could you show an example script of generating and exporting the SAR slice and a line in that slice?

    SylvainS 1 Reply Last reply
    0
    • R rediponi

      I have generated a set of simulations and i would be interested to export some information like:
      1 - SAR slice : How to determine the slice number which is closer to a coordinate: lets say at 0cm but due to gridding it corresponds to 0.013 cm.
      2- In the extracted SAR slice I am interested in the SAR values in a line along for example Y-axis. Again i know the absolute value of the line but i dont know exactly to which coordinate would correspond due to gridding.

      I generated a python script of a manually extracted line, then i run the generated script, but it gives errors.

      Could you show an example script of generating and exporting the SAR slice and a line in that slice?

      SylvainS Offline
      SylvainS Offline
      Sylvain
      ZMT
      wrote on last edited by
      #2
      1. You need teh function FindCell (see example below)
      2. You also need FindCell, together with raw access to the underlying numpy data array and a way to "reshape" it properly (see same example below)
      	# Creating the analysis pipeline
      	# Adding a new SimulationExtractor
      	simulation = document.AllSimulations["SCC34 Benchmark Simulation"]
      	simulation_extractor = simulation.Results()
      
      	# Adding a new EmSensorExtractor
      	em_sensor_extractor = simulation_extractor["Overall Field"]
      	em_sensor_extractor.FrequencySettings.ExtractedFrequency = u"All"
      	em_sensor_extractor.SurfaceCurrent.SurfaceResolution = 0.001, units.Meters
      	document.AllAlgorithms.Add(em_sensor_extractor)
      
      
      	
      	# Get data object
      	em_sensor_extractor.Outputs["SAR(x,y,z,f0)"].Update()  # output needs to be updated before extracting data
      	sar = em_sensor_extractor.Outputs["SAR(x,y,z,f0)"].Data
      	# a location in space:
      	x = 0
      	y = -0.00910
      	z = -0.00025
      	# get grid cell indices closest to that location:
      	id_x, id_y, id_z = sar.Grid.FindCell(x,y,z)
      	# Create Slice Viewer at that location
      	
      
      	inputs = [em_sensor_extractor.Outputs["SAR(x,y,z,f0)"]]
      	slice_field_viewer = analysis.viewers.SliceFieldViewer(inputs=inputs)
      	slice_field_viewer.Data.Mode = slice_field_viewer.Data.Mode.enum.QuantityRealModulus
      	slice_field_viewer.Slice.Index = id_z
      	slice_field_viewer.UpdateAttributes()
      	document.AllAlgorithms.Add(slice_field_viewer)
      	
      	# Get raw data array (in the form of a numpy array)
      	sar_array = em_sensor_extractor.Outputs["SAR(x,y,z,f0)"].Data.Field(snapshot_index=0)
      	nx, ny, nz = numpy.array(sar.Grid.Dimensions) - numpy.array((1,1,1))  # grid is defined on nodes, SAR data is in cell centers
      	sar_array = numpy.reshape(sar_array, (nx, ny, nz), order='F')
      	# now data can be accessed easily:
      	print sar_array[:, id_y, id_z]
      R 1 Reply Last reply
      0
      • SylvainS Sylvain
        1. You need teh function FindCell (see example below)
        2. You also need FindCell, together with raw access to the underlying numpy data array and a way to "reshape" it properly (see same example below)
        	# Creating the analysis pipeline
        	# Adding a new SimulationExtractor
        	simulation = document.AllSimulations["SCC34 Benchmark Simulation"]
        	simulation_extractor = simulation.Results()
        
        	# Adding a new EmSensorExtractor
        	em_sensor_extractor = simulation_extractor["Overall Field"]
        	em_sensor_extractor.FrequencySettings.ExtractedFrequency = u"All"
        	em_sensor_extractor.SurfaceCurrent.SurfaceResolution = 0.001, units.Meters
        	document.AllAlgorithms.Add(em_sensor_extractor)
        
        
        	
        	# Get data object
        	em_sensor_extractor.Outputs["SAR(x,y,z,f0)"].Update()  # output needs to be updated before extracting data
        	sar = em_sensor_extractor.Outputs["SAR(x,y,z,f0)"].Data
        	# a location in space:
        	x = 0
        	y = -0.00910
        	z = -0.00025
        	# get grid cell indices closest to that location:
        	id_x, id_y, id_z = sar.Grid.FindCell(x,y,z)
        	# Create Slice Viewer at that location
        	
        
        	inputs = [em_sensor_extractor.Outputs["SAR(x,y,z,f0)"]]
        	slice_field_viewer = analysis.viewers.SliceFieldViewer(inputs=inputs)
        	slice_field_viewer.Data.Mode = slice_field_viewer.Data.Mode.enum.QuantityRealModulus
        	slice_field_viewer.Slice.Index = id_z
        	slice_field_viewer.UpdateAttributes()
        	document.AllAlgorithms.Add(slice_field_viewer)
        	
        	# Get raw data array (in the form of a numpy array)
        	sar_array = em_sensor_extractor.Outputs["SAR(x,y,z,f0)"].Data.Field(snapshot_index=0)
        	nx, ny, nz = numpy.array(sar.Grid.Dimensions) - numpy.array((1,1,1))  # grid is defined on nodes, SAR data is in cell centers
        	sar_array = numpy.reshape(sar_array, (nx, ny, nz), order='F')
        	# now data can be accessed easily:
        	print sar_array[:, id_y, id_z]
        R Offline
        R Offline
        rediponi
        wrote on last edited by
        #3
        This post is deleted!
        1 Reply Last reply
        0
        • R Offline
          R Offline
          rediponi
          wrote on last edited by
          #4

          Thank you Sylvain.
          Could you possibly add one example of code writing array (for the line) or matrix ( for the slice ) using the Matlab export ?

          1 Reply Last reply
          0
          • SylvainS Offline
            SylvainS Offline
            Sylvain
            ZMT
            wrote on last edited by
            #5

            Currently the Matlab exporter only allows exporting a whole quantity (i.e. the whole 3D array if it is a 3D quantity, like the SAR(x,y,z,f0)).
            Once exported, though, it is relatively straightforward to slice through the array in Matlab. See for example this post: https://forum.zurichmedtech.com/topic/47/exporting-data-to-matlab-how-to-access-the-data-as-a-volume-matrix

            1 Reply Last reply
            0
            • AntoninoMCA Offline
              AntoninoMCA Offline
              AntoninoMC
              wrote on last edited by
              #6

              Dear Redi, you can export arbitrary data to Matlab (e.g. field array and grid coordinates) using the Scipy.io library. A generic use of it is the following:

              import scipy.io as sio
              dict={'field':fieldarray,'xgrid':xgrid,'ygrid':ygrid,'zgrid':zgrid}
              sio.savemat(filename,dict)

              where xgrid,ygrid,zgrid are the vectors containing the cartesian coordinates of the grid.

              1 Reply Last reply
              1
              Reply
              • Reply as topic
              Log in to reply
              • Oldest to Newest
              • Newest to Oldest
              • Most Votes


              • Login

              • Don't have an account? Register

              • Login or register to search.
              • First post
                Last post
              0
              • Search