Skip to content
GitLab
Explore
Sign in
Primary navigation
Search or go to…
Project
P
PyNutil
Manage
Activity
Members
Labels
Plan
Issues
0
Issue boards
Milestones
Wiki
Code
Merge requests
0
Repository
Branches
Commits
Tags
Repository graph
Compare revisions
Snippets
Build
Pipelines
Jobs
Pipeline schedules
Artifacts
Deploy
Releases
Package Registry
Container Registry
Operate
Environments
Terraform modules
Monitor
Incidents
Analyze
Value stream analytics
Contributor analytics
CI/CD analytics
Repository analytics
Help
Help
Support
GitLab documentation
Compare GitLab plans
Community forum
Contribute to GitLab
Provide feedback
Keyboard shortcuts
?
Snippets
Groups
Projects
Show more breadcrumbs
Harry Carey
PyNutil
Commits
eedb4afa
Commit
eedb4afa
authored
1 year ago
by
Sharon Yates
Browse files
Options
Downloads
Patches
Plain Diff
Create PixelCountPerRegion function
parent
add3d2c2
No related branches found
Branches containing commit
No related tags found
No related merge requests found
Changes
2
Hide whitespace changes
Inline
Side-by-side
Showing
2 changed files
PyNutil/coordinate_extraction.py
+38
-0
38 additions, 0 deletions
PyNutil/coordinate_extraction.py
PyNutil/folder_of_segmentations_to_meshview_multithreaded.py
+4
-49
4 additions, 49 deletions
PyNutil/folder_of_segmentations_to_meshview_multithreaded.py
with
42 additions
and
49 deletions
PyNutil/coordinate_extraction.py
+
38
−
0
View file @
eedb4afa
import
numpy
as
np
import
pandas
as
pd
from
DeepSlice.coord_post_processing.spacing_and_indexing
import
number_sections
import
json
from
read_and_write
import
loadVisuAlignJson
...
...
@@ -270,3 +271,40 @@ def labelPoints(points, label_volume, scale_factor=1):
labels
=
label_volume
[
x
,
y
,
z
]
return
labels
# related to object_counting
# consider separating out writing to CSV in future
def
PixelCountPerRegion
(
labelsDict
,
label_colours
,
output_csv
):
"""
Function for counting no. of pixels per region and writing to CSV based on
a dictionary with the region as the key and the points as the value,
"""
counted_labels
,
label_counts
=
np
.
unique
(
labelsDict
,
return_counts
=
True
)
# which regions have pixels, and how many pixels are there per region
counts_per_label
=
list
(
zip
(
counted_labels
,
label_counts
))
# create a list of unique regions and pixel counts per region
df_counts_per_label
=
pd
.
DataFrame
(
counts_per_label
,
columns
=
[
"
allenID
"
,
"
pixel count
"
])
# create a pandas df with regions and pixel counts
df_label_colours
=
pd
.
read_csv
(
label_colours
,
sep
=
"
,
"
)
# find colours corresponding to each region ID and add to the pandas dataframe
#look up name, r, g, b in df_allen_colours in df_counts_per_label based on "allenID"
new_rows
=
[]
for
index
,
row
in
df_counts_per_label
.
iterrows
():
mask
=
df_label_colours
[
"
allenID
"
]
==
row
[
"
allenID
"
]
current_region_row
=
df_label_colours
[
mask
]
current_region_name
=
current_region_row
[
"
name
"
].
values
current_region_red
=
current_region_row
[
"
r
"
].
values
current_region_green
=
current_region_row
[
"
g
"
].
values
current_region_blue
=
current_region_row
[
"
b
"
].
values
row
[
"
name
"
]
=
current_region_name
[
0
]
row
[
"
r
"
]
=
current_region_red
[
0
]
row
[
"
g
"
]
=
current_region_green
[
0
]
row
[
"
b
"
]
=
current_region_blue
[
0
]
new_rows
.
append
(
row
)
df_counts_per_label_name
=
pd
.
DataFrame
(
new_rows
)
df_counts_per_label_name
.
to_csv
(
output_csv
,
sep
=
"
;
"
,
na_rep
=
''
,
index
=
False
)
This diff is collapsed.
Click to expand it.
PyNutil/folder_of_segmentations_to_meshview_multithreaded.py
+
4
−
49
View file @
eedb4afa
...
...
@@ -10,12 +10,12 @@ import json
from
datetime
import
datetime
#import json into "input" variable, use to define input parameters
with
open
(
'
../test/test
1
.json
'
,
'
r
'
)
as
f
:
with
open
(
'
../test/test
2
.json
'
,
'
r
'
)
as
f
:
input
=
json
.
load
(
f
)
#print(input)
#import our function for converting a folder of segmentations to points
from
coordinate_extraction
import
FolderToAtlasSpace
,
labelPoints
,
WritePointsToMeshview
,
FolderToAtlasSpaceMultiThreaded
from
coordinate_extraction
import
FolderToAtlasSpace
,
labelPoints
,
WritePointsToMeshview
,
FolderToAtlasSpaceMultiThreaded
,
PixelCountPerRegion
#from read_and_write import WritePointsToMeshview
startTime
=
datetime
.
now
()
...
...
@@ -37,58 +37,13 @@ labels = labelPoints(points, data, scale_factor=2.5)
#save points to a meshview json
WritePointsToMeshview
(
points
,
labels
,
input
[
"
points_json_path
"
],
label_df
)
#SY Task:
# function for counting no. of objects per region
# Make a pandas dataframe
# Column 1: counted_labels
# Column 2: label_counts
# Column 3: region_name (look up by reading Allen2022_colours.csv, look up name and RGB)
# Save dataframe in output as CSV
# next task is to create functions from this.
counted_labels
,
label_counts
=
np
.
unique
(
labels
,
return_counts
=
True
)
counts_per_label
=
list
(
zip
(
counted_labels
,
label_counts
))
df_counts_per_label
=
pd
.
DataFrame
(
counts_per_label
,
columns
=
[
"
allenID
"
,
"
pixel count
"
])
df_allen_colours
=
pd
.
read_csv
(
input
[
"
allen_colours
"
],
sep
=
"
,
"
)
df_allen_colours
#look up name, r, g, b in df_allen_colours in df_counts_per_label based on "allenID"
new_rows
=
[]
for
index
,
row
in
df_counts_per_label
.
iterrows
():
mask
=
df_allen_colours
[
"
allenID
"
]
==
row
[
"
allenID
"
]
current_region_row
=
df_allen_colours
[
mask
]
current_region_name
=
current_region_row
[
"
name
"
].
values
current_region_red
=
current_region_row
[
"
r
"
].
values
current_region_green
=
current_region_row
[
"
g
"
].
values
current_region_blue
=
current_region_row
[
"
b
"
].
values
row
[
"
name
"
]
=
current_region_name
[
0
]
row
[
"
r
"
]
=
current_region_red
[
0
]
row
[
"
g
"
]
=
current_region_green
[
0
]
row
[
"
b
"
]
=
current_region_blue
[
0
]
new_rows
.
append
(
row
)
df_counts_per_label_name
=
pd
.
DataFrame
(
new_rows
)
df_counts_per_label_name
# write to csv file
df_counts_per_label_name
.
to_csv
(
input
[
"
counts_per_label_name
"
],
sep
=
"
;
"
,
na_rep
=
''
,
index
=
False
)
#r = df_allen_colours["r"]
#g = df_allen_colours["g"]
#b = df_allen_colours["b"]
#region_name = df_allen_colours["name"]
PixelCountPerRegion
(
labels
,
input
[
"
allen_colours
"
],
input
[
"
counts_per_label_name
"
])
#while we havent added it here it would be good to next quantify the number of cells for each label.
time_taken
=
datetime
.
now
()
-
startTime
print
(
f
"
time taken was:
{
time_taken
}
"
)
print
(
f
"
overall
time taken was:
{
time_taken
}
"
)
#get centroids and areas returns a list of objects and the center coordinate.
...
...
This diff is collapsed.
Click to expand it.
Preview
0%
Try again
or
attach a new file
.
Cancel
You are about to add
0
people
to the discussion. Proceed with caution.
Finish editing this message first!
Save comment
Cancel
Please
register
or
sign in
to comment