Created
June 30, 2011 15:04
-
-
Save thouis/1056421 to your computer and use it in GitHub Desktop.
initial rewrite of recalculate_groups
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
def recalculate_group(self, workspace, indexes): | |
'''Recalculate all measurements once post_group has run | |
workspace - the workspace being operated on | |
indexes - the indexes of the group's image sets' measurements | |
''' | |
m = workspace.measurements | |
object_name = self.object_name.value | |
assert isinstance(m, cpmeas.Measurements) | |
parent_object_numbers = m.get_all_measurements( | |
object_name, self.measurement_name(F_PARENT_OBJECT_NUMBER)) | |
parent_image_numbers = m.get_all_measurements( | |
object_name, self.measurement_name(F_PARENT_GROUP_INDEX)) | |
group_indexes = m.get_all_measurements( cpmeas.IMAGE, | |
cpp.GROUP_INDEX) | |
max_object_count = np.max([len(x) for x in parent_object_numbers]) | |
if max_object_count == 0: | |
return | |
max_image_number = np.max(indexes) + 1 | |
def w(a): | |
'''Wrap a measurement array as a numpy sparse array | |
Elements in the array can be addressed correctly by image number | |
and object number. | |
''' | |
result = scipy.sparse.lil_matrix((max_image_number+1, | |
max_object_count+2), | |
dtype = a[0].dtype) | |
for index in indexes: | |
row = a[index] | |
nobjects = len(row) | |
if nobjects > 0: | |
group_index = group_indexes[index] | |
result[group_index, 1:(len(row)+1)] = row | |
return result | |
# | |
# Recalculate the trajectories | |
# | |
old_dists = m.get_all_measurements( | |
object_name, self.measurement_name(F_DISTANCE_TRAVELED)) | |
old_integrated = m.get_all_measurements( | |
object_name, self.measurement_name(F_INTEGRATED_DISTANCE)) | |
w_integrated = w(old_integrated) | |
x = w(m.get_all_measurements(object_name, M_LOCATION_CENTER_X)) | |
y = w(m.get_all_measurements(object_name, M_LOCATION_CENTER_Y)) | |
old_trajectory_x = m.get_all_measurements( | |
object_name, self.measurement_name(F_TRAJECTORY_X)) | |
old_trajectory_y = m.get_all_measurements( | |
object_name, self.measurement_name(F_TRAJECTORY_Y)) | |
old_linearity = m.get_all_measurements( | |
object_name, self.measurement_name(F_LINEARITY)) | |
w_linearity = w(old_linearity) | |
old_lifetime = m.get_all_measurements( | |
object_name, self.measurement_name(F_LIFETIME)) | |
w_lifetime = w(old_lifetime) | |
class wrapped(object): | |
def __init__(self, feature_name): | |
self.feature_name = feature_name | |
def get(self, index): | |
return m.get_measurement(object_name, self.feature_name, index + 1) | |
def set(self, index, val): | |
m.add_measurement(object_name, self.feature_name, index + 1, val) | |
def get_parent(index, no_parent=None): | |
parent_indices = m.get_measurement(self.object_name, | |
self.measurement_name(F_PARENT_GROUP_INDEX), | |
index) | |
parent_objnums = m.get_measurement(self.object_name, | |
self.measurement_name(F_PARENT_OBJECT_NUMBER), | |
index) | |
for index in indexes: | |
# | |
# Distances traveled from step to step | |
# | |
this_x = x.get(index) | |
this_y = y.get(index) | |
last_x = x.get_parent(index, no_parent=this_x) | |
last_y = y.get_parent(index, no_parent=this_y) | |
x_diff = this_x - last_x | |
y_diff = this_y - last_y | |
trajectory_x.set(index, x_diff) | |
trajectory_y.set(index, y_diff) | |
# | |
# Integrated distance = accumulated distance for lineage | |
# | |
new_integrated = integrated.get_parent(index, no_parent=0) + \ | |
np.sqrt(x_diff * x_diff + y_diff * y_diff) | |
integrated.set(index, new_integrated) | |
# | |
# Total distance = crow-fly distance from initial ancestor | |
# | |
x_tot_diff = this_x - x.get_ancestor(index) | |
y_tot_diff = this_y - y.get_ancestor(index) | |
tot_distance = np.sqrt(x_tot_diff * x_tot_diff + | |
y_tot_diff * y_tot_diff) | |
tot_distance.set(index, tot_distance) | |
# | |
# Linearity = ratio of crow-fly distance and integrated | |
# distance. NaN for new cells is ok. | |
# | |
linearity.set(index, tot_distance / integrated_distance) | |
# | |
# Add 1 to lifetimes / zero for new | |
# | |
new_lifetimes = lifetimes.get_parent(index, no_parent=-1) + 1 | |
lifetimes.set(index, new_lifetimes) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment