How to serialize numpy arrays?

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

I have programmed a scientific python package and would like to make it accessible through the API. For this, I use the django rest framework.

From a high level point of view, in order to make one specific function inside my package, I have to configure two files. In the serializer file, I define a specific check that converts the incoming request to the native python types and defines a check for input parameters. In the view file, I call validation and do some conversions before calling the package's internal function.

Suppose my function that I want to open through the API is as follows:

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

Now the API will receive input through a json file. My first question is: how to define a serializer for the function above? Is 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 other, more suitable fields?

The view file will 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 first convert the verified data to a numpy arary. But is this the correct (pythonic) way to do this? Should this be done in the serializer?

If someone has a tutorial on how to open this function through the API, they are more than happy to read it.

+4
source share
3 answers

Converting between Numpy arrays and Python lists is pretty fast, so we just convert arrays to lists before sending data and the array whenever you need a Numpy calculation.

data = [[1,2], [3,1]]
data_np = np.array(data)
data_list = data_np.tolist()
data == data_list
+4
+1

pickle.dumps numpy.save , NumPy, , . , , , , npy. - , python, / , .

pickle:

import pickle
a = # some NumPy array
serialized = pickle.dumps(a, protocol=0) # protocol 0 is printable ASCII
deserialized_a = pickle.loads(serialized)

numpy.save , , StringIO:

a = # any NumPy array
memfile = StringIO.StringIO()
numpy.save(memfile, a)
memfile.seek(0)
serialized = json.dumps(memfile.read().decode('latin-1'))
# latin-1 maps byte n to unicode code point n

:

memfile = StringIO.StringIO()
memfile.write(json.loads(serialized).encode('latin-1'))
memfile.seek(0)
a = numpy.load(memfile)

python3 StringIO.StringIO() io.BytesIO()

0

Source: https://habr.com/ru/post/1696024/


All Articles