-
Notifications
You must be signed in to change notification settings - Fork 40
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
Add Helper functions #266
Open
michaelbornholdt
wants to merge
11
commits into
cytomining:tf2
Choose a base branch
from
michaelbornholdt:helper_functions
base: tf2
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Add Helper functions #266
Changes from 7 commits
Commits
Show all changes
11 commits
Select commit
Hold shift + click to select a range
77f1ea6
init
michaelbornholdt 4bd6fd3
First very bad version of check profile
michaelbornholdt 6e14bcd
add check profile to cli
michaelbornholdt 7a7b16a
start test function
michaelbornholdt d642c80
first version. only works locally
michaelbornholdt 78c3613
Updated Profiling
michaelbornholdt 132a459
solve check_profile
michaelbornholdt 692b4b3
get helper ready
michaelbornholdt 3e493b4
delete the test file. Not needed
michaelbornholdt d596c61
finalize functions
michaelbornholdt 24a233e
Update .gitignore
michaelbornholdt 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 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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,80 @@ | ||
""" | ||
Helper functions for checking images, locations and crops before running profile and train. | ||
""" | ||
|
||
import pandas as pd | ||
import numpy as np | ||
import cv2 | ||
import os | ||
import deepprofiler.imaging.boxes | ||
|
||
|
||
def check_profile(dset): | ||
"""Checks images and location files to prepare for the profiling function. | ||
If this function runs correctly, the function 'profile' will also run without errors. | ||
|
||
Parameters | ||
---------- | ||
config : | ||
dset : | ||
|
||
Returns | ||
------- | ||
|
||
""" | ||
ls_imgs, ls_locs = [], [] | ||
os.makedirs('checks', exist_ok=True) | ||
|
||
frame = dset.meta.data.iterrows() | ||
images = [dset.get_image_paths(r) for i, r in frame] | ||
for channels in images: | ||
for img in channels[1]: | ||
if not os.path.isfile(img): | ||
ls_imgs.append(img) | ||
print('found {} missing images'.format(len(ls_imgs)), '|| saving list of missing files to checks/') | ||
pd.DataFrame(ls_imgs, columns=['missing_images']).to_csv('checks/missing_images.csv', index=False) | ||
|
||
# start checking location files | ||
frame = dset.meta.data.iterrows() | ||
for i, r in frame: | ||
df = deepprofiler.imaging.boxes.get_single_cell_locations("{}/{}-{}".format(r["Metadata_Plate"], r["Metadata_Well"], r["Metadata_Site"]), dset.config) | ||
if df.empty: | ||
ls_locs.append("{}/{}-{}".format(r["Metadata_Plate"], r["Metadata_Well"], r["Metadata_Site"])) | ||
|
||
print('found {} missing location files'.format(len(ls_locs)), '|| saving list of missing files to checks/') | ||
pd.DataFrame(ls_locs, columns=['missing_locs']).to_csv('checks/missing_locs.csv', index=False) | ||
|
||
return ls_imgs, ls_locs | ||
|
||
|
||
""" | ||
Checking all crops before training. | ||
""" | ||
|
||
def crop_checks(ls_missing, ls_zero, img_name, sample_dir): | ||
if not os.path.isfile(os.path.join(sample_dir, img_name)): | ||
ls_missing.append(img_name) | ||
else: | ||
img = cv2.imread(os.path.join(sample_dir, img_name), cv2.IMREAD_GRAYSCALE) | ||
pos = np.nonzero(img) | ||
if len(pos[0]) == 0: | ||
ls_zero.append(img_name) | ||
|
||
|
||
def check_train(dset): | ||
"""Check all crops before training in order to avoid errors during training. | ||
|
||
Returns | ||
------- | ||
|
||
""" | ||
# print(dset.meta.data.columns) | ||
# First check if images exist | ||
df = # read sc-metadata file | ||
|
||
ls_missing = [] | ||
ls_zero = [] | ||
res = df.apply(lambda row: crop_checks(ls_missing, ls_zero, row['Image_Name'], crops_dir), axis = 1) | ||
|
||
pd.DataFrame(ls_missing, columns=['missing_crops']).to_csv('missing_crops.csv', index=False) | ||
pd.DataFrame(ls_zero, columns=['zero_crops']).to_csv('zero_crops.csv', index=False) |
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,35 @@ | ||
import sys | ||
import pandas as pd | ||
import tempfile | ||
import os | ||
|
||
import deepprofiler.dataset.helper | ||
|
||
|
||
# Dont know how to test with CLI input? | ||
|
||
tempdir = tempfile.TemporaryFile() | ||
|
||
def test_check_profile(): | ||
dset = [] | ||
deepprofiler.dataset.helper.check_profile(dset) | ||
df = pd.read_csv('missing_images.csv') | ||
print('Missing images:') | ||
print(df.missing_images.tolist()) | ||
assert len(df) == 0 | ||
|
||
|
||
def test_check_training(): | ||
dset = [] | ||
deepprofiler.dataset.helper.check_training(dset) | ||
miss_crops = pd.read_csv('missing_crops.csv') | ||
print('Missing crops:') | ||
print(miss_crops.missing_crops.tolist()) | ||
|
||
zero_crops = pd.read_csv('zero_crops.csv') | ||
print('Zero crops:') | ||
print(zero_crops.zero_crops.tolist()) | ||
assert len(miss_crops) == 0 | ||
assert len(zero_crops) == 0 | ||
|
||
|
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.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@Arkkienkeli Can you tell me how to read the sc metadata here?