Skip to content

Python API

Scripting interface for Sim4Life

126 Topics 371 Posts
  • Geometry Modeling - Snapping to Endpoints in Python API ?

    Solved
    3
    0 Votes
    3 Posts
    397 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
    411 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
    362 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
    390 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
    227 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
    343 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
    320 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)
  • This topic is deleted!

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

    1
    0 Votes
    1 Posts
    8 Views
    No one has replied
  • read output files

    3
    0 Votes
    3 Posts
    480 Views
    M

    solved with h5py read :
    import h5py

    with h5py.File(fname,'r') as hdf:
    FieldGroups=hdf['FieldGroups']
    keygroup = list(FieldGroups.keys())
    valField = FieldGroups[keygroup[0]]
    AllFields = valField['AllFields']
    EMPotential = AllFields['EM Potential(x,y,z,f0)']
    EMPotentialObject = EMPotential['_Object']
    AdditionalFieldDataCollection =
    EMPotentialObject['AdditionalFieldDataCollection']
    Snapshots = EMPotentialObject['Snapshots']
    Potential = Snapshots['0']['comp0']

    #(n, m, k, 2) numpy array PotentialData = Potential[()]

    ==========
    this can be plotted
    import pyvista as pv

  • How to export voxel data using python API?

    3
    0 Votes
    3 Posts
    642 Views
    M

    @montanaro I've tried it and it worked. Thank you very much!

  • one document with many simulations

    5
    0 Votes
    5 Posts
    518 Views
    M

    can you supply some code example ? thanks

  • Masking entities in python

    6
    0 Votes
    6 Posts
    714 Views
    brynB

    @Spuky sorry, I guess the legacy code was confusing:

    masking_filter = s4l.analysis.core.FieldMaskingFilter() masking_filter.SetInputConnection(0, J_port)

    I edited the original answer to use the current API. The J_port was the output port for the J(x,y,z,f0) field.

  • Deleting Entities by Name

    3
    0 Votes
    3 Posts
    394 Views
    E

    @AntoninoMC thanks, that worked perfect!

  • Delete Simulation and Results

    13
    0 Votes
    13 Posts
    1k Views
    L

    Hmm I am not sure I understand. Is it possible to share more of your code and explain exactly what is not working?

    I use the lines of code you posted regularly and it works just fine.

  • How to Change Material Properties with Python?

    Solved
    5
    1 Votes
    5 Posts
    659 Views
    M

    Mesh_material = sim.AddMaterialSettings(themesh(fromstl))
    Mesh_material.Name = yourname
    Mesh_material.ElectricProps.Conductivity = v # S/m

  • What is the proper workflow for generating unstructured/multidomain meshes?

    Solved
    2
    0 Votes
    2 Posts
    279 Views
    D

    Nevermind, I found example code in the "tutorial_emlf_unstructured_neuron_electroceuticals.py" example file, see below:

    def CreateMesh(): """ Creates a Multidomain Mesh with predefined priorities between domains and specified global and local options """ sel=.05 # Suface Edge Length mel=.002 # Min Edge Length #create multi-domain mesh global_opt = xcm.GlobalUnstructuredMeshingOptions() global_opt.SurfaceEdgeLength = sel global_opt.MinEdgeLength = mel # Gets all the entities in the folder 'Nerve_Model' entities=s4l.model.AllEntities()['Nerve_Model'].Entities local_opt_dict={} for ent in entities: local_opt = xcm.LocalUnstructuredMeshingOptions() print((ent, ent.Name)) if 'Saline' in ent.Name : local_opt.Priority=0 elif 'Silicone' in ent.Name : local_opt.Priority=1 elif 'Electrode' in ent.Name : local_opt.Priority=2 elif 'Nerve' in ent.Name : local_opt.Priority=3 elif 'Interstitial' in ent.Name : local_opt.Priority=4 elif 'Connective' == ent.Name : local_opt.Priority=5 elif 'Connective_' in ent.Name : local_opt.Priority=5 elif 'Blood' in ent.Name : local_opt.Priority=6 elif 'Fascicle' in ent.Name : local_opt.Priority=7 local_opt_dict[ent]=local_opt print((local_opt.Priority)) # Creates an unstructured mesh on the domains using the defined options mesh = xcm.GenerateUnstructuredMesh(entities, global_opt, local_opt_dict) return

    I did have a followup, how do we optimize the surface edge length and minimum edge lengths?

  • cant assign entities

    3
    0 Votes
    3 Posts
    478 Views
    M

    really thanks this is direction of solution
    what i did with friend

    Mesh2 = s4l.model.Import(r'C:\sintetic data\bones.stl')[0]
    so now Mesh2 is not tuple and has its own name bones meanwhile i write all in one script without functions so i dont need realy the entities as in the example of 2 plates supplied with sim4life ...
  • How to Suppress an analysis exporter prompt in the Console Window

    2
    0 Votes
    2 Posts
    245 Views
    SylvainS

    Hi, unfortunately most algorithms (this one included) do not have the option to mute their info messages. So I don't think there is a way to suppress this kind of output in the console.
    As for the possible options for the Update() function, there is only one: overwrite=True/False, to determine the behavior of the exporter if the target file already exists. The Update() function is the equivalent of clicking the Refresh button in the algorithm in the GUI: it triggers the algorithm.
    If the API Browser is lacking, you can find more information (like the function signature) by typing e.g.

    help(excel_exporter.Update)

    in the Console.