Skip to content

Commit

Permalink
Merge pull request #25 from hdr-bgnn/Last_dev
Browse files Browse the repository at this point in the history
Last dev
  • Loading branch information
thibaulttabarin authored Jul 25, 2022
2 parents e0136b1 + cff6086 commit e1f5cb6
Show file tree
Hide file tree
Showing 10 changed files with 2,652 additions and 276 deletions.
1,477 changes: 1,416 additions & 61 deletions Scripts/.ipynb_checkpoints/Morphology_dev-checkpoint.ipynb

Large diffs are not rendered by default.

920 changes: 870 additions & 50 deletions Scripts/Morphology_dev.ipynb

Large diffs are not rendered by default.

44 changes: 42 additions & 2 deletions Scripts/Morphology_main.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
@author: thibault
"""
import Traits_class as tc
import json, sys
import json, sys, math
import numpy as np

def get_scale(metadata_file):
Expand All @@ -28,6 +28,28 @@ def get_scale(metadata_file):
unit =[None]
return scale , unit


def get_angle(metadata_file):

'''
Calculate fish orientation from metadata file using major axis
return value in degree
'''
f = open(metadata_file)
data = json.load(f)
metadata_fish = list(data.values())[0]['fish'][0]

major = []
length = []

if 'primary_axis' in metadata_fish :

major = metadata_fish['primary_axis']
fish_angle = math.atan2(major[1], -major[0])*(180/math.pi)

return round(fish_angle,2)

# this class is used by json.dump to control that every value as the right format
# particular problem encounter with np.int64 value type
class NpEncoder(json.JSONEncoder):
Expand All @@ -44,17 +66,35 @@ def default(self, obj):
def main(input_file, metadata_file, output_measure, output_landmark, output_presence,
output_lm_image=None):

# Create the image segmentation object
img_seg = tc.segmented_image(input_file)
measurement = img_seg.measurement
base_name = img_seg.base_name
# Calcualte the mesaurements and landmarks
img_seg.get_all_measures_landmarks()

# Assign variables
measurements_bbox = img_seg.measurement_with_bbox
measurements_lm = img_seg.measurement_with_lm
measurements_area = img_seg.measurement_with_area
landmark = img_seg.landmark
presence_matrix = img_seg.presence_matrix

# Combine the 3 types of measurements (lm, bbox, area) and reorder the keys
measurement = {'base_name': base_name, **measurements_bbox, **measurements_lm, **measurements_area }
list_measure= ['base_name', 'SL_bbox', 'SL_lm', 'HL_bbox', 'HL_lm', 'pOD_bbox', 'pOD_lm', 'ED_bbox', 'ED_lm', 'HH_lm', 'EA_m','HA_m','FA_pca','FA_lm']
measurement = {k:measurement[k] for k in list_measure}

# Extract the scale from metadata file
# and add it to measurement dict
scale , unit = get_scale(metadata_file)
measurement['scale'] = scale
measurement['unit'] = unit

# Extract the fish angle from metadata file
# and add it to measurement dict
fish_angle = get_angle(metadata_file)
measurement['FA_pca_meta'] = fish_angle

# Save the dictionnaries in json file
# use NpEncoder to convert the value to correct type (np.int64 -> int)
with open(output_measure, 'w') as f:
Expand Down
Loading

0 comments on commit e1f5cb6

Please sign in to comment.