Skip to content

Python API

Scripting interface for Sim4Life

119 Topics 337 Posts
  • Python code does not work with S4L v.6.0

    2
    0 Votes
    2 Posts
    225 Views
    PeterStijnmanP

    It might be that they changed the sim4life python module between the two versions. You could check the functions you call from the sim4life module in the API browser. If that doesn't work, I would suggest posting the code (or a minimal working example) here so people can look more specifically.

  • This topic is deleted!

    1
    0 Votes
    1 Posts
    2 Views
    No one has replied
  • Forcing imported modules to refresh after updating

    2
    0 Votes
    2 Posts
    510 Views
    M

    This is common. Once you import a script, it becomes cached so even if you make changes to it and save it, python won't see the changes. You need to reload it as you said if you make changes to the imported script after running your main script.

  • Applying smoothing to a mesh with the API

    1
    0 Votes
    1 Posts
    171 Views
    No one has replied
  • 0 Votes
    2 Posts
    761 Views
    SylvainS

    This looks quite good, thank you for providing your scripts.
    Note that, for the second method, you could create your cylinder directly at the desired position:

    cylinder2 = model.CreateSolidCylinder(Vec3(0,1,-15),Vec3(0,1,-15),0.5)

    There is also the XCoredModeling.CoverWires() function that allows you to make a surface (i.e. face) out of a circle entity.

    Last, but not least, note that the line model_to_grid_filter.MaximumEdgeLength is ultimately what determines the tradeoff between accuracy and computational cost of the interpolation (since it defines the resolution of the triangulated mesh on which the interpolation is done).

  • Simulation results show as nan+nanj

    4
    0 Votes
    4 Posts
    541 Views
    L

    Oh that worked! Thank you 🙂

  • plot a specific parameter on a surface

    1
    1 Votes
    1 Posts
    194 Views
    No one has replied
  • 1 Votes
    3 Posts
    376 Views
    J

    Thank you for your prompt reply! That's helped a lot, I didn't know how to implement the 'magnetic_energy_evaluator.Outputs["Magnetic Energy"].Data' or the 'mag_data.GetComponent(0)[0]' lines.
    I realise now that I probably should have posted the code which I had attempted already. Thanks for helping me anyway!

  • Acoustic simulation - phase information needed (using Python)

    11
    1 Votes
    11 Posts
    1k Views
    M

    You need to make output_angle into an array and append the new values at the end every time (the i-th element will have the i-th phase you want)

    output_angle = numpy.array([]) for i in range(0,128): # your code output_angle = numpy.append(output_angle, numpy.angle(output))
  • This topic is deleted!

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

    1
    0 Votes
    1 Posts
    5 Views
    No one has replied
  • Add a material to the Materials Database

    1
    0 Votes
    1 Posts
    252 Views
    No one has replied
  • use vtk on entities in s4l enviroment

    1
    0 Votes
    1 Posts
    182 Views
    No one has replied
  • Extract voxel information

    6
    0 Votes
    6 Posts
    712 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
    635 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
    184 Views
    No one has replied
  • Memory Problem with Python Scripts

    1
    0 Votes
    1 Posts
    264 Views
    No one has replied
  • 0 Votes
    5 Posts
    793 Views
    SylvainS

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

  • Creating a 2D plot via Python API

    2
    0 Votes
    2 Posts
    986 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
    406 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.