Skip to content

Commit

Permalink
Apply ruff unsafe fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
ElliottKasoar committed Nov 13, 2024
1 parent c06ef3c commit 3bcacae
Show file tree
Hide file tree
Showing 17 changed files with 80 additions and 270 deletions.
8 changes: 0 additions & 8 deletions abcd/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,11 +60,3 @@ def from_url(cls, url, **kwargs):
url = "mongodb://mongoadmin:secret@localhost:27017/abcd_new"
abcd = ABCD.from_url(url)
abcd.print_info()

# from ase.io import iread
# for atoms in iread('../tutorials/data/bcc_bulk_54_expanded_2_high.xyz', index=slice(1)):
# # Hack to fix the representation of forces
# atoms.calc.results['forces'] = atoms.arrays['force']
#
# abcd.push(atoms)
# print(atoms)
9 changes: 3 additions & 6 deletions abcd/backends/atoms_http.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
import json
import logging
from os import linesep
from typing import List

import ase
import requests
Expand Down Expand Up @@ -45,14 +44,12 @@ def pull(self, query=None, properties=None):
def query(self, query_string):
pass

def search(self, query_string: str) -> List[str]:
results = requests.get(self.url + "/calculation").json()
return results
def search(self, query_string: str) -> list[str]:
return requests.get(self.url + "/calculation").json()

def get_atoms(self, id: str) -> Atoms:
data = requests.get(self.url + f"/calculation/{id}").json()
atoms = Atoms.from_dict(data)
return atoms
return Atoms.from_dict(data)

def __repr__(self):
return f"ABCD(type={self.__class__.__name__}, url={self.url}, ...)"
Expand Down
89 changes: 24 additions & 65 deletions abcd/backends/atoms_pymongo.py
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ def __init__(
collection_name="atoms",
username=None,
password=None,
authSource="admin",
auth_source="admin",
uri_mode=False,
**kwargs,
):
Expand All @@ -175,31 +175,31 @@ def __init__(
collection_name,
username,
password,
authSource,
auth_source,
kwargs,
)
)

if uri_mode:
self.client = MongoClient(host=host, authSource=authSource)
self.client = MongoClient(host=host, authSource=auth_source)
else:
self.client = MongoClient(
host=host,
port=port,
username=username,
password=password,
authSource=authSource,
authSource=auth_source,
)

try:
info = self.client.server_info() # Forces a call.
logger.info(f"DB info: {info}")

except pymongo.errors.OperationFailure:
raise abcd.errors.AuthenticationError()
except pymongo.errors.OperationFailure as err:
raise abcd.errors.AuthenticationError() from err

except pymongo.errors.ServerSelectionTimeoutError:
raise abcd.errors.TimeoutError()
except pymongo.errors.ServerSelectionTimeoutError as err:
raise abcd.errors.TimeoutError() from err

self.db = self.client[db_name]
self.collection = self.db[collection_name]
Expand Down Expand Up @@ -258,8 +258,7 @@ def upload(self, file: Path, extra_infos=None, store_calc=True):
def get_items(self, query=None):
# TODO: better method for aggregations
query = parser(query)
for dct in self.db.atoms.find(query):
yield dct
yield from self.db.atoms.find(query)

def get_atoms(self, query=None):
query = parser(query)
Expand Down Expand Up @@ -320,18 +319,18 @@ def properties(self, query=None):
return properties

def get_type_of_property(self, prop, category):
# TODO: Probably it would be nicer to store the type info in the database from the beginning.
# TODO: Store the type info in the database from the beginning?
atoms = self.db.atoms.find_one({prop: {"$exists": True}})
data = atoms[prop]

if category == "arrays":
if type(data[0]) == list:
if isinstance(data[0], list):
return f"array({map_types[type(data[0][0])]}, N x {len(data[0])})"
return f"vector({map_types[type(data[0])]}, N)"

if type(data) == list:
if type(data[0]) == list:
if type(data[0][0]) == list:
if isinstance(data, list):
if isinstance(data[0], list):
if isinstance(data[0][0], list):
return "list(list(...)"
return f"array({map_types[type(data[0][0])]})"
return f"vector({map_types[type(data[0])]})"
Expand Down Expand Up @@ -397,7 +396,8 @@ def add_property(self, data, query=None):

def rename_property(self, name, new_name, query=None):
logger.info(f"rename: query={query}, old={name}, new={new_name}")
# TODO name in derived.info_keys OR name in derived.arrays_keys OR name in derived.derived_keys
# TODO name in derived.info_keys OR name in derived.arrays_keys
# OR name in derived.derived_keys
self.collection.update_many(
parser(query), {"$push": {"derived.info_keys": new_name}}
)
Expand All @@ -407,15 +407,6 @@ def rename_property(self, name, new_name, query=None):
{"$pull": {"derived.info_keys": name}, "$rename": {name: new_name}},
)

# self.collection.update_many(
# parser(query + ['arrays.{}'.format(name)]),
# {'$push': {'derived.arrays_keys': new_name}})
#
# self.collection.update_many(
# parser(query + ['arrays.{}'.format(name)]),
# {'$pull': {'derived.arrays_keys': name},
# '$rename': {'arrays.{}'.format(name): 'arrays.{}'.format(new_name)}})

def delete_property(self, name, query=None):
logger.info(f"delete: query={name}, porperty={query}")

Expand All @@ -435,7 +426,7 @@ def exec(self, code, query=None):
# TODO: Separate python environment with its own packages loaded

for dct in self.get_items(query):
atoms = AtomsModel(self.collection, dct)
AtomsModel(self.collection, dct)
exec(code)

def __repr__(self):
Expand Down Expand Up @@ -483,30 +474,30 @@ def histogram(name, data, **kwargs):
print(f"Mixed type error of the {name} property!")
return None

if ptype == float:
if isinstance(data[0], float):
bins = kwargs.get("bins", 10)
return _hist_float(name, data, bins)

if ptype == int:
if isinstance(data[0], int):
bins = kwargs.get("bins", 10)
return _hist_int(name, data, bins)

if ptype == str:
if isinstance(data[0], str):
return _hist_str(name, data, **kwargs)

if ptype == datetime:
if isinstance(data[0], datetime):
bins = kwargs.get("bins", 10)
return _hist_date(name, data, bins)

print(f"{name}: Histogram for list of {type(data[0])} types are not supported!")
logger.info(
f"{name}: Histogram for list of {type(data[0])} types are not supported!"
)

else:
logger.info(f"{name}: Histogram for {type(data)} types are not supported!")
return None

logger.info(f"{name}: Histogram for {type(data)} types are not supported!")
return None


def _hist_float(name, data, bins=10):
data = np.array(data)
Expand Down Expand Up @@ -596,39 +587,7 @@ def _hist_str(name, data, bins=10, truncate=20):


if __name__ == "__main__":
# import json
# from ase.io import iread
# from pprint import pprint
# from server.styles.myjson import JSONEncoderOld, JSONDecoderOld, JSONEncoder

print("hello")
db = MongoDatabase(username="mongoadmin", password="secret")
print(db.info())
print(db.count())

print(db.hist("uploaded"))

# for atoms in iread('../../tutorials/data/bcc_bulk_54_expanded_2_high.xyz', index=slice(None)):
# # print(at)
# atoms.calc.results['forces'] = atoms.arrays['force']
# # at.arrays['force'] = None
#
# json_data = json.dumps(atoms, cls=JSONEncoderOld)
# print(json_data)
#
# atom_dict = json.loads(json_data, cls=JSONDecoderOld)
# print(atom_dict)
#
# print(atoms == atom_dict)
#
# with JSONEncoder() as encoder:
# data = encoder.encode(atoms)
#
# print(data)
#
# with DictEncoder() as encoder:
# data = encoder.encode(atoms)
#
# pprint(data)
#
# db.collection.insert_one(DictEncoder().encode(atoms))
4 changes: 2 additions & 2 deletions abcd/database.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from abc import ABCMeta, abstractmethod
# ruff: noqa: B024, B027
from abc import ABCMeta
import logging

logger = logging.getLogger(__name__)
Expand All @@ -7,7 +8,6 @@
class AbstractABCD(metaclass=ABCMeta):
"""Factory method"""

@abstractmethod
def __init__(self):
pass

Expand Down
12 changes: 6 additions & 6 deletions abcd/frontends/commandline/commands.py
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,8 @@ def key_delete(*, db, query, yes, keys, **kwargs):

if not yes:
print(
f"Please use --yes for deleting keys from {db.count(query=query)} configurations"
f"Please use --yes for deleting keys from {db.count(query=query)} "
"configurations"
)
exit(1)

Expand All @@ -232,7 +233,8 @@ def key_delete(*, db, query, yes, keys, **kwargs):
def execute(*, db, query, yes, python_code, **kwargs):
if not yes:
print(
f"Please use --yes for executing code on {db.count(query=query)} configurations"
f"Please use --yes for executing code on {db.count(query=query)} "
"configurations"
)
exit(1)

Expand All @@ -258,16 +260,14 @@ def server(*, abcd_url, url, api_only, **kwargs):


class Formater:
partialBlocks = ["▏", "▎", "▍", "▌", "▋", "▊", "▉", "█"] # char=pb

def title(self, title):
print("", title, "=" * len(title), sep=os.linesep)

def describe(self, data):
if data["type"] == "hist_float":
print(
"{} count: {} min: {:11.4e} med: {:11.4e} max: {:11.4e} std: {:11.4e} var:{"
":11.4e}".format(
"{} count: {} min: {:11.4e} med: {:11.4e} max: {:11.4e} std: {:11.4e} "
"var:{:11.4e}".format(
data["name"],
sum(data["counts"]),
data["min"],
Expand Down
4 changes: 1 addition & 3 deletions abcd/frontends/commandline/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -32,9 +32,7 @@ def load(cls):

logger.info(f"Using config file: {file}")

config = cls.from_json(file)

return config
return cls.from_json(file)

def save(self):
file = (
Expand Down
16 changes: 0 additions & 16 deletions abcd/frontends/commandline/parser.py
Original file line number Diff line number Diff line change
Expand Up @@ -234,20 +234,4 @@ def main(args=None):
if __name__ == "__main__":
main(["summary"])
main("delete-key -q pbc pbc".split())
# main('upload -e cas -i ../../../tutorials/GB_alphaFe_001/tilt/00110391110_v6bxv2_tv0.4bxv0.2_d1.6z_traj.xyz'.split())
# main('summary -q formula~"Si2"'.split())
# main('upload -e cas -i ../../../tutorials/GB_alphaFe_001/tilt/00110391110_v6bxv2_tv0.4bxv0.2_d1.6z_traj.xyz'.split())
# main('-v login mongodb://mongoadmin:secret@localhost:27017/abcd'.split())
# main('-v summary'.split())
# main('-v summary -p energy'.split())
# main('-v summary -p *'.split())
# main('add-key -q cas selected user="cas"'.split())
main("delete-key user".split())
# main(['summary', '-p', '*'])
# main(['summary', '-p', 'info.config_name, info.energy'])
# main(['summary', '-p', 'info.config_name, info.energy,info.energy;info.energy info.energy'])
# main(['-s', 'fancy', 'summary', '-p', '*'])
# main(['summary', '-p', '*'])
# main(['-s', 'fancy', 'summary'])
# main(['-v', 'summary', '-p', 'config_type', '-p', 'haha', '-p' 'sdgsdg, dsgsdg,asd fgg', '-q', 'config_type~bcc',
# '-q', 'config_type~bcc'])
29 changes: 11 additions & 18 deletions abcd/model.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,8 +12,8 @@


class Hasher:
def __init__(self, method=md5()):
self.method = method
def __init__(self, method=md5):
self.method = method()

def update(self, value):
if isinstance(value, int):
Expand Down Expand Up @@ -86,7 +86,9 @@ def __getitem__(self, key):

def __setitem__(self, key, value):
if key == "derived":
# raise KeyError('Please do not use "derived" as key because it is protected!')
# raise KeyError(
# 'Please do not use "derived" as key because it is protected!'
# )
# Silent return to avoid raising error in pymongo package
return

Expand All @@ -96,7 +98,8 @@ def __setitem__(self, key, value):
super().__setitem__(key, value)

def convert(self, value):
# TODO: https://api.mongodb.com/python/current/api/bson/index.html using type_registry
# TODO: https://api.mongodb.com/python/current/api/bson/index.html
# using type_registry

if isinstance(value, np.int64):
return int(value)
Expand All @@ -105,7 +108,9 @@ def convert(self, value):

def update_key_category(self, key, value):
if key == "_id":
# raise KeyError('Please do not use "derived" as key because it is protected!')
# raise KeyError(
# 'Please do not use "derived" as key because it is protected!'
# )
return

for category in ("arrays_keys", "info_keys", "results_keys", "derived_keys"):
Expand Down Expand Up @@ -136,8 +141,7 @@ def __delitem__(self, key):
super().__delitem__(key)

def __iter__(self):
for item in super().__iter__():
yield item
yield from super().__iter__()
yield "derived"

@classmethod
Expand Down Expand Up @@ -317,14 +321,3 @@ def pre_save(self):

model = AbstractModel.from_atoms(atoms)
print(model.to_ase())

# xyz = io.StringIO(
# """
# 2
# Properties=species:S:1:pos:R:3 s="sadf" _vtk_test="t e s t _ s t r" pbc="F F F"
# Si 0.00000000 0.00000000 0.00000000
# Si 0.00000000 0.00000000 0.00000000
#
# """)
#
# atoms = read(xyz, format='extxyz', index=0)
Loading

0 comments on commit 3bcacae

Please sign in to comment.