Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to serialize numpy arrays?

I'm new to django-rest-framework so I'm sorry if my question is too simple or doesn't make much sense.

I've programmed a scientific python package and would like to make it available via an API. For this I'm using the django rest framework.

From a high-level perspective to make one particular function inside my package available I have to adjust two files. In a serializer file I define certain validation which translate the incoming request to python native types and defines the validation of the input parameters. In the view file I'm calling the validation and do some conversions before calling the package internal function.

Suppose my function I want to expose via API looks like this:

In [6]: def f(a):
   ...:    return np.mean(a)
   ...: 

Now the API will receive the input via a json file. My first question is how do I define the serializer for the function above? Is a ListField the right choice?

class fSerializer(serializers.Serializer):
    nparray = serializers.ListField(
        source="a",
        child=serializers.DecimalField(max_digits=12, decimal_places=2)
    )

Or are there any other, better suited fields?

The view file would then look something like this:

 class fViewSet(viewsets.ViewSet):

    def create(self, request):
        serializer = fSerializer(data=request.data)
        if serializer.is_valid():
            try:
                a = np.asarray(serializer.validated_data.get("a"))
                json_return = json.dumps(f(a))
                return HttpResponse(json_return,
                                    content_type='application/json')

As you can see I'm converting first the validated data to numpy arary. But is this the right (pythonic) way to do it? Should it be done in the serializer?

If someone has a tutorial for this kind of function being exposed via an API, more than happy to read that.

like image 982
math Avatar asked Apr 10 '18 14:04

math


Video Answer


1 Answers

Conversion between Numpy arrays and Python lists are rather fast, so we have just converted arrays to lists before sending the data and to array whenever some Numpy calculation is needed.

data = [[1,2], [3,1]]
data_np = np.array(data)
data_list = data_np.tolist()
data == data_list
like image 192
Antti A Avatar answered Sep 22 '22 01:09

Antti A