Re: Strange error in django

2015-04-22 Thread John DeRosa
If you get an exception, the “except” clause will drop down into the “return” 
statement, and classification_serializer will be referenced before it’s 
assigned to. (Because it never was assigned to.)

John

> On Apr 22, 2015, at 2:36 PM, Cristian Javier Martinez 
>  wrote:
> 
> Hi! I'm using the django rest framework and successfully deployed an 
> application in a production environment but I'm having an strange error 
> related to threads when I call a method that performs a simple classification 
> task (using a scikit learn classifier) and I have no idea what is causing the 
> error. This is the method in views.py
> 
> @api_view(['GET', 'POST'])
> def classify_item(request):
> """
> Classify item, or list classifications.
> """
> if request.method == 'GET':
> items = Item.objects.all()
> serializer = ItemSerializer(items, many=True)
> return Response(serializer.data)
> 
> elif request.method == 'POST':
> serializer = ItemSerializer(data=request.data['item'])
> if serializer.is_valid():
> try:
> item = serializer.data
> filter_name = request.data['filter']
> 
> 
> # fix encoding
> item_dict =  {}
> for key in item:
> value = item[key]
> if isinstance(value, unicode):
> value = value.encode('utf-8')
> item_dict[key] = value
> 
> classifier_name = 
> CLASSIFIER_NAME_BY_FILTER[filter_name]
> 
> logger.debug("retrieving the persisted 
> classifier and classifing the item")
> clf = 
> joblib.load(os.path.join(CLASSIFIERS_PATH, filter_name, classifier_name))
> 
> logger.debug("predicting probabilities")
> data = pd.DataFrame([item_dict])
> 
> logger.debug("scoring item")
> score = clf.predict(data)[0][1]
> 
> logger.debug("score: {}".format(score))
> 
> 
> # create and save classification
> classification = 
> Classification(classifier_name=classifier_name,score=score,item_id=item['_id'])
> classification_serializer = 
> ClassificationSerializer(classification)
> #classification_serializer.save()
> except Exception as e:
> logger.error("Ocurrio un error al intentar 
> parsear el item: {}".format(e))
> return Response(classification_serializer.data, 
> status=status.HTTP_201_CREATED)
> 
> return Response(serializer.errors, 
> status=status.HTTP_400_BAD_REQUEST)
> 
> And the output is:
> 
> [22/Apr/2015 21:14:56] DEBUG [classifiers.views:63] retrieve the classifier 
> for the given sited_id and classify item
> [22/Apr/2015 21:15:14] DEBUG [classifiers.views:66] predicting probabilities
> [22/Apr/2015 21:15:14] DEBUG [classifiers.views:69] scoring item
> [22/Apr/2015 21:15:14] ERROR [classifiers.views:80] Ocurrio un error al 
> intentar parsear el item: 'Thread' object has no attribute '_children'
> [22/Apr/2015 21:15:14] ERROR [django.request:256] Internal Server Error: 
> /items/
> Traceback (most recent call last):
>   File 
> "/home/keepcon/meli_filters_env/local/lib/python2.7/site-packages/django/core/handlers/base.py",
>  line 132, in get_response
> response = wrapped_callback(request, *callback_args, **callback_kwargs)
>   File 
> "/home/keepcon/meli_filters_env/local/lib/python2.7/site-packages/newrelic-2.50.0.39/newrelic/hooks/framework_django.py",
>  line 499, in wrapper
> return wrapped(*args, **kwargs)
>   File 
> "/home/keepcon/meli_filters_env/local/lib/python2.7/site-packages/django/views/decorators/csrf.py",
>  line 58, in wrapped_view
> return view_func(*args, **kwargs)
>   File 
> "/home/keepcon/meli_filters_env/local/lib/python2.7/site-packages/django/views/generic/base.py",
>  line 71, in view
> return self.dispatch(request, *args, **kwargs)
>   File 
> "/home/keepcon/meli_filters_env/local/lib/python2.7/site-packages/newrelic-2.50.0.39/newrelic/hooks/component_djangorestframework.py",
>  line 27, in _nr_wrapper_APIView_dispatch_
> return wrapped(*args, **kwargs)
>   File 
> "/home/keepcon/meli_filters_env/local/lib/python2.7/site-packages/rest_framework/views.py",
>  line 452, in dispatch
> response = self.handle_exception(exc)
>   File 
> "/home/keepcon/meli_filters_env/local/lib/python2.7/site-packages/rest_framework/views.py",
>  

Re: Strange error in django

2015-04-22 Thread Cristian Javier Martinez
Thanks for your reply John DeRosa but the question is about what is causing 
the exception because I'm not using threads at all and the error says 
"*'Thread' 
object has no attribute '_children'"*. I'm catching the error and printing 
it out before the return as you can see in the log.

El miércoles, 22 de abril de 2015, 19:13:55 (UTC-3), John DeRosa escribió:
>
> If you get an exception, the “except” clause will drop down into the 
> “return” statement, and classification_serializer will be referenced before 
> it’s assigned to. (Because it never was assigned to.)
>
> John
>
> On Apr 22, 2015, at 2:36 PM, Cristian Javier Martinez <
> martinezcri...@gmail.com > wrote:
>
> Hi! I'm using the django rest framework and successfully deployed an 
> application in a production environment but I'm having an strange error 
> related to threads when I call a method that performs a simple 
> classification task (using a scikit learn classifier) and I have no idea 
> what is causing the error. This is the method in views.py
>
> @api_view(['GET', 'POST'])
> def classify_item(request):
> """
> Classify item, or list classifications.
> """
> if request.method == 'GET':
> items = Item.objects.all()
> serializer = ItemSerializer(items, many=True)
> return Response(serializer.data)
>
> elif request.method == 'POST':
> serializer = ItemSerializer(data=request.data['item'])
> if serializer.is_valid():
> try:
> item = serializer.data
> filter_name = request.data['filter']
>
>
> # fix encoding
> item_dict =  {}
> for key in item:
> value = item[key]
> if isinstance(value, unicode):
> value = 
> value.encode('utf-8')
> item_dict[key] = value
>
> classifier_name = 
> CLASSIFIER_NAME_BY_FILTER[filter_name]
>
> logger.debug("retrieving the persisted 
> classifier and classifing the item")
> clf = 
> joblib.load(os.path.join(CLASSIFIERS_PATH, filter_name, classifier_name))
>
> logger.debug("predicting probabilities")
> data = pd.DataFrame([item_dict])
>
> logger.debug("scoring item")
> score = clf.predict(data)[0][1]
>
> logger.debug("score: {}".format(score))
>
>
> # create and save classification
> classification = 
> Classification(classifier_name=classifier_name,score=score,item_id=item['_id'])
> classification_serializer = 
> ClassificationSerializer(classification)
> #classification_serializer.save()
> except Exception as e:
> logger.error("Ocurrio un error al intentar 
> parsear el item: {}".format(e))
> return Response(classification_serializer.data, 
> status=status.HTTP_201_CREATED)
>
> return Response(serializer.errors, 
> status=status.HTTP_400_BAD_REQUEST)
>
> And the output is:
>
> [22/Apr/2015 21:14:56] DEBUG [classifiers.views:63] retrieve the 
> classifier for the given sited_id and classify item
> [22/Apr/2015 21:15:14] DEBUG [classifiers.views:66] predicting 
> probabilities
> [22/Apr/2015 21:15:14] DEBUG [classifiers.views:69] scoring item
> [22/Apr/2015 21:15:14] ERROR [classifiers.views:80] Ocurrio un error al 
> intentar parsear el item: *'Thread' object has no attribute '_children'*
> [22/Apr/2015 21:15:14] ERROR [django.request:256] Internal Server Error: 
> /items/
> Traceback (most recent call last):
>   File 
> "/home/keepcon/meli_filters_env/local/lib/python2.7/site-packages/django/core/handlers/base.py",
>  
> line 132, in get_response
> response = wrapped_callback(request, *callback_args, **callback_kwargs)
>   File 
> "/home/keepcon/meli_filters_env/local/lib/python2.7/site-packages/newrelic-2.50.0.39/newrelic/hooks/framework_django.py",
>  
> line 499, in wrapper
> return wrapped(*args, **kwargs)
>   File 
> "/home/keepcon/meli_filters_env/local/lib/python2.7/site-packages/django/views/decorators/csrf.py",
>  
> line 58, in wrapped_view
> return view_func(*args, **kwargs)
>   File 
> "/home/keepcon/meli_filters_env/local/lib/python2.7/site-packages/django/views/generic/base.py",
>  
> line 71, in view
> return self.dispatch(request, *args, **kwargs)
>   File 
> "/home/keepcon/meli_filters_env/local/lib/python2.7/site-packages/newrelic-2.50.0.39/newre

Re: Strange error in django

2015-04-22 Thread John DeRosa
Oh, sorry.

Well, you’re using joblib. The joblib.load() call loads something and return a 
Python object into the symbol clf.

It would seem you need to look at the object that joblib.load is 
reconstituting. This code snippet doesn’t give enough information to diagnose 
this, but if I were you, I’d probably try this:

1. What file does "joblib.load(os.path.join(CLASSIFIERS_PATH, filter_name, 
classifier_name))” read, and what object does it return?

2. Can you interactively run this code in the Python interpreter? Does it run 
OK, or does it throw an exception?

3. If it works on your development machine, try running it interactively on 
your production machine. Ssh into your production server and run the code.

4. What does dir(clf) return? Does the clf object have the methods you expect 
it to have?


If you can get it to throw an exception when you run it from the interactive 
prompt, you can single-step it.

John


> On Apr 22, 2015, at 4:10 PM, Cristian Javier Martinez 
>  wrote:
> 
> Thanks for your reply John DeRosa but the question is about what is causing 
> the exception because I'm not using threads at all and the error says 
> "'Thread' object has no attribute '_children'". I'm catching the error and 
> printing it out before the return as you can see in the log.
> 
> El miércoles, 22 de abril de 2015, 19:13:55 (UTC-3), John DeRosa escribió:
> If you get an exception, the “except” clause will drop down into the “return” 
> statement, and classification_serializer will be referenced before it’s 
> assigned to. (Because it never was assigned to.)
> 
> John
> 
>> On Apr 22, 2015, at 2:36 PM, Cristian Javier Martinez 
>> > wrote:
>> 
>> Hi! I'm using the django rest framework and successfully deployed an 
>> application in a production environment but I'm having an strange error 
>> related to threads when I call a method that performs a simple 
>> classification task (using a scikit learn classifier) and I have no idea 
>> what is causing the error. This is the method in views.py
>> 
>> @api_view(['GET', 'POST'])
>> def classify_item(request):
>> """
>> Classify item, or list classifications.
>> """
>> if request.method == 'GET':
>> items = Item.objects.all()
>> serializer = ItemSerializer(items, many=True)
>> return Response(serializer.data)
>> 
>> elif request.method == 'POST':
>> serializer = ItemSerializer(data=request.data['item'])
>> if serializer.is _valid():
>> try:
>> item = serializer.data
>> filter_name = request.data['filter']
>> 
>> 
>> # fix encoding
>> item_dict =  {}
>> for key in item:
>> value = item[key]
>> if isinstance(value, unicode):
>> value = value.encode('utf-8')
>> item_dict[key] = value
>> 
>> classifier_name = 
>> CLASSIFIER_NAME_BY_FILTER[filter_name]
>> 
>> logger.debug("retrieving the persisted 
>> classifier and classifing the item")
>> clf = 
>> joblib.load(os.path.join(CLASSIFIERS_PATH, filter_name, classifier_name))
>> 
>> logger.debug("predicting probabilities")
>> data = pd.DataFrame([item_dict])
>> 
>> logger.debug("scoring item")
>> score = clf.predict(data)[0][1]
>> 
>> logger.debug("score: {}".format(score))
>> 
>> 
>> # create and save classification
>> classification = 
>> Classification(classifier_name=classifier_name,score=score,item_id=item['_id'])
>> classification_serializer = 
>> ClassificationSerializer(classification)
>> #classification_serializer.save()
>> except Exception as e:
>> logger.error("Ocurrio un error al intentar 
>> parsear el item: {}".format(e))
>> return Response(classification_serializer.data, 
>> status=status.HTTP_201_CREATED)
>> 
>> return Response(serializer.errors, 
>> status=status.HTTP_400_BAD_REQUEST)
>> 
>> And the output is:
>> 
>> [22/Apr/2015 21:14:56] DEBUG [classifiers.views:63] retrieve the classifier 
>> for the given sited_id and classify item
>> [22/Apr/2015 21:15:14] DEBUG [classifiers.views:66] predicting probabilities
>> [22/Apr/2015 21:15:14] DEBUG [classifiers.views:69] scoring item
>> [22/Apr/2015 21:15:14] ERROR [classifiers.views:80] Ocurrio un e

Re: Strange error in django

2015-04-22 Thread Vijay Khemlani
What specific version of Python are you using (2.7...x?)

On Wed, Apr 22, 2015 at 8:26 PM, John DeRosa 
wrote:

> Oh, sorry.
>
> Well, you’re using joblib. The joblib.load() call loads something and
> return a Python object into the symbol clf.
>
> It would seem you need to look at the object that joblib.load is
> reconstituting. This code snippet doesn’t give enough information to
> diagnose this, but if I were you, I’d probably try this:
>
> 1. What file does "joblib.load(os.path.join(CLASSIFIERS_PATH, filter_name,
> classifier_name))” read, and what object does it return?
>
> 2. Can you interactively run this code in the Python interpreter? Does it
> run OK, or does it throw an exception?
>
> 3. If it works on your development machine, try running it interactively
> on your production machine. Ssh into your production server and run the
> code.
>
> 4. What does dir(clf) return? Does the clf object have the methods you
> expect it to have?
>
>
> If you can get it to throw an exception when you run it from the
> interactive prompt, you can single-step it.
>
> John
>
>
> On Apr 22, 2015, at 4:10 PM, Cristian Javier Martinez <
> martinezcristianjav...@gmail.com> wrote:
>
> Thanks for your reply John DeRosa but the question is about what is
> causing the exception because I'm not using threads at all and the error
> says "*'Thread' object has no attribute '_children'"*. I'm catching the
> error and printing it out before the return as you can see in the log.
>
> El miércoles, 22 de abril de 2015, 19:13:55 (UTC-3), John DeRosa escribió:
>>
>> If you get an exception, the “except” clause will drop down into the
>> “return” statement, and classification_serializer will be referenced before
>> it’s assigned to. (Because it never was assigned to.)
>>
>> John
>>
>> On Apr 22, 2015, at 2:36 PM, Cristian Javier Martinez <
>> martinezcri...@gmail.com> wrote:
>>
>> Hi! I'm using the django rest framework and successfully deployed an
>> application in a production environment but I'm having an strange error
>> related to threads when I call a method that performs a simple
>> classification task (using a scikit learn classifier) and I have no idea
>> what is causing the error. This is the method in views.py
>>
>> @api_view(['GET', 'POST'])
>> def classify_item(request):
>> """
>> Classify item, or list classifications.
>> """
>> if request.method == 'GET':
>> items = Item.objects.all()
>> serializer = ItemSerializer(items, many=True)
>> return Response(serializer.data)
>>
>> elif request.method == 'POST':
>> serializer = ItemSerializer(data=request.data['item'])
>> if serializer.is_valid():
>> try:
>> item = serializer.data
>> filter_name = request.data['filter']
>>
>>
>> # fix encoding
>> item_dict =  {}
>> for key in item:
>> value = item[key]
>> if isinstance(value, unicode):
>> value =
>> value.encode('utf-8')
>> item_dict[key] = value
>>
>> classifier_name =
>> CLASSIFIER_NAME_BY_FILTER[filter_name]
>>
>> logger.debug("retrieving the persisted
>> classifier and classifing the item")
>> clf =
>> joblib.load(os.path.join(CLASSIFIERS_PATH, filter_name, classifier_name))
>>
>> logger.debug("predicting probabilities")
>> data = pd.DataFrame([item_dict])
>>
>> logger.debug("scoring item")
>> score = clf.predict(data)[0][1]
>>
>> logger.debug("score: {}".format(score))
>>
>>
>> # create and save classification
>> classification =
>> Classification(classifier_name=classifier_name,score=score,item_id=item['_id'])
>> classification_serializer =
>> ClassificationSerializer(classification)
>> #classification_serializer.save()
>> except Exception as e:
>> logger.error("Ocurrio un error al
>> intentar parsear el item: {}".format(e))
>> return Response(classification_serializer.data,
>> status=status.HTTP_201_CREATED)
>>
>> return Response(serializer.errors,
>> status=status.HTTP_400_BAD_REQUEST)
>>
>> And the output is:
>>
>> [22/Apr/2015 21:14:56] DEBUG [classifiers.views:63] retrieve the
>> classifier for the given sited_id and classify item
>> [22/Apr/2015 21:15:14] DEBUG [classifiers.views:6