Skip to content
GitLab
Menu
Projects
Groups
Snippets
Help
Help
Support
Community forum
Keyboard shortcuts
?
Submit feedback
Contribute to GitLab
Sign in
Toggle navigation
Menu
Open sidebar
Saad Jbabdi
CellCounting
Commits
f42b6103
Commit
f42b6103
authored
Sep 20, 2018
by
Saad Jbabdi
Browse files
Delete #create_db.py#
parent
1a2fb61b
Changes
1
Hide whitespace changes
Inline
Side-by-side
CellCounting/click_cells/#create_db.py#
deleted
100755 → 0
View file @
1a2fb61b
#!/usr/bin/env python3
import
argparse
import
numpy
as
np
import
pandas
as
pd
import
re
from
CellCounting.Utils.db
import
DataBase
DB_IMAGE_RES
=
64
def
check_imshape
(
shape
):
sx
,
sy
,
_
=
shape
if
sx
!=
sy
:
return
False
if
(
sx
%
DB_IMAGE_RES
!=
0
)
or
(
sy
%
DB_IMAGE_RES
!=
0
):
return
False
return
True
def
append_file_content
(
fname
,
image_list
,
count_list
):
df
=
pd
.
read_table
(
fname
)
udf
=
df
.
groupby
(
'Sub-Image-File'
).
count
()
for
f
in
udf
.
index
:
# Load Numpy array
im
=
np
.
load
(
f
.
strip
())
if
not
check_imshape
(
im
.
shape
):
print
(
"Error: Bad Image dimensions. Must be square and multiple of {}"
.
format
(
DB_IMAGE_RES
))
sizx
,
sizy
,
_
=
im
.
shape
# Split into sub-zones
size_ratio
=
sizx
//
DB_IMAGE_RES
im2
=
im
.
reshape
(
size_ratio
,
sizx
//
size_ratio
,
size_ratio
,
sizy
//
size_ratio
,
3
)
im3
=
im2
.
transpose
(
0
,
2
,
1
,
3
,
4
).
reshape
(
size_ratio
**
2
,
sizx
//
size_ratio
,
sizy
//
size_ratio
,
3
)
image_list
.
append
(
im3
)
res
=
re
.
findall
(
"w_(\d+).(\d+)_h_(\d+).(\d+)"
,
f
)[
0
]
W
=
round
(
float
(
res
[
0
]
+
"."
+
res
[
1
]))
H
=
round
(
float
(
res
[
2
]
+
"."
+
res
[
3
]))
count_cells
=
np
.
zeros
((
size_ratio
,
size_ratio
),
dtype
=
int
)
for
indiv_cells
in
df
.
values
[
df
[
'Sub-Image-File'
]
==
f
]:
if
np
.
isnan
(
indiv_cells
[
1
]):
pass
else
:
w
=
float
(
indiv_cells
[
1
])
-
W
h
=
float
(
indiv_cells
[
2
])
-
H
count_cells
[
int
(
w
//
(
sizx
/
size_ratio
)),
int
(
h
//
(
sizy
/
size_ratio
))]
+=
1
count_list
.
append
(
count_cells
.
flatten
())
return
len
(
udf
.
index
)
def
create_db
(
file_list
,
outfile
):
image_list
=
[]
count_list
=
[]
total
=
0
for
f
in
file_list
:
total
+=
append_file_content
(
f
,
image_list
,
count_list
)
shape
=
image_list
[
0
].
shape
[
1
:]
count_list
=
np
.
array
(
count_list
).
flatten
()
np
.
savez
(
outfile
,
counts
=
count_list
,
images
=
np
.
array
(
image_list
).
reshape
(
-
1
,
*
shape
))
print
(
"Created DB with {} Images from a list of {} with {} containing cells."
.
format
(
len
(
count_list
),
total
,(
count_list
>
0
).
sum
()))
def
main
():
# Parse command line arguments
parser
=
argparse
.
ArgumentParser
(
"Create DB from clicked textfiles"
)
parser
.
add_argument
(
"outfile"
,
help
=
"Output file name"
)
parser
.
add_argument
(
"file"
,
help
=
"Clicky text file"
,
nargs
=
'+'
)
args
=
parser
.
parse_args
()
create_db
(
args
.
file
,
args
.
outfile
)
if
__name__
==
'__main__'
:
main
()
Write
Preview
Markdown
is supported
0%
Try again
or
attach a new file
.
Attach a file
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Cancel
Please
register
or
sign in
to comment