Skip to content

Commit

Permalink
Remove redundant "Optimized-" models (#196)
Browse files Browse the repository at this point in the history
* Remove redundant "Optimized-" models

* Remove references in swcc project

* Lint fix of migration file

* Remove reference in tasks

* Remove implicit references in swcc project
  • Loading branch information
annehaley authored Sep 28, 2022
1 parent 80fe670 commit 45ad8c0
Show file tree
Hide file tree
Showing 12 changed files with 86 additions and 256 deletions.
18 changes: 1 addition & 17 deletions shapeworks_cloud/core/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,16 +67,8 @@ class Meta:
fields = ['project', 'mesh']


class OptimizedShapeModelFilter(FilterSet):
project = ModelChoiceFilter(queryset=models.Project.objects.all())

class Meta:
models = models.OptimizedShapeModel
fields = ['project']


class OptimizedParticlesFilter(FilterSet):
shape_model = ModelChoiceFilter(queryset=models.OptimizedShapeModel.objects.all())
project = ModelChoiceFilter(queryset=models.Project.objects.all())
original_segmentation = ModelChoiceFilter(
field_name='groomed_segmentation__segmentation',
queryset=models.Segmentation.objects.all(),
Expand All @@ -97,11 +89,3 @@ class Meta:
'original_segmentation',
'original_mesh',
]


class OptimizedPCAModelFilter(FilterSet):
shape_model = ModelChoiceFilter(queryset=models.OptimizedShapeModel.objects.all())

class Meta:
models = models.OptimizedParticles
fields = ['shape_model']
50 changes: 50 additions & 0 deletions shapeworks_cloud/core/migrations/0013_remove_redundant_models.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
# Generated by Django 3.2.15 on 2022-09-27 14:38

from django.db import migrations, models
import django.db.models.deletion


class Migration(migrations.Migration):

dependencies = [
('core', '0012_analysis_models'),
]

operations = [
migrations.RemoveField(
model_name='optimizedshapemodel',
name='project',
),
migrations.RemoveField(
model_name='optimizedsurfacereconstruction',
name='particles',
),
migrations.RemoveField(
model_name='optimizedsurfacereconstructionmeta',
name='shape_model',
),
migrations.RemoveField(
model_name='optimizedparticles',
name='shape_model',
),
migrations.AddField(
model_name='optimizedparticles',
name='project',
field=models.ForeignKey(
default=None, on_delete=django.db.models.deletion.CASCADE, to='core.project'
),
preserve_default=False,
),
migrations.DeleteModel(
name='OptimizedPCAModel',
),
migrations.DeleteModel(
name='OptimizedShapeModel',
),
migrations.DeleteModel(
name='OptimizedSurfaceReconstruction',
),
migrations.DeleteModel(
name='OptimizedSurfaceReconstructionMeta',
),
]
53 changes: 1 addition & 52 deletions shapeworks_cloud/core/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -109,26 +109,13 @@ class GroomedMesh(TimeStampedModel, models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE, related_name='groomed_meshes')


class OptimizedShapeModel(TimeStampedModel, models.Model):
project = models.ForeignKey(
Project,
on_delete=models.CASCADE,
related_name='optimized_shape_models',
)
parameters = models.JSONField(default=dict)


class OptimizedParticles(TimeStampedModel, models.Model):
project = models.ForeignKey(Project, on_delete=models.CASCADE)
world = S3FileField()
local = S3FileField()
transform = S3FileField()
constraints = S3FileField(null=True)

shape_model = models.ForeignKey(
OptimizedShapeModel,
on_delete=models.CASCADE,
related_name='particles',
)
groomed_segmentation = models.ForeignKey(
GroomedSegmentation,
on_delete=models.CASCADE,
Expand All @@ -143,41 +130,3 @@ class OptimizedParticles(TimeStampedModel, models.Model):
blank=True,
null=True,
)


class OptimizedSurfaceReconstructionMeta(TimeStampedModel, models.Model):
method = models.CharField(max_length=255) # TODO: Should be choices
reconstruction_params = S3FileField()
template_reconstruction = S3FileField()

shape_model = models.ForeignKey(
OptimizedShapeModel,
on_delete=models.CASCADE,
related_name='+',
)


class OptimizedSurfaceReconstruction(TimeStampedModel, models.Model):
particles = models.OneToOneField(
OptimizedParticles,
on_delete=models.CASCADE,
related_name='surface_reconstruction',
primary_key=True,
)
sample_reconstruction = S3FileField()


class OptimizedPCAModel(TimeStampedModel, models.Model):
shape_model = models.OneToOneField(
OptimizedShapeModel,
on_delete=models.CASCADE,
related_name='pca_model',
primary_key=True,
)

mean_particles = S3FileField()
pca_modes = S3FileField()
eigen_spectrum = S3FileField()


# TODO: checkpoints... what about pca model reference?
22 changes: 0 additions & 22 deletions shapeworks_cloud/core/rest.py
Original file line number Diff line number Diff line change
Expand Up @@ -116,34 +116,12 @@ class GroomedMeshViewSet(BaseViewSet):
filterset_class = filters.GroomedMeshFilter


class OptimizedShapeModelViewSet(BaseViewSet):
queryset = models.OptimizedShapeModel.objects.all()
serializer_class = serializers.OptimizedShapeModelSerializer
filterset_class = filters.OptimizedShapeModelFilter


class OptimizedParticlesViewSet(BaseViewSet):
queryset = models.OptimizedParticles.objects.all()
serializer_class = serializers.OptimizedParticlesSerializer
filterset_class = filters.OptimizedParticlesFilter


class OptimizedSurfaceReconstructionMetaViewSet(BaseViewSet):
queryset = models.OptimizedSurfaceReconstructionMeta.objects.all()
serializer_class = serializers.OptimizedSurfaceReconstructionMetaSerializer


class OptimizedSurfaceReconstructionViewSet(BaseViewSet):
queryset = models.OptimizedSurfaceReconstruction.objects.all()
serializer_class = serializers.OptimizedSurfaceReconstructionSerializer


class OptimizedPCAModelViewSet(BaseViewSet):
queryset = models.OptimizedPCAModel.objects.all()
serializer_class = serializers.OptimizedPCAModelSerializer
filterset_class = filters.OptimizedPCAModelFilter


class CachedAnalysisViewSet(BaseViewSet):
queryset = models.CachedAnalysis.objects.all()

Expand Down
39 changes: 0 additions & 39 deletions shapeworks_cloud/core/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -122,14 +122,6 @@ def get_id(self, obj) -> int:
return obj.mesh_id


class OptimizedShapeModelSerializer(serializers.ModelSerializer):
parameters = serializers.DictField()

class Meta:
model = models.OptimizedShapeModel
fields = '__all__'


class OptimizedParticlesSerializer(serializers.ModelSerializer):
world = S3FileSerializerField()
local = S3FileSerializerField()
Expand All @@ -139,34 +131,3 @@ class OptimizedParticlesSerializer(serializers.ModelSerializer):
class Meta:
model = models.OptimizedParticles
fields = '__all__'


class OptimizedSurfaceReconstructionMetaSerializer(serializers.ModelSerializer):
reconstruction_params = S3FileSerializerField()
template_reconstruction = S3FileSerializerField()

class Meta:
model = models.OptimizedSurfaceReconstructionMeta
fields = '__all__'


class OptimizedSurfaceReconstructionSerializer(serializers.ModelSerializer):
sample_reconstruction = S3FileSerializerField()

class Meta:
model = models.OptimizedSurfaceReconstruction
fields = '__all__'


class OptimizedPCAModelSerializer(serializers.ModelSerializer):
id = serializers.SerializerMethodField()
mean_particles = S3FileSerializerField()
pca_modes = S3FileSerializerField()
eigen_spectrum = S3FileSerializerField()

class Meta:
model = models.OptimizedPCAModel
fields = '__all__'

def get_id(self, obj) -> int:
return obj.shape_model.id
7 changes: 2 additions & 5 deletions shapeworks_cloud/core/tasks.py
Original file line number Diff line number Diff line change
Expand Up @@ -189,7 +189,7 @@ def post_command_function(project, download_dir, project_dfs, project_filename):
def optimize(user_id, project_id, form_data):
def pre_command_function():
# delete any previous results
models.OptimizedShapeModel.objects.filter(project=project_id).delete()
models.OptimizedParticles.objects.filter(project=project_id).delete()

def post_command_function(project, download_dir, project_dfs, project_filename):
save_dfs_to_project_spreadsheet(
Expand All @@ -211,13 +211,10 @@ def post_command_function(project, download_dir, project_dfs, project_filename):
target_mesh = project_groomed_meshes.filter(
file__endswith=groomed_filename,
).first()
result_shape_object = models.OptimizedShapeModel.objects.create(
project=project, parameters=form_data
)
result_particles_object = models.OptimizedParticles.objects.create(
groomed_segmentation=target_segmentation,
groomed_mesh=target_mesh,
shape_model=result_shape_object,
project=project,
)
result_particles_object.world.save(
row['world_particles_1'].split('/')[-1],
Expand Down
6 changes: 0 additions & 6 deletions shapeworks_cloud/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,15 +20,9 @@
'groomed-segmentations', rest.GroomedSegmentationViewSet, basename='groomed_segmentation'
)
router.register('groomed-meshes', rest.GroomedMeshViewSet, basename='groomed_mesh')
router.register(
'optimized-shape-models', rest.OptimizedShapeModelViewSet, basename='optimized_shape_model'
)
router.register(
'optimized-particles', rest.OptimizedParticlesViewSet, basename='optimized_particles'
)
router.register(
'optimized-pca-model', rest.OptimizedPCAModelViewSet, basename='optimized_pca_model'
)
router.register('cached-analysis', rest.CachedAnalysisViewSet, basename='cached_analysis')
router.register(
'cached-analysis-mode', rest.CachedAnalysisModeViewSet, basename='cached_analysis_mode'
Expand Down
4 changes: 0 additions & 4 deletions swcc/swcc/models/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,8 +5,6 @@
Image,
Mesh,
OptimizedParticles,
OptimizedPCAModel,
OptimizedShapeModel,
Segmentation,
)
from .project import Project
Expand All @@ -19,8 +17,6 @@
'Image',
'Mesh',
'OptimizedParticles',
'OptimizedPCAModel',
'OptimizedShapeModel',
'Project',
'Segmentation',
'Subject',
Expand Down
50 changes: 2 additions & 48 deletions swcc/swcc/models/other_models.py
Original file line number Diff line number Diff line change
@@ -1,14 +1,9 @@
from __future__ import annotations

from pathlib import Path

try:
from typing import Any, Dict, Iterator, List, Literal, Optional
from typing import List, Literal, Optional
except ImportError:
from typing import (
Any,
Dict,
Iterator,
List,
Optional,
)
Expand Down Expand Up @@ -67,57 +62,18 @@ class GroomedMesh(ApiModel):
project: Project


class OptimizedShapeModel(ApiModel):
_endpoint = 'optimized-shape-models'

project: Project
parameters: Dict[str, Any]

@property
def particles(self) -> Iterator[OptimizedParticles]:
return OptimizedParticles.list(shape_model=self)

def add_particles(
self,
world: Path,
local: Path,
transform: Path,
groomed_segmentation: Optional[GroomedSegmentation],
groomed_mesh: Optional[GroomedMesh],
constraints: Path,
) -> OptimizedParticles:
return OptimizedParticles(
world=world,
local=local,
transform=transform,
shape_model=self,
groomed_segmentation=groomed_segmentation,
groomed_mesh=groomed_mesh,
constraints=constraints,
).create()


class OptimizedParticles(ApiModel):
_endpoint = 'optimized-particles'

world: FileType[Literal['core.OptimizedParticles.world']]
local: FileType[Literal['core.OptimizedParticles.local']]
transform: FileType[Literal['core.OptimizedParticles.transform']]
shape_model: OptimizedShapeModel
project: Project
groomed_segmentation: Optional[GroomedSegmentation]
groomed_mesh: Optional[GroomedMesh]
constraints: Optional[FileType[Literal['core.OptimizedParticles.constraints']]] = None


class OptimizedPCAModel(ApiModel):
_endpoint = 'optimized-pca-model'

mean_particles: FileType[Literal['core.OptimizedPCAModel.mean_particles']]
pca_modes: FileType[Literal['core.OptimizedPCAModel.pca_modes']]
eigen_spectrum: FileType[Literal['core.OptimizedPCAModel.eigen_spectrum']]
shape_model: OptimizedShapeModel


class CachedAnalysisModePCA(ApiModel):
_endpoint = 'cached-analysis-mode-pca'

Expand Down Expand Up @@ -152,6 +108,4 @@ class CachedAnalysis(ApiModel):
Image.update_forward_refs()
GroomedSegmentation.update_forward_refs()
GroomedMesh.update_forward_refs()
OptimizedShapeModel.update_forward_refs()
OptimizedParticles.update_forward_refs()
OptimizedPCAModel.update_forward_refs()
Loading

0 comments on commit 45ad8c0

Please sign in to comment.