Skip to content

Commit

Permalink
Merge pull request #131 from eastgenomics/IN-748-Fix-some-issues
Browse files Browse the repository at this point in the history
In 748 fix some issues
  • Loading branch information
PolarisAstrum-NHS authored Aug 29, 2024
2 parents e31edd3 + fed56d5 commit bd7cb83
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 65 deletions.
73 changes: 40 additions & 33 deletions trendyqc/trend_monitoring/backend_utils/plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,12 @@ def get_subset_queryset(data: Dict) -> QuerySet:
))
else:
if date_start and date_end:
if isinstance(date_start, list):
date_start = date_start[0]

if isinstance(date_end, list):
date_end = date_end[0]

filter_dict["report__date__range"] = (date_start, date_end)

# combine all the data passed through the form to build the final queryset
Expand Down Expand Up @@ -278,26 +284,33 @@ def format_data_for_plotly_js(plot_data: pd.DataFrame) -> tuple:
list: List of lists of the traces that need to be plotted
"""

colors = [
"#FF0000", # red
"#FFBABA", # pinkish
"#A04D4D", # brown
"#B30000", # maroon
"#FF7800", # orange
"#B69300", # ugly yellow
"#7D8040", # olive
"#00FF00", # bright green
"#000000", # black
"#969696", # grey
"#c7962c", # goldish
"#ff65ff", # fushia
"#6600cc", # purple
"#1c6dff", # blue
"#6ddfff", # light blue
"#ffdf3c", # yellow
"#00cc99", # turquoise
"#00a600", # green
]
colors = {
"Cancer Endocrine Neurology": [
"#FF0000", # red
"#FFBABA", # pinkish
"#A04D4D", # brown
"#B30000", # maroon
],
"Myeloid": [
"#FF7800", # orange
"#B69300", # ugly yellow
"#000000", # black
"#969696", # grey
],
"TruSight Oncology 500": [
"#7D8040", # olive
"#00cc99", # turquoise
"#00a600", # green
"#00FF00", # bright green

],
"Twist WES": [
"#ff65ff", # fushia
"#6600cc", # purple
"#1c6dff", # blue
"#6ddfff", # light blue
]
}

# args dict for configuring the traces for combined, first, second lane
args = {
Expand Down Expand Up @@ -340,18 +353,10 @@ def format_data_for_plotly_js(plot_data: pd.DataFrame) -> tuple:
groups = build_groups(plot_data)
seen_groups = []

group_colors = {}

# too many groups are possible
if len(colors) < len(groups):
if sum([len(v) for v in colors.values()]) < len(groups):
return f"Not enough colors are possible for the groups: {groups}"

# assign colors to groups
for i, group in enumerate(groups):
random_color = random.choice(colors)
group_colors[group] = random_color
colors.remove(random_color)

# first second lane flag to fix duplication in the legend
seen_first_lane = False
seen_second_lane = False
Expand All @@ -369,6 +374,8 @@ def format_data_for_plotly_js(plot_data: pd.DataFrame) -> tuple:
legend_name = f"{assay_name} - {sequencer_id}"

if legend_name not in seen_groups:
assay_colors = colors[assay_name]
color_assay_sequencer = assay_colors.pop(0)
seen_groups.append(legend_name)
shown_legend = True
else:
Expand All @@ -386,8 +393,8 @@ def format_data_for_plotly_js(plot_data: pd.DataFrame) -> tuple:
args["First lane"]["columns"] = plot_data.columns[7:9]
args["Second lane"]["columns"] = plot_data.columns[9:11]

args["Combined"]["boxplot_color"] = group_colors[legend_name]
args["Combined"]["boxplot_line_color"] = group_colors[legend_name]
args["Combined"]["boxplot_color"] = color_assay_sequencer
args["Combined"]["boxplot_line_color"] = color_assay_sequencer
args["Combined"]["name"] = legend_name
args["Combined"]["offsetgroup"] = legend_name
args["Combined"]["legendgroup"] = legend_name
Expand Down Expand Up @@ -444,8 +451,8 @@ def format_data_for_plotly_js(plot_data: pd.DataFrame) -> tuple:
"project_name": project_name,
"lane": None,
"offsetgroup": "",
"boxplot_color": group_colors[legend_name],
"boxplot_line_color": group_colors[legend_name],
"boxplot_color": color_assay_sequencer,
"boxplot_line_color": color_assay_sequencer,
"showlegend": shown_legend
},
**legend_args
Expand Down
24 changes: 12 additions & 12 deletions trendyqc/trend_monitoring/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -95,52 +95,52 @@ def clean(self):
cleaned_data.get("days_back", None)
]

if not any(run_subset):
self.add_error(
None, ValidationError("No subset of runs selected")
)

start_date = cleaned_data.get("date_start", None)
end_date = cleaned_data.get("date_end", None)

if not cleaned_data.get("days_back", None):
# if the days_back or runs are not selected, get a default date range
if not cleaned_data.get("days_back", None) and not cleaned_data.get("run_select", None):
if start_date:
# clean was converting the type automatically, i need to do it
# manually now
start_date = datetime.datetime.strptime(
start_date[0], "%Y-%m-%d"
).date()
cleaned_data["date_start"] = start_date

if end_date:
end_date = datetime.datetime.strptime(
end_date[0], "%Y-%m-%d"
).date()
cleaned_data["date_end"] = end_date

if not start_date:
start_date = datetime.date.today() + relativedelta(months=-6)
cleaned_data["date_start"] = start_date

# if start date provided but not end date, define end date as
# today's date
if not end_date:
end_date = datetime.date.today()
cleaned_data["date_end"] = end_date

if not any(run_subset):
self.add_error(
None, ValidationError("No subset of runs selected")
)

# basic check if the start date is later than the end date
if end_date < start_date:
self.add_error(
None, ValidationError(
"date_start", ValidationError(
(
"Date end cannot be smaller than date start: "
f"{start_date} - {end_date}"
)
)
)
else:
cleaned_data["date_start"] = [start_date]
cleaned_data["date_end"] = [end_date]

if not cleaned_data.get("metrics_y", None):
self.add_error(None, ValidationError("No Y-axis metric selected"))
self.add_error("metrics_y", ValidationError("No Y-axis metric selected"))
else:
metrics = []

Expand Down
2 changes: 1 addition & 1 deletion trendyqc/trend_monitoring/templates/base.html
Original file line number Diff line number Diff line change
Expand Up @@ -69,7 +69,7 @@
<br>

<script>
$('#messages_div').delay(5000).fadeOut(800);
$('#messages_div').delay(15000).fadeOut(800);
</script>
</body>
</html>
38 changes: 19 additions & 19 deletions trendyqc/trend_monitoring/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,25 @@ def _get_context_data(self):
# get the default context data (the one key i need is one called tables)
context = super().get_context_data()

# setup the filter table
filter_table = FilterTable(Filter.objects.all())

# so moving the FilterTable away from the class init "breaks" the
# default pagination for the filter table. I reused the code in the
# django-tables2 code (https://github.com/jieter/django-tables2/blob/master/django_tables2/views.py#L235)
# to resetup the pagination
# i have no idea what this code does tbh
table_counter = count()
filter_table.prefix = (
filter_table.prefix or
self.table_prefix.format(next(table_counter))
)
RequestConfig(
self.request, paginate=self.get_table_pagination(filter_table)
).configure(filter_table)

context["tables"].append(filter_table)

# get all the assays and sort them
assays = sorted({
assay
Expand Down Expand Up @@ -132,25 +151,6 @@ def get(self, request):
"""

context = self._get_context_data()

filter_table = FilterTable(Filter.objects.all())

# so moving the FilterTable away from the class init "breaks" the
# default pagination for the filter table. I reused the code in the
# django-tables2 code (https://github.com/jieter/django-tables2/blob/master/django_tables2/views.py#L235)
# to resetup the pagination
# i have no idea what this code does tbh
table_counter = count()
filter_table.prefix = (
filter_table.prefix or
self.table_prefix.format(next(table_counter))
)
RequestConfig(
self.request, paginate=self.get_table_pagination(filter_table)
).configure(filter_table)

context["tables"].append(filter_table)

request.session.pop("form", None)
return render(request, self.template_name, context)

Expand Down

0 comments on commit bd7cb83

Please sign in to comment.