-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #1 from hdr-bgnn/code-import
Adds initial code
- Loading branch information
Showing
6 changed files
with
240 additions
and
1 deletion.
There are no files selected for viewing
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,41 @@ | ||
name: Create and publish a Docker image | ||
|
||
on: | ||
release: | ||
types: [published] | ||
|
||
env: | ||
REGISTRY: ghcr.io | ||
IMAGE_NAME: ${{ github.repository }} | ||
|
||
jobs: | ||
build-and-push-image: | ||
runs-on: ubuntu-latest | ||
permissions: | ||
contents: read | ||
packages: write | ||
|
||
steps: | ||
- name: Checkout repository | ||
uses: actions/checkout@v2 | ||
|
||
- name: Log in to the Container registry | ||
uses: docker/login-action@f054a8b539a109f9f41c372932f1ae047eff08c9 | ||
with: | ||
registry: ${{ env.REGISTRY }} | ||
username: ${{ github.actor }} | ||
password: ${{ secrets.GITHUB_TOKEN }} | ||
|
||
- name: Extract metadata (tags, labels) for Docker | ||
id: meta | ||
uses: docker/metadata-action@98669ae865ea3cffbcbaa878cf57c20bbf1c6c38 | ||
with: | ||
images: ${{ env.REGISTRY }}/${{ env.IMAGE_NAME }} | ||
|
||
- name: Build and push Docker image | ||
uses: docker/build-push-action@ad44023a93711e3deb337508980b4b5e9bcdc5dc | ||
with: | ||
context: . | ||
push: true | ||
tags: ${{ steps.meta.outputs.tags }} | ||
labels: ${{ steps.meta.outputs.labels }} |
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,21 @@ | ||
name: Run python tests | ||
|
||
on: [push] | ||
|
||
jobs: | ||
build: | ||
|
||
runs-on: ubuntu-latest | ||
strategy: | ||
matrix: | ||
python-version: ["3.7", "3.8", "3.9", "3.10"] | ||
|
||
steps: | ||
- uses: actions/checkout@v3 | ||
- name: Set up Python ${{ matrix.python-version }} | ||
uses: actions/setup-python@v4 | ||
with: | ||
python-version: ${{ matrix.python-version }} | ||
- name: Run python unit tests | ||
run: | | ||
python3 -m unittest discover . |
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,11 @@ | ||
FROM python:3.8.10-slim-buster | ||
LABEL "org.opencontainers.image.authors"="John Bradley <[email protected]>" | ||
LABEL "org.opencontainers.image.description"="Tool to reformat drexel metadata JSON files" | ||
|
||
WORKDIR /pipeline | ||
|
||
# ADD scripts in /pipeline to the PATH | ||
ENV PATH="/pipeline:${PATH}" | ||
|
||
COPY dm_formatter.py /pipeline | ||
CMD echo "dm_formatter.py input output" |
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 |
---|---|---|
@@ -1,2 +1,26 @@ | ||
# drexel_metadata_formatter | ||
Reformats the metadata output file from drexel_metadata repo | ||
Reformats the metadata output file from the [drexel_metadata repo](https://github.com/hdr-bgnn/drexel_metadata/). | ||
|
||
## Requirements | ||
- [Python 3](https://www.python.org/) | ||
|
||
|
||
## Usage | ||
``` | ||
usage: dm_formatter.py [-h] input output | ||
Convert metadata json file to BGNN minnow project format. | ||
positional arguments: | ||
input Path of input drexel_metadata format JSON metadata file. | ||
output Path of output BGNN minnow format JSON metadata file. | ||
optional arguments: | ||
-h, --help show this help message and exit | ||
``` | ||
|
||
## Testing | ||
The unit tests can be run with the following command: | ||
``` | ||
python3 -m unittest discover . | ||
``` |
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,85 @@ | ||
#!/usr/bin/env python3 | ||
# Convert drexel_metadata JSON file to BGNN minnow metadata JSON file | ||
import json | ||
import argparse | ||
|
||
|
||
def reformat_for_bgnn(result): | ||
""" | ||
Reformat and reduce the size of the result dictionary. | ||
Collect only the data necessary for BGNN minnow project. The new format matches the | ||
BGNN_metadata version. Therefore some of the values not calculated in drexel version are by | ||
default set to "None". | ||
Parameters | ||
---------- | ||
result : dict | ||
DESCRIPTION. output from gen_metadata() | ||
Returns | ||
------- | ||
bgnn_result : dict | ||
DESCRIPTION. {'base_name': xx, 'version':xx, | ||
'fish': {'fish_num': xx,"bbox":xx, 'pixel_analysis':xx, 'rescale':xx, | ||
'eye_bbox': xx, 'eye_center':xx , 'angle_degree': xx, | ||
'eye_direction':xx, 'foreground_mean':xx, 'background_mean':xx}, | ||
'ruler': {'bbox':xx, 'scale':xx, 'unit':xx}} | ||
""" | ||
|
||
name_base = list(result.keys())[0] | ||
first_value = list(result.values())[0] | ||
|
||
# Fish metadata | ||
fish_num = first_value['fish_count'] | ||
fish_bbox = first_value['fish'][0]['bbox'] | ||
pixel_analysis = not first_value['fish'][0]['pixel_analysis_failed'] | ||
|
||
if first_value['fish'][0]['has_eye']: | ||
eye_center = first_value['fish'][0]['eye_center'] | ||
else : | ||
eye_center = "None" | ||
|
||
eye_direction = first_value['fish'][0]['side'] | ||
foreground_mean = first_value['fish'][0]['foreground']['mean'] | ||
background_mean = first_value['fish'][0]['background']['mean'] | ||
|
||
dict_fish = {'fish_num': fish_num,"bbox": fish_bbox, | ||
'pixel_analysis': pixel_analysis, 'rescale': "None", | ||
'eye_bbox': "None", 'eye_center': eye_center , 'angle_degree': "None", | ||
'eye_direction': eye_direction, 'foreground_mean': round(foreground_mean,2), | ||
'background_mean': round(background_mean,2)} | ||
|
||
# Ruler metadata | ||
ruler_bbox = first_value['ruler_bbox'] if first_value['has_ruler'] else "None" | ||
scale = round(first_value['scale'],2) if "scale" in first_value.keys() else "None" | ||
unit = first_value['unit'] if "unit" in first_value.keys() else "None" | ||
|
||
dict_ruler = {'bbox':ruler_bbox, 'scale':scale, 'unit':unit} | ||
|
||
bgnn_result = {'base_name': name_base, 'version':"from drexel", | ||
'fish': dict_fish, 'ruler': dict_ruler} | ||
|
||
return bgnn_result | ||
|
||
|
||
def argument_parser(): | ||
parser = argparse.ArgumentParser(description='Convert metadata json file to BGNN minnow project format.') | ||
parser.add_argument('input', help='Path of input drexel_metadata format JSON metadata file.') | ||
parser.add_argument('output', help='Path of output BGNN minnow format JSON metadata file.') | ||
return parser | ||
|
||
|
||
def main(): | ||
parser = argument_parser() | ||
args = parser.parse_args() | ||
print(f"Converting {args.input} to BGNN minnows format {args.output}") | ||
with open(args.input, 'r') as infile: | ||
data = json.load(infile) | ||
result = reformat_for_bgnn(data) | ||
with open(args.output, 'w') as outfile: | ||
outfile.write(json.dumps(result)) | ||
|
||
|
||
if __name__ == '__main__': | ||
main() |
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,57 @@ | ||
import unittest | ||
from dm_formatter import reformat_for_bgnn | ||
|
||
|
||
DM_INPUT_METADATA = { | ||
"SOME-FISH-1234": { | ||
"fish_count": 1, | ||
"fish": [ { | ||
"pixel_analysis_failed": False, | ||
"bbox": [1,2,3,4], | ||
"has_eye": True, | ||
"eye_center": [50, 50], | ||
"side": "left", | ||
"foreground": { | ||
"mean": 100.1111111, | ||
}, | ||
"background": { | ||
"mean": 200.1111111, | ||
} | ||
} | ||
], | ||
"has_ruler": True, | ||
"ruler_bbox": [ | ||
5, | ||
6, | ||
7, | ||
8 | ||
], | ||
} | ||
} | ||
|
||
BGNN_EXPECTED_METADATA = { | ||
'base_name': 'SOME-FISH-1234', | ||
'fish': {'angle_degree': 'None', | ||
'background_mean': 200.11, | ||
'bbox': [1, 2, 3, 4], | ||
'eye_bbox': 'None', | ||
'eye_center': [50, 50], | ||
'eye_direction': 'left', | ||
'fish_num': 1, | ||
'foreground_mean': 100.11, | ||
'pixel_analysis': True, | ||
'rescale': 'None'}, | ||
'ruler': {'bbox': [5, 6, 7, 8], 'scale': 'None', 'unit': 'None'}, | ||
'version': 'from drexel' | ||
} | ||
|
||
|
||
class TestFormatter(unittest.TestCase): | ||
def test_reformat_for_bgnn(self): | ||
result = reformat_for_bgnn(DM_INPUT_METADATA) | ||
self.assertEqual(result, BGNN_EXPECTED_METADATA) | ||
|
||
|
||
|
||
if __name__ == '__main__': | ||
unittest.main() |