Skip to content

Commit d98da63

Browse files
committed
Add mesh inversion functions
1 parent 19ba948 commit d98da63

File tree

2 files changed

+53
-13
lines changed

2 files changed

+53
-13
lines changed

cellpack_analysis/lib/mesh_tools.py

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1024,3 +1024,19 @@ def get_weights_from_distances(
10241024
else:
10251025
weights = 1 - scaled_distances
10261026
return weights
1027+
1028+
1029+
def invert_mesh_faces(input_mesh_path: str | Path, output_mesh_path: str | Path) -> None:
1030+
"""
1031+
Invert the faces of a mesh and save to a new file.
1032+
1033+
Parameters
1034+
----------
1035+
input_mesh_path
1036+
Path to the input mesh file
1037+
output_mesh_path
1038+
Path to save the inverted mesh file
1039+
"""
1040+
mesh = trimesh.load_mesh(input_mesh_path)
1041+
mesh.invert()
1042+
mesh.export(output_mesh_path)

cellpack_analysis/notebooks/export_simularium_paths_as_csv.py

Lines changed: 37 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -15,12 +15,14 @@
1515
# TODO: inverted meshes?
1616
# %%
1717
import json
18+
import shutil
1819

1920
import boto3
2021
import pandas as pd
2122
from tqdm import tqdm
2223

2324
from cellpack_analysis.lib.file_io import get_datadir_path, get_results_path
25+
from cellpack_analysis.lib.mesh_tools import invert_mesh_faces
2426

2527
# %%
2628
base_datadir = get_datadir_path()
@@ -39,6 +41,7 @@
3941
simularium_path = (
4042
base_datadir / "packing_outputs" / dataset / condition / rule / structure_name / "spheresSST"
4143
)
44+
figure_path = simularium_path / "figures"
4245
# %%
4346
records = []
4447
base_s3_mesh_url = (
@@ -47,10 +50,22 @@
4750
base_s3_simularium_url = "https://cellpack-analysis-data.s3.us-west-2.amazonaws.com/"
4851
s3_client = boto3.client("s3")
4952
# %%
50-
# Update meshes in s3 bucket
51-
mesh_path = base_datadir / "structure_data" / structure_id / "meshes"
52-
for file_path in tqdm(mesh_path.rglob("*")):
53+
# Invert and upload meshes to s3 bucket
54+
invert_meshes = False
55+
mesh_dir = base_datadir / "structure_data" / structure_id / "meshes"
56+
for file_path in tqdm(mesh_dir.rglob("*")):
5357
if file_path.is_file():
58+
# invert mem meshes
59+
if invert_meshes and "mem" in file_path.name and "original" not in file_path.name:
60+
orig_mesh_path = file_path.parent / f"{file_path.stem}_original.obj"
61+
# copy original file with suffix
62+
shutil.copy(
63+
file_path,
64+
orig_mesh_path,
65+
)
66+
invert_mesh_faces(input_mesh_path=orig_mesh_path, output_mesh_path=file_path)
67+
68+
# upload to s3
5469
s3_key = file_path.relative_to(base_datadir).as_posix()
5570
s3_client.upload_file(str(file_path), "cellpack-analysis-data", s3_key)
5671
s3_client.put_object_acl(
@@ -59,6 +74,7 @@
5974
Key=s3_key,
6075
)
6176
# %%
77+
reupload = False
6278
# Update simularium files and upload to s3
6379
for file_path in tqdm(simularium_path.rglob("*.simularium")):
6480
# update mesh paths in simularium file
@@ -69,17 +85,25 @@
6985
mapping["geometry"]["url"] = f"{base_s3_mesh_url}{mapping['geometry']['url']}"
7086
with open(file_path, "w") as f:
7187
json.dump(sim_data, f, indent=2)
72-
# try:
73-
# s3_client.head_object(Bucket="cellpack-analysis-data", Key=s3_key)
74-
# except s3_client.exceptions.ClientError:
75-
# upload to s3
88+
7689
s3_key = file_path.relative_to(base_datadir).as_posix()
77-
s3_client.upload_file(str(file_path), "cellpack-analysis-data", s3_key)
78-
s3_client.put_object_acl(
79-
ACL="public-read",
80-
Bucket="cellpack-analysis-data",
81-
Key=s3_key,
82-
)
90+
91+
should_upload = reupload
92+
if not reupload:
93+
try:
94+
s3_client.head_object(Bucket="cellpack-analysis-data", Key=s3_key)
95+
should_upload = False
96+
except s3_client.exceptions.ClientError:
97+
should_upload = True
98+
99+
if should_upload:
100+
s3_client.upload_file(str(file_path), "cellpack-analysis-data", s3_key)
101+
s3_client.put_object_acl(
102+
ACL="public-read",
103+
Bucket="cellpack-analysis-data",
104+
Key=s3_key,
105+
)
106+
83107
s3_path = f"{base_s3_simularium_url}{s3_key}"
84108
record = {
85109
"File Path": str(s3_path),

0 commit comments

Comments
 (0)