Skip to content

Commit

Permalink
Merge branch 'm2ms-1183' into model-refactoring-stage-1
Browse files Browse the repository at this point in the history
  • Loading branch information
kaliif committed Oct 23, 2023
2 parents 7085522 + 0479838 commit 6236d8f
Show file tree
Hide file tree
Showing 8 changed files with 234 additions and 23 deletions.
27 changes: 27 additions & 0 deletions scoring/migrations/0003_auto_20231020_1501.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
# Generated by Django 3.2.20 on 2023-10-20 15:01

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


class Migration(migrations.Migration):

dependencies = [
('viewer', '0013_auto_20230921_1417'),
('scoring', '0002_auto_20230818_1532'),
]

operations = [
migrations.CreateModel(
name='SiteObvsSiteObservationGroup',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('site_observation', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='viewer.siteobservation')),
('site_obvs_group', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='scoring.siteobservationgroup')),
],
),
migrations.AddConstraint(
model_name='siteobvssiteobservationgroup',
constraint=models.UniqueConstraint(fields=('site_observation', 'site_obvs_group'), name='unique_siteobservationgroupcontents'),
),
]
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
# Generated by Django 3.2.20 on 2023-10-20 15:02

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('viewer', '0013_auto_20230921_1417'),
('scoring', '0003_auto_20231020_1501'),
]

# NB! had some trouble with autmoatically generated AlterField, so
# changed it to remove + add
operations = [
migrations.RemoveField(
model_name='siteobservationgroup',
name='site_observation',
),
migrations.AddField(
model_name='siteobservationgroup',
name='site_observation',
field=models.ManyToManyField(related_name='site_observation_groups', through='scoring.SiteObvsSiteObservationGroup', to='viewer.SiteObservation'),
),
]
26 changes: 25 additions & 1 deletion scoring/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -150,9 +150,33 @@ class SiteObservationGroup(models.Model):
# Set the description
description = models.TextField(null=True)
# Set the ManyToMany
site_observation = models.ManyToManyField(SiteObservation, related_name="site_observation_groups")
site_observation = models.ManyToManyField(
SiteObservation,
through="SiteObvsSiteObservationGroup",
through_fields=("site_obvs_group", "site_observation"),
related_name="site_observation_groups",
)
# Set the centre of mass
x_com = models.FloatField(null=True)
y_com = models.FloatField(null=True)
z_com = models.FloatField(null=True)

def __str__(self) -> str:
return f"{self.pk}"

def __repr__(self) -> str:
return "<Xtalform %r %r %r>" % (self.id, self.group_type, self.description)


class SiteObvsSiteObservationGroup(models.Model):
site_obvs_group = models.ForeignKey(SiteObservationGroup, null=False, on_delete=models.CASCADE)
site_observation = models.ForeignKey(SiteObservation, null=False, on_delete=models.CASCADE)

class Meta:
constraints = [
models.UniqueConstraint(
fields=["site_observation", "site_obvs_group",],
name="unique_siteobservationgroupcontents",
),
]

42 changes: 39 additions & 3 deletions viewer/fixtures/tagcategories.json
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,50 @@
"model": "viewer.tagcategory",
"pk": 1,
"fields": {
"category": "Sites",
"category": "ConformerSites",
"colour": "00CC00",
"description": null
}
},
{
"model": "viewer.tagcategory",
"pk": 2,
"fields": {
"category": "CanonSites",
"colour": "ffcc00",
"description": null
}
},
{
"model": "viewer.tagcategory",
"pk": 3,
"fields": {
"category": "XtalformSites",
"colour": "0099ff",
"description": null
}
},
{
"model": "viewer.tagcategory",
"pk": 4,
"fields": {
"category": "Quatassemblies",
"colour": "00cc99",
"description": null
}
},
{
"model": "viewer.tagcategory",
"pk": 5,
"fields": {
"category": "Xtalforms",
"colour": "ffcc00",
"description": null
}
},
{
"model": "viewer.tagcategory",
"pk": 6,
"fields": {
"category": "Series",
"colour": "0000CC",
Expand All @@ -19,7 +55,7 @@
},
{
"model": "viewer.tagcategory",
"pk": 3,
"pk": 7,
"fields": {
"category": "Forum",
"colour": "CC0000",
Expand All @@ -28,7 +64,7 @@
},
{
"model": "viewer.tagcategory",
"pk": 4,
"pk": 8,
"fields": {
"category": "Other",
"colour": "000000",
Expand Down
26 changes: 26 additions & 0 deletions viewer/migrations/0014_auto_20231023_1048.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
# Generated by Django 3.2.20 on 2023-10-23 10:48

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


class Migration(migrations.Migration):

dependencies = [
('viewer', '0013_auto_20230921_1417'),
]

operations = [
migrations.CreateModel(
name='SiteObvsSiteObservationTag',
fields=[
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
('site_observation', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='viewer.siteobservation')),
('site_obvs_tag', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='viewer.siteobservationtag')),
],
),
migrations.AddConstraint(
model_name='siteobvssiteobservationtag',
constraint=models.UniqueConstraint(fields=('site_observation', 'site_obvs_tag'), name='unique_siteobservationtagcontents'),
),
]
22 changes: 22 additions & 0 deletions viewer/migrations/0015_auto_20231023_1049.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
# Generated by Django 3.2.20 on 2023-10-23 10:49

from django.db import migrations, models


class Migration(migrations.Migration):

dependencies = [
('viewer', '0014_auto_20231023_1048'),
]

operations = [
migrations.RemoveField(
model_name='siteobservationtag',
name='site_observations',
),
migrations.AddField(
model_name='siteobservationtag',
name='site_observations',
field=models.ManyToManyField(through='viewer.SiteObvsSiteObservationTag', to='viewer.SiteObservation'),
),
]
20 changes: 19 additions & 1 deletion viewer/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -919,7 +919,11 @@ class Meta:


class SiteObservationTag(Tag):
site_observations = models.ManyToManyField(SiteObservation, blank=True)
site_observations = models.ManyToManyField(
SiteObservation,
through="SiteObvsSiteObservationTag",
through_fields=("site_obvs_tag", "site_observation"),
)
mol_group = models.ForeignKey("scoring.SiteObservationGroup", null=True, blank=True,
on_delete=models.SET_NULL)

Expand All @@ -930,6 +934,20 @@ def __repr__(self) -> str:
return "<SiteObservationTag %r %r>" % (self.id, self.site_observations)


class SiteObvsSiteObservationTag(models.Model):
site_obvs_tag = models.ForeignKey(SiteObservationTag, null=False, on_delete=models.CASCADE)
site_observation = models.ForeignKey(SiteObservation, null=False, on_delete=models.CASCADE)

class Meta:
constraints = [
models.UniqueConstraint(
fields=["site_observation", "site_obvs_tag",],
name="unique_siteobservationtagcontents",
),
]



class SessionProjectTag(Tag):
"""Data for SessionProjectTag(s) inherited from Tag.
"""
Expand Down
69 changes: 51 additions & 18 deletions viewer/target_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -930,7 +930,16 @@ def process_metadata(

result.append(self._log_msg(site_observation_objects))

self._tag_site_observations(site_observation_objects)
tag_categories = (
"ConformerSites",
"CanonSites",
"XtalformSites",
"Quatassemblies",
"Xtalforms",
)

for cat in tag_categories:
self._tag_site_observations(site_observation_objects, cat)

# final remaining fk, attach reference site observation to canon_site_conf
for val in canon_site_conf_objects.values():
Expand All @@ -947,22 +956,54 @@ def process_metadata(

return result

def _tag_site_observations(self, site_observation_objects):
def _tag_site_observations(self, site_observation_objects, category):
# this is an attempt to replicate tag creation from previous
# loader. as there are plenty of differences, I cannot just
# use the same functions..

logger.debug("Getting category %s", category)
groups = {}
for _, obj in site_observation_objects.items():
tag = self._get_tag(obj.instance.canon_site_conf)
if tag not in groups.keys():
groups[tag] = [obj.instance]
if category == "ConformerSites":
tags = [
"conf_site: " + ",".join(obj.instance.canon_site_conf.residues[:3]),
]
elif category == "CanonSites":
tags = [
"canon_site: "
+ ",".join(obj.instance.xtalform_site.canon_site.residues[:3]),
]
elif category == "XtalformSites":
tags = [
"xtalform_site: "
+ ",".join(obj.instance.xtalform_site.residues[:3]),
]
# tricky one. connected via m2m
elif category == "Quatassemblies":
tags = [
"quatassembly: " + qa.name
for qa in obj.instance.xtalform_site.xtalform.quat_assembly.all()
]

elif category == "Xtalforms":
tags = [
"xtalform: " + obj.instance.xtalform_site.xtalform.name,
]
else:
groups[tag].append(obj.instance)
tags = [
"Unspecified",
]

for tag in tags:
if tag not in groups.keys():
groups[tag] = [obj.instance]
else:
groups[tag].append(obj.instance)

# I suspect I need to group them by site..
for tag, so_list in groups.items():
try:
# TODO: not unique, needs a solution
so_group = SiteObservationGroup.objects.get(
target=self.target, description=tag
)
Expand All @@ -985,24 +1026,16 @@ def _tag_site_observations(self, site_observation_objects):
except SiteObservationTag.DoesNotExist:
so_tag = SiteObservationTag()
so_tag.tag = tag
so_tag.category = TagCategory.objects.get(category="Sites")
so_tag.category = TagCategory.objects.get(category=category)
so_tag.target = self.target
so_tag.mol_group = so_group

so_tag.save()

for site_obvs in so_list:
if site_obvs not in so_group.site_observation.all():
logger.debug("site_obvs_group site_obvs_id=%s", site_obvs.id)
so_group.site_observation.add(site_obvs)

if site_obvs not in so_tag.site_observations.all():
logger.debug("site_obvs_tag site_obvs_id=%s", site_obvs.id)
so_tag.site_observations.add(site_obvs)

def _get_tag(self, canon_site_conf):
tag = ",".join(canon_site_conf.residues[:3])
return tag
logger.debug("site_obvs_id=%s", site_obvs.id)
so_group.site_observation.add(site_obvs)
so_tag.site_observations.add(site_obvs)

def _is_already_uploaded(self, target_created, project_created):
if target_created or project_created:
Expand Down

0 comments on commit 6236d8f

Please sign in to comment.