Page Menu
Home
c4science
Search
Configure Global Search
Log In
Files
F99003839
add_columns.py
No One
Temporary
Actions
Download File
Edit File
Delete File
View Transforms
Subscribe
Mute Notifications
Award Token
Subscribers
None
File Metadata
Details
File Info
Storage
Attached
Created
Sat, Jan 18, 09:36
Size
3 KB
Mime Type
text/x-python
Expires
Mon, Jan 20, 09:36 (1 d, 23 h)
Engine
blob
Format
Raw Data
Handle
23687888
Attached To
R13271 Optical_Trapping_ML
add_columns.py
View Options
from
DM_communication
import
get_docs_VM
,
update_one_VM
#code to add the gram type features
#dictionary of bacteria and their Gram classification
bacteria_gram_dict
=
{
'bs134'
:
1
,
# Gram-positive
'li142'
:
1
,
'ys134'
:
0
,
# Gram-negative
'pp46'
:
0
,
'coli'
:
0
,
'ns139'
:
0
,
'pp6'
:
0
,
'se26'
:
1
,
'se9'
:
1
,
}
#let's add a shape column
bacteria_shape_dict
=
{
'bs134'
:
0
,
# Baccilus
'li142'
:
0
,
'ys134'
:
0
,
'pp46'
:
0
,
'coli'
:
0
,
'ns139'
:
1
,
#Coccus
'pp6'
:
0
,
'se26'
:
1
,
'se9'
:
1
,
}
def
add_shape_to_docs
(
db_name
,
coll_name
,
bacteria_shape_dict
):
docs
=
get_docs_VM
(
db_name
,
coll_name
,
query
=
{})
for
doc
in
docs
:
bacteria_name
=
doc
.
get
(
'bacteria'
,
None
)
if
bacteria_name
is
None
:
print
(
f
"Document {doc['_id']} does not have a 'bacteria' field."
)
continue
shape_type
=
bacteria_shape_dict
.
get
(
bacteria_name
,
None
)
if
shape_type
is
not
None
:
filt
=
{
'_id'
:
doc
[
'_id'
]}
update_one_VM
(
db_name
,
coll_name
,
filt
,
{
"$set"
:
{
"shape_type"
:
shape_type
}})
else
:
print
(
f
"Bacteria name {bacteria_name} not found in dictionary."
)
print
(
"Documents updated with shape_type."
)
def
print_shape_types
(
db_name
,
coll_name
):
#lets test our new function
#fetch all documents to print gram types
docs
=
get_docs_VM
(
db_name
,
coll_name
,
query
=
{})
for
doc
in
docs
:
bacteria_name
=
doc
.
get
(
'bacteria'
,
None
)
shape_type
=
doc
.
get
(
'shape_type'
,
None
)
print
(
f
"Bacteria: {bacteria_name}, shape type: {shape_type}"
)
#loop through the dictionnary to add a key gram_type from known list of bacteria
def
add_gram_type_to_docs
(
db_name
,
coll_name
,
bacteria_gram_dict
):
docs
=
get_docs_VM
(
db_name
,
coll_name
,
query
=
{})
for
doc
in
docs
:
bacteria_name
=
doc
.
get
(
'bacteria'
,
None
)
if
bacteria_name
is
None
:
print
(
f
"Document {doc['_id']} does not have a 'bacteria' field."
)
continue
gram_type
=
bacteria_gram_dict
.
get
(
bacteria_name
,
None
)
if
gram_type
is
not
None
:
filt
=
{
'_id'
:
doc
[
'_id'
]}
update_one_VM
(
db_name
,
coll_name
,
filt
,
{
"$set"
:
{
"gram_type"
:
gram_type
}})
else
:
print
(
f
"Bacteria name {bacteria_name} not found in dictionary."
)
print
(
"Documents updated with gram_type."
)
def
print_gram_types
(
db_name
,
coll_name
):
#lets test our new function
#fetch all documents to print gram types
docs
=
get_docs_VM
(
db_name
,
coll_name
,
query
=
{})
for
doc
in
docs
:
bacteria_name
=
doc
.
get
(
'bacteria'
,
None
)
gram_type
=
doc
.
get
(
'gram_type'
,
None
)
print
(
f
"Bacteria: {bacteria_name}, Gram type: {gram_type}"
)
if
__name__
==
'__main__'
:
#add_gram_type_to_docs('Optical_Trapping_ML_DB', 'tseries_Gram_analysis', bacteria_gram_dict)
#print_gram_types('Optical_Trapping_ML_DB', 'tseries_Gram_analysis')
add_shape_to_docs
(
'Optical_Trapping_ML_DB'
,
'tseries_Gram_analysis'
,
bacteria_shape_dict
)
print_shape_types
(
'Optical_Trapping_ML_DB'
,
'tseries_Gram_analysis'
)
Event Timeline
Log In to Comment