Skip to content

Commit

Permalink
Feature/standardize result files (#187)
Browse files Browse the repository at this point in the history
* reconstruct result_file in save_result

* * add env.base_name
* use value of seed instead of index in the result name
* add function to append seed number to base_name
* standardize env.result_file
* change default seed to 0
* update simularium file name

* delete setupfile param in save_as_simularium

* moved ingrJsonNode from IOutils to Writers

* remove unused vAnalysis code
set result file

* set result file name

* formatting

* remove comment and add docstring

* delete redundant lines in Analysis

* construct seed_basename using env

---------

Co-authored-by: Saurabh Mogre <saurabh.mogre@alleninstitute.org>
  • Loading branch information
rugeli and mogres authored Aug 29, 2023
1 parent d2889b8 commit 2c9db6d
Show file tree
Hide file tree
Showing 3 changed files with 96 additions and 178 deletions.
11 changes: 3 additions & 8 deletions cellpack/autopack/Analysis.py
Original file line number Diff line number Diff line change
Expand Up @@ -2200,7 +2200,6 @@ def pack_one_seed(
seed_index,
seed_list,
bounding_box,
packing_basename,
center_distance_dict=None,
pairwise_distance_dict=None,
ingredient_position_dict=None,
Expand All @@ -2215,17 +2214,14 @@ def pack_one_seed(
"""
Packs one seed of a recipe and returns the recipe object
"""
seed_basename = f"seed_{seed_index}_{packing_basename}"
self.env.result_file = str(
self.env.out_folder / f"results_seed_{seed_index}_{packing_basename}"
)
seed_basename = self.env.add_seed_number_to_base_name(seed_index)
seed = seed_list[seed_index]
# Clear
if self.afviewer:
self.afviewer.clearFill("Test_Spheres2D")
else:
self.env.reset()
self.env.saveResult = True
seed = seed_list[seed_index] # int(time())
numpy.random.seed(seed)
self.build_grid()
two_d = self.env.is_two_d()
Expand Down Expand Up @@ -2425,7 +2421,7 @@ def doloop(
"""
if seed_list is None:
seed_list = self.getHaltonUnique(number_of_packings)
packing_basename = f"{self.env.name}_{config_name}_{recipe_version}"
packing_basename = self.env.base_name
numpy.savetxt(
self.env.out_folder / f"seeds_{packing_basename}.txt",
seed_list,
Expand Down Expand Up @@ -2514,7 +2510,6 @@ def doloop(
seed_index=seed_index,
seed_list=seed_list,
bounding_box=bounding_box,
packing_basename=packing_basename,
center_distance_dict=center_distance_dict,
pairwise_distance_dict=pairwise_distance_dict,
ingredient_position_dict=ingredient_position_dict,
Expand Down
164 changes: 14 additions & 150 deletions cellpack/autopack/Environment.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,7 +52,6 @@
from scipy import spatial
import numpy
import pickle
import math
import json
from json import encoder
import logging
Expand Down Expand Up @@ -83,7 +82,7 @@
from cellpack.autopack import IOutils
from .octree import Octree
from .Gradient import Gradient
from .transformation import euler_from_matrix, signed_angle_between_vectors
from .transformation import signed_angle_between_vectors

# backward compatibility with kevin method
from cellpack.autopack.BaseGrid import BaseGrid as BaseGrid
Expand Down Expand Up @@ -150,9 +149,7 @@ def __init__(self, config=None, recipe=None):
# saving/pickle option
self.saveResult = "out" in config
self.out_folder = create_output_dir(config["out"], name, config["place_method"])
self.result_file = (
f"{self.out_folder}/{self.name}_{config['name']}_{self.version}"
)
self.base_name = f"{self.name}_{config['name']}_{self.version}"
self.grid_file_out = (
f"{self.out_folder}/{self.name}_{config['name']}_{self.version}_grid.dat"
)
Expand Down Expand Up @@ -516,9 +513,7 @@ def save_result(
free_points,
distances,
t0,
vAnalysis,
vTestid,
seedNum,
all_ingr_as_array,
save_grid_logs=False,
save_result_as_file=False,
Expand All @@ -538,7 +533,6 @@ def save_result(
self.store_asTxt()
Writer(format=self.format_output).save(
self,
self.result_file,
kwds=["compNum"],
result=True,
quaternion=True,
Expand All @@ -547,142 +541,6 @@ def save_result(
)

self.log.info("time to save result file %d", time() - t0)
if vAnalysis == 1:
# START Analysis Tools: Graham added back this big chunk of code
# for analysis tools and graphic on 5/16/12
# Needs to be cleaned up into a function and proper uPy code
# totalVolume = self.grid.gridVolume*unitVol
unitVol = self.grid.gridSpacing**3
wrkDirRes = self.result_file + "_analyze_"
for o in self.compartments: # only for compartment ?
# totalVolume -= o.surfaceVolume
# totalVolume -= o.interiorVolume
innerPointNum = len(o.insidePoints) - 1
self.log.info(" . . . . ")
self.log.info("for compartment o = %s", o.name)
self.log.info("inner Point Count = %d", innerPointNum)
self.log.info("inner Volume = %s", o.interiorVolume)
self.log.info("innerVolume temp Confirm = %d", innerPointNum * unitVol)
usedPts = 0
unUsedPts = 0
vDistanceString = ""
insidepointindce = numpy.nonzero(
numpy.equal(self.grid.compartment_ids, -o.number)
)[0]
for i in insidepointindce: # xrange(innerPointNum):
# pt = o.insidePoints[i] #fpts[i]
# print (pt,type(pt))
# for pt in self.histo.freePointsAfterFill:#[:self.histo.nbFreePointsAfterFill]:
d = self.distancesAfterFill[i]
vDistanceString += str(d) + "\n"
if d <= 0: # >self.smallestProteinSize-0.001:
usedPts += 1
else:
unUsedPts += 1
filename = (
wrkDirRes
+ "vResultMatrix1"
+ o.name
+ "_Testid"
+ str(vTestid)
+ "_Seed"
+ str(seedNum)
+ "_dists.txt"
) # Used this from thesis to overwrite less informative SVN version on next line on July 5, 2012
# filename = wrkDirRes+"/vDistances1.txt"
f = open(filename, "w")
f.write(vDistanceString)
f.close()

# result is [pos,rot,ingr.name,ingr.compNum,ptInd]
# if resultfilename == None:
# resultfilename = self.result_file
resultfilenameT = (
wrkDirRes
+ "vResultMatrix1"
+ o.name
+ "_Testid"
+ str(vTestid)
+ "_Seed"
+ str(seedNum)
+ "_Trans.txt"
) # Used this from thesis to overwrite less informative SVN version on next line on July 5, 2012
resultfilenameR = (
wrkDirRes
+ "vResultMatrix1"
+ o.name
+ "_Testid"
+ str(vTestid)
+ "_Seed"
+ str(seedNum)
+ "_Rot.txt"
)
vTranslationString = ""
vRotationString = ""
result = []
matCount = 0
for pos, rot, ingr, ptInd in o.molecules:
# BEGIN: newer code from Theis version added July 5, 2012
if hasattr(self, "afviewer"):
mat = rot.copy()
mat[:3, 3] = pos

# r = R.from_matrix(mat).as_euler("xyz", degrees=False)
r = euler_from_matrix(mat, "rxyz")
h1 = math.degrees(math.pi + r[0])
p1 = math.degrees(r[1])
b1 = math.degrees(-math.pi + r[2])
self.log.info("rot from matrix = %r %r %r %r", r, h1, p1, b1)
# END: newer code from Theis version added July 5, 2012
result.append([pos, rot])
pt3d = result[matCount][0]
(
x,
y,
z,
) = pt3d

vTranslationString += (
str(x) + ",\t" + str(y) + ",\t" + str(z) + "\n"
)
# vRotationString += str(rot3d) #str(h)+ ",\t" + str(p) + ",\t" + str(b) + "\n"
vRotationString += (
str(h1)
+ ",\t"
+ str(p1)
+ ",\t"
+ str(b1)
+ ",\t"
+ ingr.name
+ "\n"
)
matCount += 1

rfile = open(resultfilenameT, "w")
rfile.write(vTranslationString)
rfile.close()

rfile = open(resultfilenameR, "w")
rfile.write(vRotationString)
rfile.close()
self.log.info("len(result) = %d", len(result))
self.log.info("len(self.molecules) = %d", len(self.molecules))
# Graham Note: There is overused disk space- the rotation matrix is 4x4 with an offset of 0,0,0
# and we have a separate translation vector in the results and molecules arrays.
# Get rid of the translation vector and move it to the rotation matrix to save space...
# will that slow the time it takes to extract the vector from the matrix when we need to call it?
self.log.info(
"*************************************************** vDistance String Should be on"
)
self.log.info("unitVolume2 = %d", unitVol)
self.log.info("Number of Points Unused = %d", unUsedPts)
self.log.info("Number of Points Used = %d", usedPts)
self.log.info("Volume Used = %d", usedPts * unitVol)
self.log.info("Volume Unused = %d", unUsedPts * unitVol)
self.log.info("vTestid = %d", vTestid)
self.log.info("self.grid.nbGridPoints = %r", self.grid.nbGridPoints)
self.log.info("self.gridVolume = %d", self.grid.gridVolume)

self.log.info("self.compartments In Environment = %d", len(self.compartments))
if self.compartments == []:
unitVol = self.grid.gridSpacing**3
Expand Down Expand Up @@ -2055,12 +1913,20 @@ def update_count(self, allIngredients):
ingr.count = count
ingr.left_to_place = count

def add_seed_number_to_base_name(self, seed_number):
return f"{self.base_name}_seed_{seed_number}"

def set_result_file_name(self, seed_basename):
"""
Sets the result file name using the output folder path and a given seed basename
"""
self.result_file = str(self.out_folder / f"results_{seed_basename}")

def pack_grid(
self,
seedNum=14,
seedNum=0,
name=None,
vTestid=3,
vAnalysis=0,
**kw,
):
"""
Expand All @@ -2070,6 +1936,8 @@ def pack_grid(
# set periodicity
autopack.testPeriodicity = self.use_periodicity
t1 = time()
seed_base_name = self.add_seed_number_to_base_name(seedNum)
self.set_result_file_name(seed_base_name)
self.timeUpDistLoopTotal = 0
self.static = []
if self.grid is None:
Expand Down Expand Up @@ -2404,9 +2272,7 @@ def pack_grid(
free_points,
distances=distances,
t0=stime,
vAnalysis=vAnalysis,
vTestid=vTestid,
seedNum=seedNum,
all_ingr_as_array=all_ingr_as_array,
)

Expand All @@ -2423,9 +2289,7 @@ def pack_grid(
free_points,
distances=distances,
t0=time(),
vAnalysis=vAnalysis,
vTestid=vTestid,
seedNum=seedNum,
all_ingr_as_array=all_ingr_as_array,
)

Expand Down
Loading

0 comments on commit 2c9db6d

Please sign in to comment.