I have the following code to annotate a graph using property maps:
from graph_tool.all import *
# define graph
g = Graph()
g.set_directed(True)
species = g.new_vertex_property("string")
species_dict = {}
reaction_dict = {}
#add species and reactions
s1 = g.add_vertex()
species[s1] = 'limonene'
species_dict[g.vertex_index[s1]] = 'limonene'
g.vertex_properties["species"] = species
g.vp.species[s1]
When I run this I obtain the following error message:
File "/home/pmj27/projects/NOC/exergy/make_graph.py", line 45, in <module>
g.vp.species[s1]
AttributeError: 'PropertyDict' object has no attribute 'species'
Why is this? If I type g.vp into my IPython console I get {'species': <PropertyMap object with key type 'Vertex' and value type 'string', for Graph 0x7f285d90ea10, at 0x7f285d90ef90>} as answer, so there clearly is a property map.
The access to property maps via attributes (as g.vp.species[s1] in your example) is only available in more recent versions of graph-tool (currently 2.11, as of Nov 2015). In the version you are using (2.2.42), you must use the dictionary interface: g.vp["species"][s1].
Related
We can do the following to create a connection, and then attached the connection to the graph g object, and then use g to mirror gremlin query inline.
from gremlin_python import statics
from gremlin_python.structure.graph import Graph
from gremlin_python.process.graph_traversal import __
from gremlin_python.driver.driver_remote_connection import DriverRemoteConnection
Create a GraphTraversalSource which is the basis for all Gremlin traversals:
graph = Graph()
connection = DriverRemoteConnection('ws://localhost:8182/gremlin', 'g')
g = graph.traversal().withRemote(connection)
g.V().limit(2).toList()
However, I want to submit string grelmin query like below,
connection = DriverRemoteConnection('ws://localhost:8182/gremlin', 'g')
query = "g.V().limit(2).toList()"
connection.submit(query)
Then I'm getting the following error. Looks like I did NOT call the submit() function correctly, and I can't find any docs or examples on this function. Please help.
[ERROR] AttributeError: 'str' object has no attribute 'source_instructions'
Traceback (most recent call last):
File "/var/task/sentry_sdk/integrations/aws_lambda.py", line 152, in sentry_handler
return handler(aws_event, aws_context, *args, **kwargs)
response = remoteConn.submit(query)
File "/var/task/gremlin_python/driver/driver_remote_connection.py", line 56, in submit
result_set = self._client.submit(bytecode, request_options=self._extract_request_options(bytecode))
File "/var/task/gremlin_python/driver/driver_remote_connection.py", line 81, in _extract_request_options
options_strategy = next((x for x in bytecode.source_instructions
Here is an example of calling submit from Gremlin Python, you need to create the connection a slightly different way:
client = client.Client('ws://localhost:8182/gremlin','g')
query = """
g.V().hasLabel('airport').
sample(30).
order().by('code').
local(__.values('code','city').fold()).
toList()
"""
result = client.submit(query)
future_results = result.all()
results = future_results.result()
client.close()
The full example is here
I have been following this guide for implementing a Detectron2 model on Sagemaker.
It all looks good, both on the training and the batch transform side.
However, I tried to tweak a bit the code to create an Endpoint that can be invoked by sending a payload, and I am having some troubles with it.
At the end of this notebook, after creating the SageMaker model object:
model = PyTorchModel(
name="d2-sku110k-model",
model_data=training_job_artifact,
role=role,
sagemaker_session=sm_session,
entry_point="predict_sku110k.py",
source_dir="container_serving",
image_uri=serve_image_uri,
framework_version="1.6.0",
code_location=f"s3://{bucket}/{prefix_code}",
)
I added the following code:
predictor = model.deploy(initial_instance_count=1, instance_type='ml.m5.xlarge')
And I can see that the model has been successfully deployed.
However, when I try to predict an image with :
predictor.predict(input)
I get the following error:
ModelError: An error occurred (ModelError) when calling the InvokeEndpoint operation: Received server error (500) from primary with message "Type [application/x-npy] not support this type yet
Traceback (most recent call last):
File "/opt/conda/lib/python3.6/site-packages/sagemaker_inference/transformer.py", line 126, in transform
result = self._transform_fn(self._model, input_data, content_type, accept)
File "/opt/conda/lib/python3.6/site-packages/sagemaker_inference/transformer.py", line 215, in _default_transform_fn
data = self._input_fn(input_data, content_type)
File "/opt/ml/model/code/predict_sku110k.py", line 98, in input_fn
raise ValueError(err_msg)
ValueError: Type [application/x-npy] not support this type yet
I tried a bunch of different input types: a image byte-encoded (created with cv2.imencode('.jpg', cv_img)[1].tobytes()), a numpy array, a BytesIO object (created with io module), a dictionary of the form {'input': image} where image is any of the previous (this is because this format was used by a tensorflow endpoint I created some time ago).
As I think it might be relevant, I also copy paste here the Inference script used as entry point:
"""Code used for sagemaker batch transform jobs"""
from typing import BinaryIO, Mapping
import json
import logging
import sys
from pathlib import Path
import numpy as np
import cv2
import torch
from detectron2.engine import DefaultPredictor
from detectron2.config import CfgNode
##############
# Macros
##############
LOGGER = logging.Logger("InferenceScript", level=logging.INFO)
HANDLER = logging.StreamHandler(sys.stdout)
HANDLER.setFormatter(logging.Formatter("%(levelname)s | %(name)s | %(message)s"))
LOGGER.addHandler(HANDLER)
##########
# Deploy
##########
def _load_from_bytearray(request_body: BinaryIO) -> np.ndarray:
npimg = np.frombuffer(request_body, np.uint8)
return cv2.imdecode(npimg, cv2.IMREAD_COLOR)
def model_fn(model_dir: str) -> DefaultPredictor:
r"""Load trained model
Parameters
----------
model_dir : str
S3 location of the model directory
Returns
-------
DefaultPredictor
PyTorch model created by using Detectron2 API
"""
path_cfg, path_model = None, None
for p_file in Path(model_dir).iterdir():
if p_file.suffix == ".json":
path_cfg = p_file
if p_file.suffix == ".pth":
path_model = p_file
LOGGER.info(f"Using configuration specified in {path_cfg}")
LOGGER.info(f"Using model saved at {path_model}")
if path_model is None:
err_msg = "Missing model PTH file"
LOGGER.error(err_msg)
raise RuntimeError(err_msg)
if path_cfg is None:
err_msg = "Missing configuration JSON file"
LOGGER.error(err_msg)
raise RuntimeError(err_msg)
with open(str(path_cfg)) as fid:
cfg = CfgNode(json.load(fid))
cfg.MODEL.WEIGHTS = str(path_model)
cfg.MODEL.DEVICE = "cuda" if torch.cuda.is_available() else "cpu"
return DefaultPredictor(cfg)
def input_fn(request_body: BinaryIO, request_content_type: str) -> np.ndarray:
r"""Parse input data
Parameters
----------
request_body : BinaryIO
encoded input image
request_content_type : str
type of content
Returns
-------
np.ndarray
input image
Raises
------
ValueError
ValueError if the content type is not `application/x-image`
"""
if request_content_type == "application/x-image":
np_image = _load_from_bytearray(request_body)
else:
err_msg = f"Type [{request_content_type}] not support this type yet"
LOGGER.error(err_msg)
raise ValueError(err_msg)
return np_image
def predict_fn(input_object: np.ndarray, predictor: DefaultPredictor) -> Mapping:
r"""Run Detectron2 prediction
Parameters
----------
input_object : np.ndarray
input image
predictor : DefaultPredictor
Detectron2 default predictor (see Detectron2 documentation for details)
Returns
-------
Mapping
a dictionary that contains: the image shape (`image_height`, `image_width`), the predicted
bounding boxes in format x1y1x2y2 (`pred_boxes`), the confidence scores (`scores`) and the
labels associated with the bounding boxes (`pred_boxes`)
"""
LOGGER.info(f"Prediction on image of shape {input_object.shape}")
outputs = predictor(input_object)
fmt_out = {
"image_height": input_object.shape[0],
"image_width": input_object.shape[1],
"pred_boxes": outputs["instances"].pred_boxes.tensor.tolist(),
"scores": outputs["instances"].scores.tolist(),
"pred_classes": outputs["instances"].pred_classes.tolist(),
}
LOGGER.info(f"Number of detected boxes: {len(fmt_out['pred_boxes'])}")
return fmt_out
# pylint: disable=unused-argument
def output_fn(predictions, response_content_type):
r"""Serialize the prediction result into the desired response content type"""
return json.dumps(predictions)
Can anyone point out what is the correct format for invoking the model (or how to tweak the code to use the endpoint)? I am thinking to change the request_content_type to 'application/json', but I am not sure that it will help much.
Edit: I tried a solution inspired by this SO thread but it did not work for my case.
It's been a while since you asked this so I hope you found a solution already, but for people seeing this in the future ...
The error appears to be because you are sending the request with the default content_type (no specified a content type in the request, neither specified a serialiser), but your code is made in a way that will only respond to requests that come with content type "application/x-image"
The default content-type is "application/json"
You have 2 options here, you either amend your code to be able to handle "application/json" content type, or when you invoke the endpoint, you add a content-type header with the right value. You could do this by changing the predict method as below:
instead of:
predictor.predict(input)
try:
predictor.predict(input, initial_args={"ContentType":"application/x-image"})
I trained an instance of scikit-learn's TfidfVectorizer and I want to persist it to disk. I saved the IDF matrix (the idf_ attribute) to disk as a numpy array and I saved the vocabulary (vocabulary_) to disk as a JSON object (I'm avoiding pickle, for security and other reasons). I'm trying to do this:
import json
from idf import idf # numpy array with the pre-computed IDFs
from sklearn.feature_extraction.text import TfidfVectorizer
# dirty trick so I can plug my pre-computed IDFs
# necessary because "vectorizer.idf_ = idf" doesn't work,
# it returns "AttributeError: can't set attribute."
class MyVectorizer(TfidfVectorizer):
TfidfVectorizer.idf_ = idf
# instantiate vectorizer
vectorizer = MyVectorizer(lowercase = False,
min_df = 2,
norm = 'l2',
smooth_idf = True)
# plug vocabulary
vocabulary = json.load(open('vocabulary.json', mode = 'rb'))
vectorizer.vocabulary_ = vocabulary
# test it
vectorizer.transform(['foo bar'])
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 1314, in transform
return self._tfidf.transform(X, copy=False)
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/feature_extraction/text.py", line 1014, in transform
check_is_fitted(self, '_idf_diag', 'idf vector is not fitted')
File "/Library/Frameworks/Python.framework/Versions/2.7/lib/python2.7/site-packages/sklearn/utils/validation.py", line 627, in check_is_fitted
raise NotFittedError(msg % {'name': type(estimator).__name__})
sklearn.utils.validation.NotFittedError: idf vector is not fitted
So, what am I doing wrong? I'm failing to fool the vectorizer object: somehow it knows that I'm cheating (i.e., passing it pre-computed data and not training it with actual text). I inspected the attributes of the vectorizer object but I can't find anything like 'istrained', 'isfitted', etc. So, how do I fool the vectorizer?
Ok, I think I got it: the vectorizer instance has an attribute _tfidf, which in turn must have an attribute _idf_diag. The transform method calls a check_is_fitted function that checks whether whether that _idf_diag exists. (I had missed it because it's an attribute of an attribute.) So, I inspected the TfidfVectorizer source code to see how _idf_diag is created. Then I just added it to the _tfidf attribute:
import scipy.sparse as sp
# ... code ...
vectorizer._tfidf._idf_diag = sp.spdiags(idf,
diags = 0,
m = len(idf),
n = len(idf))
And now the vectorization works.
Im trying to use msi library to read some specifc informations in the database of msi file by using the function openDatabase
My code is:
db = msilib.OpenDatabase(msiFile, MSIDBOPEN_READONLY)
view = db.OpenView("SELECT Key, Name, Value FROM Registry")
while True:
view.Execute(None)
record = view.Fetch()
But I got an error:
NameError: global name 'MSIDBOPEN_READONLY' is not defined
I am new to django and I am trying to use get_or_create model function but I get an error even I have the attribute in my model
AttributeError at /professor/adicionar-compromisso
'tuple' object has no attribute 'dias'
Request Method: POST
Request URL: http://localhost:8000/professor/adicionar-compromisso
Django Version: 1.4.1
Exception Type: AttributeError
Exception Value:
'tuple' object has no attribute 'dias'
Exception Location: c:\htdocs\rpv\GerenDisponibilidade\professor\models.py in inserirCompromisso, line 63
Python Executable: C:\Python27\python.exe
Python Version: 2.7.3
Python Path:
['c:\\htdocs\\rpv\\GerenDisponibilidade',
'C:\\Python27\\lib\\site-packages\\distribute-0.6.27-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\pip-1.1-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\sphinx-1.1.3-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\docutils-0.9.1-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\jinja2-2.6-py2.7.egg',
'C:\\Python27\\lib\\site-packages\\pygments-1.5-py2.7.egg',
'C:\\Windows\\system32\\python27.zip',
'C:\\Python27\\DLLs',
'C:\\Python27\\lib',
'C:\\Python27\\lib\\plat-win',
'C:\\Python27\\lib\\lib-tk',
'C:\\Python27',
'C:\\Python27\\lib\\site-packages',
'C:\\Python27\\lib\\site-packages\\setuptools-0.6c11-py2.7.egg-info']
Server time: Seg, 3 Set 2012 17:57:17 -0300
Model
class DiaSemana(models.Model):
DIAS_CHOICES = (
("Seg", "Segunda-Feira"),
("Ter", "Terça-Feira"),
("Qua", "Quarta-Feira"),
("Qui", "Quinta-Feira"),
("Sex", "Sexta-Feira"),
("Sab", "Sábado"),
("Dom", "Domingo"),
)
dias = models.CharField(max_length=20, choices=DIAS_CHOICES)
Here I am trying to search to check if there is existing value, otherwise create new and save
for diaSemana in diaSemanas:
d = DiaSemana.objects.get_or_create(dias=diaSemana)
d.dias = diaSemana;
d.save()
c.save()
c.diaSemana.add(d);
What's wrong?
get_or_create does not just return the object:
Returns a tuple of (object, created), where object is the retrieved or created object and created is a boolean specifying whether a new object was created.
In your case d has been assigned this tuple instead of the object you expected, so you get the attribute error. You can fix your code by changing it to:
d, created = DiaSemana.objects.get_or_create(dias=diaSemana)
The following two lines look unnecessary to me. The get_or_create call above ensures that d.dias=diaSemana, so there's no need to assign it again. There's probably no need to call save either.
d.dias = diaSemana;
d.save()
instead of this:
dias = models.CharField(max_length=20, choices=DIAS_CHOICES)
do:
dias = models.CharField(max_length=20, choices=DIAS_CHOICES)[0]
as #Alasdair said, the first one in the tuple is the object
Documentation clearly says that get_or_create returns tuple (object, created) - and this is exactly error you are seeing.
https://docs.djangoproject.com/en/dev/ref/models/querysets/#get-or-create