-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* #294 fix: Update experiment update conditions * fix: Fix registration of fiducial experiments * feat: Move populations to error state if cordmap doesn't produce cell files * fix: Rollback custom cordmap changes * #294 fix: Improve update experiment logic * #286 feat: Change csv headers at download time * fix: Update population cells file location
- Loading branch information
1 parent
3e65a2d
commit 76c44a1
Showing
3 changed files
with
58 additions
and
24 deletions.
There are no files selected for viewing
48 changes: 48 additions & 0 deletions
48
applications/portal/backend/api/helpers/download_populations.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
import csv | ||
import io | ||
import os | ||
import tempfile | ||
import zipfile | ||
|
||
from django.db.models import Q | ||
from django.http import HttpResponse | ||
|
||
from api.models import Population | ||
|
||
|
||
def get_populations_zip(active_populations, experiment): | ||
filename_prefix = f'{experiment.name}' if experiment else 'population' | ||
filename_suffix = 's' if len(active_populations) > 1 else '' | ||
|
||
temp_file = tempfile.TemporaryFile() | ||
with zipfile.ZipFile(temp_file, 'w') as zip_file: | ||
for population in Population.objects.filter(Q(experiment=experiment) | Q(experiment=None), id__in=active_populations): | ||
if population.cells: | ||
modified_csv_content = modify_csv_headers(population.cells.path) | ||
zip_file.writestr(os.path.basename(population.cells.path), modified_csv_content) | ||
filename_prefix += f"_{population.name}" | ||
|
||
temp_file.seek(0) # Reset file pointer | ||
return temp_file, f"{filename_prefix}_population{filename_suffix}.zip" | ||
|
||
|
||
|
||
def modify_csv_headers(csv_file_path): | ||
with open(csv_file_path, 'r') as csvfile: | ||
reader = csv.reader(csvfile) | ||
modified_data = io.StringIO() | ||
writer = csv.writer(modified_data) | ||
|
||
# Swap 'x' and 'y' in headers | ||
headers = next(reader) | ||
x_index = headers.index('x') | ||
y_index = headers.index('y') | ||
headers[x_index], headers[y_index] = headers[y_index], headers[x_index] | ||
writer.writerow(headers) | ||
|
||
# Write the rest of the rows as they are | ||
for row in reader: | ||
writer.writerow(row) | ||
|
||
modified_data.seek(0) | ||
return modified_data.getvalue() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters