Skip to content

Python API

Scripting interface for Sim4Life

126 Topics 371 Posts
  • Extract voxel information

    6
    0 Votes
    6 Posts
    790 Views
    W

    I figured it out, made it in the gui and used to python to see what the syntax had to be

    inputs = [field_masking_filter.Outputs["EM E(x,y,z,f0)"]]
  • Accessing the results of the simulation via python scripting

    7
    0 Votes
    7 Posts
    689 Views
    M

    Not sure what you're trying to do, but some sample code to extract the field and data is below (should apply to anything with a rectilinear grid, but it's from Acoustic simulations):

    sim = document.AllSimulations[simulation_idx] simulation_extractor = sim.Results() sim_sensor_extractor = simulation_extractor["Overall Field"] # Sensor Name sim_field_extractor = sim_sensor_extractor.Outputs["Intensity"] # Field Name sim_field_extractor.Update() d_sim = sim_field_extractor.Data sim_ref_field_extractor.Update() out = d_sim_ref.Field(0) # 3D Field is extracted as a 1D array. # 0 corresponds to the snapshot (typically, time or frequency) # If you need this as a 3D array then use # out_3d = np.reshape(out, grid_dims, order='F') # once you have 'grid_dims' which I show how to get below # important to remember order = 'F' because of the way that 3D arrays are unrolled into 1D (Fortran style) sim_field_extractor.Update() # Just to be safe, I add this update step a few more times than needed # Get grid: axes, dimensions x = d_sim.Grid.XAxis y = d_sim.Grid.YAxis z = d_sim.Grid.ZAxis # Careful: Simulations in S4L have grids defined at the nodes, and values (the field) defined at cell centers # Therefore if you want the 'grid' used for the values in your field, you'll need to probably get the midpoints of the grid and reduce the grid dimensions by one in each direction. # Something like: x_mid = (x[1:]+x[:-1]) / 2 grid = d_sim_ref.Grid.Dimensions
  • Creating a Custom Menu with Actions

    1
    0 Votes
    1 Posts
    209 Views
    No one has replied
  • Memory Problem with Python Scripts

    1
    0 Votes
    1 Posts
    272 Views
    No one has replied
  • 0 Votes
    5 Posts
    851 Views
    SylvainS

    @CEiber That's a very good idea, thanks a lot!

  • Creating a 2D plot via Python API

    2
    0 Votes
    2 Posts
    1k Views
    SylvainS

    Hi, you get an error because your input is a 3D field (EM E(x,y,z,f0)) and this viewer expects 1D data (e.g. E as a function of x).
    To address this, you can use the 1D Field Filter (under Field Tools in the GUI) to first extract one-dimensional data out of your 3D field, and then connect the Plot viewer. Do it first in the GUI, then use To-Python function in the context menu to create your script.

    I hope this helps.

  • Extracting neuron simulation results

    2
    0 Votes
    2 Posts
    419 Views
    J

    To update, I've learned the membrane voltage, membrane current, and extracellular voltage can be specified as the measured quantity for a line sensor, with the number of time snapshots of your choosing. Thus, all info to calculate the induced potential on the electrode over time from neural activity is available, one just has to create multiple, identical spline entities as only one quantity can be sensed from a structure at at time (at least in the version of 5.0 that I'm using).

    This also helps get a relative idea of where the action potential initiates along the axon relative to the electrode, though not an exact X/Y/Z location to show in the model view.

  • How can I choose "Distribute Along Line = True"?

    3
    0 Votes
    3 Posts
    457 Views
    A

    Yes, this has solved the issue. Thanks a lot Silvain!

  • This topic is deleted!

    1
    0 Votes
    1 Posts
    2 Views
    No one has replied
  • This topic is deleted!

    1
    0 Votes
    1 Posts
    45 Views
    No one has replied
  • Missing psSAR in SAR statistics table when using Python API

    1
    3 Votes
    1 Posts
    852 Views
    No one has replied
  • How to Create Disks in Implant Safety Tool with Python

    1
    0 Votes
    1 Posts
    191 Views
    No one has replied
  • Continuing Simulation and choosing Input Simulation

    4
    0 Votes
    4 Posts
    362 Views
    A

    For those who are interested, I have found a way to work around the issue. The python-command to continue a Simulation called:

    thermal_initial_global.InitializationOptions.enum.ContinueSimulation

    can only take the first argument, which in my case is "heating 0". So if you now have created "heating 1" based on "heating 0", and then proceed to delete "heating 0", the only available argument is "heating 1". This will enable you to further continue your simulation. You can delete a simulation using the following command:

    document.AllSimulations.Remove(thermal_sim)

    given you have returned thermal_sim, of course. If you create an empty list, you can append your thermal_sim's into it and then choose which item you want to delete.

    I am sure there are more elegant methods, but if you need to do this specific task, it works.

  • how to normalize in Simulation Combiner ?

    2
    0 Votes
    2 Posts
    290 Views
    A

    This might be an old question, but I have just stumbled accross this problem and solution recently. Someone reading this in the future might find it helpful. The code you were looking for is the following:

    #Sources heat_source = thermal_sim.AddHeatSourceSettings(em_sim.OverallFieldSensor) heat_source.NormalizeToInputPower = True heat_source.PowerScaleFactor = 6, units.Watts

    Assuming you want 6W of power, of course.

  • 1 Votes
    2 Posts
    301 Views
    N

    The problem has to do with (to me) unlogical ordering of the 'weights'-dict. I have made a workaround which extracts the values from the dict in alphabetical order of the keys, which seems to work.

  • Creating a simulation batch file for multiport simulation

    2
    0 Votes
    2 Posts
    383 Views
    SylvainS

    This should work:

    [outputname.replace('Output', 'Input') for outputname in [sim.GetOutputFileName(i) for i in range(sim.NumberOfResults)]]

    however, I could not find a way to programmatically generate the input files themselves, in case they are missing.

    First of all, the WriteInputFile() function is only available via sim.raw.WriteInputFile() (instead of simply sim.WriteInputFile()) and it only generates the input file for the first port (which is probably a bug in the API).

    One possible workaround (albeit not entirely satisfactory) is to call sim.RunSimulation(wait_for_submission=True) for all the multiport simulations, which will ensure that all input files are generated and all simulations are enqueued in ARES. You can then manually kill all of these submitted jobs from the Task manager of Sim4Life....

  • Xtracting S11 data via python

    5
    1 Votes
    5 Posts
    527 Views
    SylvainS

    Note: I would even recommend to raise an exception if the pipeline fails to update, before continuing with the data processing. This makes the scripts easier to maintain in the long run:

    S11 = simEM_results["EdgeSource (Dipole)"]["Reflection Coefficient(f)"] assert S11.Update(), 'Failed to update pipeline' S11_data = S11.Data.GetComponent(0)
  • 1 Votes
    1 Posts
    612 Views
    No one has replied
  • Extracting a set of Voltage Value without Voltage Reader

    1
    0 Votes
    1 Posts
    239 Views
    No one has replied
  • Exporting data to excel (csv) or matlab (mat)

    6
    0 Votes
    6 Posts
    1k Views
    AntoninoMCA

    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.