Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions examples/article_figures/fig1_hiv_examples.py
Original file line number Diff line number Diff line change
Expand Up @@ -114,6 +114,7 @@
central_axis=False,
use_dol=True,
axis_object = ax,
random_seed=seed
)

filename = my_experiment.date_as_string + 'vlab4mic_hiv_antibody.png'
Expand Down
4 changes: 2 additions & 2 deletions examples/article_figures/fig3_abc_panels.py
Original file line number Diff line number Diff line change
Expand Up @@ -101,7 +101,7 @@ def create_parameter_grid_as_list(h_positions, v_positions, margin=0.1, min_z =
ax.set_xticklabels([])
ax.set_yticklabels([])

filename = myexperiment2.date_as_string + 'vlab4mic_fig3_panelA.png'
filename = myexperiment2.date_as_string + 'vlab4mic_fig3_panelB.png'
filename2 = os.path.join(myexperiment2.output_directory, filename)
fig.savefig(filename2,dpi=300, bbox_inches='tight')
plt.close()
Expand Down Expand Up @@ -153,7 +153,7 @@ def create_parameter_grid_as_list(h_positions, v_positions, margin=0.1, min_z =
myexperiment2.particle.gen_axis_plot(axis_object=ax4, with_sources=False, source_plotsize=0, source_plotmarker="o", view_init=[90,0,0],
xlim=[0,1000], ylim=[0,1000], zlim=[0,600], emitter_plotsize=1)

filename = myexperiment2.date_as_string + 'vlab4mic_fig3_panelB.png'
filename = myexperiment2.date_as_string + 'vlab4mic_fig3_panel_AC.png'
filename2 = os.path.join(myexperiment2.output_directory, filename)
fig2.savefig(filename2,dpi=300, bbox_inches='tight')
plt.close()
Expand Down
2 changes: 1 addition & 1 deletion examples/article_figures/fig3_de_panels.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,6 @@
axs[3].set_title(title)


filename = os.path.join(sweep_gen.output_directory, 'vlab4mic_fig3B.png')
filename = os.path.join(sweep_gen.output_directory, 'vlab4mic_fig3E.png')
fig.savefig(filename, dpi=300, bbox_inches='tight')
plt.close()
4 changes: 2 additions & 2 deletions examples/article_figures/figS_image_for_positioning.py
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,8 @@
plt.close()



img_mask = np.random.rand(100,100)
rng_ = np.random.default_rng(seed=random_seed)
img_mask = rng_.random([100,100])
p = 0.99
img_mask[img_mask >= p] = 1
img_mask[img_mask < p] = 0
Expand Down
4 changes: 2 additions & 2 deletions src/vlab4mic/experiments.py
Original file line number Diff line number Diff line change
Expand Up @@ -617,7 +617,7 @@ def _build_coordinate_field(
if use_self_particle and self.generators_status("particle"):
print("creating field from existing particle")
exported_field, fieldobject = field_from_particle(
self.particle, **self.virtualsample_params, **kwargs
self.particle, **self.virtualsample_params, random_seed=self.random_seed, **kwargs
)
self.virtualsample_params["minimal_distance"] = (
fieldobject.molecules_params["minimal_distance"]
Expand All @@ -631,7 +631,7 @@ def _build_coordinate_field(
else:
# create minimal field
fieldobject = coordinates_field.create_min_field(
**self.virtualsample_params, **kwargs
**self.virtualsample_params, random_seed=self.random_seed, **kwargs
)
exported_field = fieldobject.export_field()
if keep:
Expand Down
37 changes: 20 additions & 17 deletions src/vlab4mic/generate/coordinates_field.py
Original file line number Diff line number Diff line change
Expand Up @@ -121,7 +121,7 @@ def init_from_file(self, field_yaml):
self.set_molecules_params(**molecules)

def create_minimal_field(
self, nmolecules=1, random_placing=False, random_orientations=False, random_rotations=False, **kwargs
self, nmolecules=1, random_placing=False, random_orientations=False, random_rotations=False, random_seed=None, **kwargs
):
"""
Create a minimal field with a specified number of molecules and placement options.
Expand Down Expand Up @@ -163,7 +163,7 @@ def create_minimal_field(
# if no list was passed, then we will use the default nmolecules parameter
# having more than one particle necesarily use random placing
self.random_placing = True
self.generate_random_positions()
self.generate_random_positions(random_seed=random_seed)
self._gen_abs_from_rel_positions()
self.fluorophre_emitters = {
fluo_name: self.get_molecule_param("absolute_positions")
Expand All @@ -174,7 +174,7 @@ def create_minimal_field(
self.set_molecule_param("nMolecules", 1)
if random_placing:
self.random_placing = True
self.generate_random_positions()
self.generate_random_positions(random_seed=random_seed)
self._gen_abs_from_rel_positions()
point = self.get_molecule_param("absolute_positions")
self.fluorophre_emitters = {fluo_name: point.reshape(1, 3)}
Expand Down Expand Up @@ -296,6 +296,7 @@ def set_molecules_params(
random_positions,
random_orientations,
random_rotations,
random_seed=None,
**kwargs,
):
"""
Expand All @@ -317,11 +318,11 @@ def set_molecules_params(
# self.change_number_of_molecules(nMolecules)
self.set_molecule_param("nMolecules", nMolecules)
if random_positions:
self.generate_random_positions()
self.generate_random_positions(random_seed=random_seed)
if random_orientations:
self.generate_random_orientations()
self.generate_random_orientations(random_seed=random_seed)
if random_rotations:
self.initialise_random_rotations()
self.initialise_random_rotations(random_seed=random_seed)
for key, value in kwargs.items():
self.molecules_params[key] = value

Expand All @@ -337,7 +338,7 @@ def show_params(self):
print(key, ": ", val)

# methods to prime molecule positions, orientations...
def generate_random_positions(self):
def generate_random_positions(self, random_seed=None):
"""
Generate random positions for molecules within the field, optionally enforcing a minimal distance.
"""
Expand All @@ -359,23 +360,23 @@ def generate_random_positions(self):
self.molecules_params["relative_positions"] = rand
self._gen_abs_from_rel_positions()

def randomise_axial_position(self):
rng = np.random.default_rng()
def randomise_axial_position(self, random_seed=None):
rng = np.random.default_rng(seed=random_seed)
nmolecules = self.get_molecule_param("nMolecules")
axial_offsets = rng.choice(self.axial_offset, nmolecules, replace=True)
for i, zpos in enumerate(axial_offsets):
self.molecules_params["absolute_positions"][i][2] = zpos + self.z_offset


def generate_random_orientations(self):
def generate_random_orientations(self, random_seed=None):
"""
Generate random orientations for all molecules in the field.
"""
# give new orientation
norientations = self.get_molecule_param("nMolecules")
orientations = []
unconstrained = True
rng = np.random.default_rng()
rng = np.random.default_rng(seed=random_seed)
xy_orientation_changes = np.zeros((norientations,))
if self.xy_orientations is not None:
xy_orientation_changes = rng.choice(self.xy_orientations, norientations, replace=True)
Expand Down Expand Up @@ -403,12 +404,12 @@ def generate_random_orientations(self):
self.set_molecule_param("orientations_planewise", orientations_planewise)
self.set_molecule_param("orientations", None)

def initialise_random_rotations(self, rotation_angles: list = None):
def initialise_random_rotations(self, rotation_angles: list = None, random_seed=None):
"""
Generate random rotations around central axis for all molecules in the field.
"""
nrotations = self.get_molecule_param("nMolecules")
rng = np.random.default_rng()
rng = np.random.default_rng(seed=random_seed)
if self.rotation_angles is None:
print("random unconstrained rotations")
rotations = rng.integers(360, size=nrotations)
Expand Down Expand Up @@ -531,7 +532,7 @@ def get_fluorophore_params(self):
return self.fluoparams

# working with macromolecules
def create_molecules_from_InstanceObject(self, InstancePrototype: LabeledInstance):
def create_molecules_from_InstanceObject(self, InstancePrototype: LabeledInstance, random_seed=None):
"""
Create molecules in the field from a prototype instance.

Expand All @@ -550,7 +551,7 @@ def create_molecules_from_InstanceObject(self, InstancePrototype: LabeledInstanc
self._set_molecule_minimal_distance(dist=particle_copy.radial_hindance)
#self._set_molecule_minimal_distance(dist=min_distance)
if self.random_placing:
self.generate_random_positions()
self.generate_random_positions(random_seed=random_seed)
self._gen_abs_from_rel_positions()
# due to constraints, the actual number of particles might not be reps
nmolecules = len(self.molecules_params["absolute_positions"])
Expand All @@ -569,9 +570,9 @@ def create_molecules_from_InstanceObject(self, InstancePrototype: LabeledInstanc
if self.sample_initial_orientation is not None:
self.generate_global_orientation(self.sample_initial_orientation)
if self.random_orientations:
self.generate_random_orientations()
self.generate_random_orientations(random_seed=random_seed)
if self.random_rotations:
self.initialise_random_rotations()
self.initialise_random_rotations(random_seed=random_seed)
# self.relabel_molecules()
self.relabel_molecules()

Expand Down Expand Up @@ -943,6 +944,7 @@ def create_min_field(
random_orientations=False,
random_rotations=False,
prints=False,
random_seed=None,
**kwargs,
):
"""
Expand Down Expand Up @@ -977,6 +979,7 @@ def create_min_field(
random_placing=random_placing,
random_orientations=random_orientations,
random_rotations=random_rotations,
random_seed=random_seed,
**kwargs,
)
return coordinates_field
Expand Down
6 changes: 4 additions & 2 deletions src/vlab4mic/generate/labelled_instance.py
Original file line number Diff line number Diff line change
Expand Up @@ -1001,6 +1001,7 @@ def show_probe(
use_dol=False,
exact_dol= None,
axis_object=None,
random_seed=None,
**kwargs,
):
"""
Expand Down Expand Up @@ -1052,6 +1053,7 @@ def show_probe(
probe_emitters, center, np.array([0, 0, 0])
)
if dol is not None and use_dol:
rng_ = np.random.default_rng(seed=random_seed)
list_reoriented_points = []
max_emitters = centered_emitters.shape[0]
emitter_indices = np.arange(max_emitters)
Expand All @@ -1060,13 +1062,13 @@ def show_probe(
selected_emitters = [emitter_indices[x] for x in exact_dol]
#selected_emitters = emitter_indices[exact_dol]
else:
int_dol = np.random.poisson(lam=dol)
int_dol = rng_.poisson(lam=dol)
if int_dol != 0:
if int_dol > max_emitters:
# use max value
selected_emitters = emitter_indices
else:
selected_emitters = np.random.choice(
selected_emitters = rng_.choice(
emitter_indices, size=int_dol, replace=False
)
for se in selected_emitters:
Expand Down
4 changes: 2 additions & 2 deletions src/vlab4mic/workflows.py
Original file line number Diff line number Diff line change
Expand Up @@ -273,7 +273,7 @@ def particle_from_structure(


def field_from_particle(
particle: labinstance.LabeledInstance, field_config: str = None, **kwargs
particle: labinstance.LabeledInstance, field_config: str = None, random_seed=None, **kwargs
):
"""
Create a particle field from an input particle object.
Expand Down Expand Up @@ -305,7 +305,7 @@ def field_from_particle(
# coordinates_field.init_from_file(field_config)
else:
coordinates_field = create_min_field(**kwargs)
coordinates_field.create_molecules_from_InstanceObject(particle)
coordinates_field.create_molecules_from_InstanceObject(particle, random_seed=random_seed)
coordinates_field.construct_static_field()
return coordinates_field.export_field(), coordinates_field

Expand Down
Loading