Ali_bean
Ali_bean

Reputation: 341

Generating a 4-D array from 1-D data reshape

I have a 1-D set of permeability data for an oil reservoir,

It is a 1-D array in the format 6 columns, X rows, it needs to be restructured in the shape of:

60 x 220 x 85 cells (1.122x106 cells)

If i import the data, and use the re-shape function with z = 1, it fills in the correct format (reading left to right and filling y first with x=1,z=1 then filling y with x=2 etc.

If i use z=2 it fills [x1,y,z2] after it reaches the top of [x1,y,z1], and then goes back and fills [x2,y,z1] effectively splitting it between each slice,

import numpy as np
data = np.loadtxt("spe_phi_sample.prn")
print(data.shape)
data = np.reshape(data, (60,85,2))
print(data.shape)
x,y,z = data.nonzero()
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.scatter(x, y, -z, c=data[x,y,z] ,  zdir='z')

z=1 Data with tracer (correct format) z=1 data with tracer (correct format)

z=2 data with tracer (see how it populated wrong slice) z=2 data with tracer (see how it populated wrong slice)

Is there a way to determine how re-shape populates the data? Is there a more suitable function for creating a 4-d array from a large 1-d array by determining when to slice for each axis, or does this need to be done manually? I'm not too experienced in python, thanks

Upvotes: 1

Views: 142

Answers (2)

Flabetvibes
Flabetvibes

Reputation: 3194

When you use numpy.reshape you must have in mind that it will read the elements using the index order and will place the elements into the reshaped array using this index order. The default index order is given by: the last axis index changing fastest, back to the first axis index changing slowest (where ..., the second axis is y and the first axis is x).

To better understand what happens, here is a piece of code:

import numpy as np

# Create array.
size1 = 10
size2 = 60
shape = (size1, size2)
a = np.mod(np.arange(1, size1 * size2 + 1), size2).reshape(shape)
# Reshape array with z = 1.
b = np.reshape(a, (size1, size2, 1))
# Reshape array with z = 2.
c = np.reshape(a, (size1, size2 / 2, 2))
# Reshape array correctly.
d = np.reshape(a, (size1, 2, size2 / 2))
d = np.swapaxes(d, 1, 2)

Here is b (with blue for low value (1) and red for high value (10)): enter image description here

Here is c: enter image description here

And here is d: enter image description here

As you can see, c has been populated along the z axis first whereas d has been populated along the y axis first, thanks to the numpy.swapaxes function.

I hope this will help you.

Upvotes: 1

hpaulj
hpaulj

Reputation: 231738

'It is a 1-D array in the format 6 columns, X rows,' from loadtxt makes me suspect that it is a structured array.

Show us both data.shape and data.dtype.

While you are at show us the same values for data after reshape.

It might also help to see a few 'rows' of data, e.g. data[:3,...].

Upvotes: 0

Related Questions