-
Notifications
You must be signed in to change notification settings - Fork 1
Make full-resolution jpg files of atlases #640
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from all commits
Commits
Show all changes
17 commits
Select commit
Hold shift + click to select a range
6b85156
Add an atlas context
stephen-riggs b9f836b
Add code to make atlas jpg file
stephen-riggs b860589
Update test
stephen-riggs 7d912ee
Add new endpoint to router manifest
stephen-riggs cf24537
Secure name
stephen-riggs cf59dad
Try and fix atlas destination determination
stephen-riggs 3914439
Fix post to api
stephen-riggs 574b89b
Atlas post needs to be a model
stephen-riggs 141fddf
Convert image to 8-bit
stephen-riggs 0b24c71
Merged recent changes from 'main' branch
tieneupin fd3414d
Added logs to keep track of atlas image conversion workflow
tieneupin 93537e0
Replaced 'mrcfile.read' with 'mrcfile.open'
tieneupin b9bc8f6
Added unit test for the 'atlas_jpg_from_mrc' function
tieneupin 726022a
Added a unit test for the atlas image FastAPI endpoint
tieneupin 144aed6
Sanitised file paths in logs
tieneupin 88c5bde
Updated log output slightly
tieneupin 2ce7816
Parametrised test to further improve coverage
tieneupin File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or 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 hidden or 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,52 @@ | ||
| import logging | ||
| from pathlib import Path | ||
| from typing import Optional | ||
|
|
||
| import requests | ||
|
|
||
| from murfey.client.context import Context | ||
| from murfey.client.contexts.spa import _get_source | ||
| from murfey.client.contexts.spa_metadata import _atlas_destination | ||
| from murfey.client.instance_environment import MurfeyInstanceEnvironment | ||
| from murfey.util.api import url_path_for | ||
| from murfey.util.client import authorised_requests, capture_post | ||
|
|
||
| logger = logging.getLogger("murfey.client.contexts.atlas") | ||
|
|
||
| requests.get, requests.post, requests.put, requests.delete = authorised_requests() | ||
|
|
||
|
|
||
| class AtlasContext(Context): | ||
| def __init__(self, acquisition_software: str, basepath: Path): | ||
| super().__init__("Atlas", acquisition_software) | ||
| self._basepath = basepath | ||
|
|
||
| def post_transfer( | ||
| self, | ||
| transferred_file: Path, | ||
| environment: Optional[MurfeyInstanceEnvironment] = None, | ||
| **kwargs, | ||
| ): | ||
| super().post_transfer( | ||
| transferred_file=transferred_file, | ||
| environment=environment, | ||
| **kwargs, | ||
| ) | ||
|
|
||
| if ( | ||
| environment | ||
| and "Atlas_" in transferred_file.stem | ||
| and transferred_file.suffix == ".mrc" | ||
| ): | ||
| source = _get_source(transferred_file, environment) | ||
| if source: | ||
| transferred_atlas_name = _atlas_destination( | ||
| environment, source, transferred_file | ||
| ) / transferred_file.relative_to(source.parent) | ||
| capture_post( | ||
| f"{str(environment.url.geturl())}{url_path_for('session_control.spa_router', 'make_atlas_jpg', session_id=environment.murfey_session)}", | ||
| json={"path": str(transferred_atlas_name)}, | ||
| ) | ||
| logger.info( | ||
| f"Submitted request to create JPG image of atlas {str(transferred_atlas_name)!r}" | ||
| ) |
This file contains hidden or 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 hidden or 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 hidden or 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 hidden or 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,43 @@ | ||
| import logging | ||
| from pathlib import Path | ||
|
|
||
| import mrcfile | ||
| import PIL.Image | ||
| from werkzeug.utils import secure_filename | ||
|
|
||
| from murfey.util import sanitise | ||
| from murfey.util.config import get_machine_config | ||
|
|
||
| logger = logging.getLogger("murfey.workflows.spa.atlas") | ||
|
|
||
|
|
||
| def atlas_jpg_from_mrc(instrument_name: str, visit_name: str, atlas_mrc: Path): | ||
| logger.debug( | ||
| f"Starting workflow to create JPG image of atlas {sanitise(str(atlas_mrc))!r}" | ||
| ) | ||
| with mrcfile.open(atlas_mrc) as mrc: | ||
| data = mrc.data | ||
|
|
||
| machine_config = get_machine_config(instrument_name=instrument_name)[ | ||
| instrument_name | ||
| ] | ||
|
|
||
| parts = [secure_filename(p) for p in atlas_mrc.parts] | ||
| visit_idx = parts.index(visit_name) | ||
| core = Path("/".join(parts[: visit_idx + 1])) | ||
| sample_id = "Sample" | ||
| for p in parts: | ||
| if "Sample" in p: | ||
| sample_id = p | ||
| break | ||
| atlas_jpg_file = ( | ||
| core | ||
| / machine_config.processed_directory_name | ||
| / "atlas" | ||
| / secure_filename(f"{sample_id}_{atlas_mrc.stem}_fullres.jpg") | ||
| ) | ||
| atlas_jpg_file.parent.mkdir(parents=True, exist_ok=True) | ||
|
|
||
| im = PIL.Image.fromarray(data) | ||
| im.convert(mode="L").save(atlas_jpg_file) | ||
| logger.debug(f"JPG image of atlas saved as {str(atlas_jpg_file)!r}") | ||
This file contains hidden or 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 hidden or 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,76 @@ | ||
| from pathlib import Path | ||
| from unittest import mock | ||
| from unittest.mock import MagicMock | ||
|
|
||
| from fastapi import FastAPI | ||
| from fastapi.testclient import TestClient | ||
| from pytest_mock import MockerFixture | ||
|
|
||
| from murfey.server.api.auth import ( | ||
| validate_instrument_server_session_access, | ||
| validate_instrument_token, | ||
| ) | ||
| from murfey.server.api.session_control import spa_router | ||
| from murfey.server.murfey_db import murfey_db_session | ||
| from murfey.util.api import url_path_for | ||
|
|
||
|
|
||
| def test_make_atlas_jpg(mocker: MockerFixture, tmp_path: Path): | ||
| # Set up the objects to mock | ||
| instrument_name = "test" | ||
| visit_name = "test_visit" | ||
| session_id = 1 | ||
|
|
||
| # Override the database session generator | ||
| mock_session = MagicMock() | ||
| mock_session.instrument_name = instrument_name | ||
| mock_session.visit = visit_name | ||
| mock_query_result = MagicMock() | ||
| mock_query_result.one.return_value = mock_session | ||
| mock_db_session = MagicMock() | ||
| mock_db_session.exec.return_value = mock_query_result | ||
|
|
||
| def mock_get_db_session(): | ||
| yield mock_db_session | ||
|
|
||
| # Mock the instrument server tokens dictionary | ||
| mock_tokens = mocker.patch( | ||
| "murfey.server.api.instrument.instrument_server_tokens", | ||
| {session_id: {"access_token": mock.sentinel}}, | ||
| ) | ||
|
|
||
| # Mock the called workflow function | ||
| mock_atlas_jpg = mocker.patch( | ||
| "murfey.server.api.session_control.atlas_jpg_from_mrc", | ||
| return_value=None, | ||
| ) | ||
|
|
||
| # Set up the test file | ||
| image_dir = tmp_path / instrument_name / "data" / visit_name / "Atlas" | ||
| image_dir.mkdir(parents=True, exist_ok=True) | ||
| test_file = image_dir / "Atlas1.mrc" | ||
|
|
||
| # Set up the backend server | ||
| backend_app = FastAPI() | ||
|
|
||
| # Override validation and database dependencies | ||
| backend_app.dependency_overrides[validate_instrument_token] = lambda: None | ||
| backend_app.dependency_overrides[validate_instrument_server_session_access] = ( | ||
| lambda: session_id | ||
| ) | ||
| backend_app.dependency_overrides[murfey_db_session] = mock_get_db_session | ||
| backend_app.include_router(spa_router) | ||
| backend_server = TestClient(backend_app) | ||
|
|
||
| atlas_jpg_url = url_path_for( | ||
| "api.session_control.spa_router", "make_atlas_jpg", session_id=session_id | ||
| ) | ||
| response = backend_server.post( | ||
| atlas_jpg_url, | ||
| json={"path": str(test_file)}, | ||
| headers={"Authorization": f"Bearer {mock_tokens[session_id]['access_token']}"}, | ||
| ) | ||
|
|
||
| # Check that the expected calls were made | ||
| mock_atlas_jpg.assert_called_once_with(instrument_name, visit_name, test_file) | ||
| assert response.status_code == 200 |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.