Skip to content

Python API

Scripting interface for Sim4Life

138 Topics 432 Posts
  • Is it possible to use Python API without opening Sim4Life?

    4
    0 Votes
    4 Posts
    1k Views
    brynB
    You can open a smash file using import s4l_v1 as s4l s4l.document.Open(smash_file_path) You can save it using s4l.document.Save(smash_file_path) You can find a simulation using (assuming you have unique simulation names sim = [sim for sim in s4l.document.AllSimulations if sim.Name == "the name of your simulation"][0] If you want to load different models (from .sab file or .smash), but don't need to load e.g. simulations (in .smash file), you can import the file entities = s4l.model.Import(file_path) if you need to reset the document or model because you are accumulating too many entities and memory use is getting too high, you can use e.g. # reset the model sl4.model.Clear() # or keep entities, but discard model history import XCoreModeling as xcm xcm.GetActiveModel().ClearHistory() # or reset entire document (reset model, simulations, analysis) s4l.document.New()
  • Location of Simulation Outputs and Voxel Coordinates?

    1
    0 Votes
    1 Posts
    257 Views
    No one has replied
  • About TI Simulation

    4
    0 Votes
    4 Posts
    819 Views
    L
    I want to see the impact of different grid sizes on the simulation, so I cloned a pair of simulations and modified the maximum step size of the grid. However, I found that if I create a new pair of simulations using the same settings, the grid sizes, simulation durations, and results of the new simulations and the cloned simulations are all different. Why does this happen?
  • Iso Surfaces - Creating volumes with a threshold

    1
    0 Votes
    1 Posts
    259 Views
    No one has replied
  • Accessing specific entities of my model from the python API

    2
    1 Votes
    2 Posts
    479 Views
    L
    found my own answer: [image: 1730811141668-a0bbc3ce-5488-4bea-8af6-95ae9439824b-image.png]
  • Installing Additional Python Packages in Sim4Life Environment

    Solved python pip
    9
    1 Votes
    9 Posts
    2k Views
    G
    @Sylvain Yes thanks finally I succeed!
  • 1 Votes
    1 Posts
    254 Views
    No one has replied
  • Clear or Reset Geometry Workspace in Python?

    python geometry
    4
    0 Votes
    4 Posts
    1k Views
    brynB
    I typically do the following (which also clears the history and frees memory): import XCoreModeling as xcm xcm.GetActiveModel().Clear()
  • RuntimeError: Could not allocate memory for numpy array

    python numpy allocation
    3
    0 Votes
    3 Posts
    2k Views
    SylvainS
    have you tried overwriting the E_field and raw_data variables?
  • Extract the EM E field along the Spline

    3
    0 Votes
    3 Posts
    864 Views
    A
    Thank you so much for hour help with the technical issue! Your assistance made a big difference, and I truly appreciate your expertise and prompt support!
  • Max modulation tool in API

    2
    0 Votes
    2 Posts
    763 Views
    J
    Would like to know too in 2024 :)
  • Change parameters in Simulation Combiner

    2
    0 Votes
    2 Posts
    706 Views
    C
    The following example for a 2-port simulation combiner should help. i = 0 for channel in em_multi_port_simulation_combiner.GetChannelWeights(): power = [1.0, 2.0] phase = [45, 90] em_multi_port_simulation_combiner.SetChannelWeight(channel, power[i], phase[i]) i += 1 em_multi_port_simulation_combiner.UpdateAttributes() em_multi_port_simulation_combiner.Update() document.AllAlgorithms.Add(em_multi_port_simulation_combiner)
  • Geometry Modeling - Snapping to Endpoints in Python API ?

    Solved python
    3
    0 Votes
    3 Posts
    1k Views
    brynB
    Hi @dbsim4 I think it would help a lot if you could post an image depicting what you are trying to achieve. Answering the question from the subject line: No, there is no snapping in Python (not sure how that API could look like), but there are functions to get the distance between entities (and corresponding closest points), which may help. brick1 = XCoreModeling.CreateSolidBlock(Vec3(0), Vec3(1)) brick2 = XCoreModeling.CreateSolidBlock(Vec3(2), Vec3(3)) res = XCoreModeling.GetEntityEntityDistance(brick1, brick2) print(f"Distance brick1-brick2: {res[0].Distance}") print(f"Closest point on brick1: {res[0].ClosestPosition}") print(f"Closest point on brick2: {res[1].ClosestPosition}") or distance to a point: brick = XCoreModeling.CreateSolidBlock(Vec3(0), Vec3(1)) res = XCoreModeling.GetEntityEntityDistance(brick, Vec3(3)) print(f"Distance brick-point: {res.Distance}") print(f"Closest point on brick: {res.ClosestPosition}") For geometry that has end-points or corners, you could extract the vertices and again use distance wrt some other point as a way to write a script. edge = XCoreModeling.CreateEdge(Vec3(0), Vec3(1)) vertices = XCoreModeling.GetVertices(edge) assert len(vertices) == 2 for v in vertices: print(v.Position) brick = XCoreModeling.CreateSolidBlock(Vec3(0), Vec3(1)) vertices = XCoreModeling.GetVertices(edge) assert len(vertices) == 8
  • How to create solid circle ie. with surface ?

    Solved
    4
    0 Votes
    4 Posts
    969 Views
    brynB
    @dbsim4 regarding your "Update", I am not sure I understand the context. Are you trying to assign the circle loop as an edge source?
  • Refresh Matlab Expoter

    2
    0 Votes
    2 Posts
    702 Views
    brynB
    Like all entities in the Analysis pipeline, the Matlab exporter has an Update method to execute. to find more information on a specific class you can export the pipeline from the GUI as a python script use the help() function to get more information on a specific class, e.g. import s4l_v1 as s4l help(s4l.analysis.exporters.MatlabExporter)
  • Interaction with Neuron results

    2
    0 Votes
    2 Posts
    698 Views
    AntoninoMCA
    @Hüfer yes, you can access all the simulation results as well as the parameters of you EM-neuronal simulations using the Python interface. For example, you can access titration data, or the transmembrane voltage profiles stored on a point or line sensor data, etc. or you can dynamically change the stimulation pulse shape parameters (phase duration, amplitude, etc.). Sim4Life provides you already with some Python tutorials. There are at least 3 tutorials provided that show how to use Python to setup a T-Neuron simulation, how to run it and how to access the simulation results. Please have a look at these tutorials to learn Sim4Life programming (if you are a Python expert, learning will take you a day!). To access these tutorials, open the 'Scripter' panel in Sim4Life, and load one of the scripts in the folder opened when selecting "Open Example Script" clicking on the open folder button.
  • multiport sensor combiner freq normalization

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

    1
    0 Votes
    1 Posts
    1 Views
    No one has replied
  • clear simulation

    2
    0 Votes
    2 Posts
    638 Views
    H
    Hi Michael, What type of simulation/solver you are running? If I understand correct, all the simulations are of the same size, but the output files are of different sizes? This should not be the case. Unless simulations have larger grids, more sensors recorded, more frequency snapshots, etc, the output file size must not change. Best, Habib
  • Specifying diameter during axon discretization

    2
    0 Votes
    2 Posts
    638 Views
    L
    I have managed to solve this. The function below seems to work def DiscretizeAxonModel(Axon_name, Diameter, type,folder): axon_entity = model.AllEntities()[Axon_name] if type=='motor': model_properties=model.MotorMrgNeuronProperties() elif type=='sensory': model_properties=model.SensoryMrgNeuronProperties() else: model_properties=model.MotorNeuronProperties() model_properties.AxonDiameter=Diameter discretized_axon = model.CreateAxonNeuron(axon_entity,model_properties) discretized_axon.Name = Axon_name +'_neuron' folder.Add(discretized_axon)