In this code, I generate the coordinates of a tube made of multiple rings. Finally, I write those x,y,z coordinates in a .csv file. I want to simplify this code (maybe using a for loop?).

import numpy as np
import matplotlib.pyplot as plt

radius = bLength*numPoints / (2 * np.pi)
theta = np.linspace(0,2*np.pi,numPoints,endpoint=False)

#creating the x, y coordinates of the first ring
x0,y0=(radius * np.cos(theta)), (radius * np.sin(theta))
#creating the x, y coordinates of the second ring
x1,y1=(radius * np.cos(theta+dtheta/2)) , (radius * np.sin(theta+dtheta/2))
#above first ring and second ring will come alternatively to form a tube. 
#now generating the z-coordinate of the first ring, second ring, third ring, etc.

###Now saving the x, y, z coordinates in csv files.

np.savetxt('cooRing00.csv',np.c_[x0,y0,cons0],delimiter=' ',fmt='%10f')
np.savetxt('cooRing01.csv',np.c_[x1,y1,cons1],delimiter=' ',fmt='%10f')
np.savetxt('cooRing02.csv',np.c_[x0,y0,cons2],delimiter=' ',fmt='%10f')
np.savetxt('cooRing03.csv',np.c_[x1,y1,cons3],delimiter=' ',fmt='%10f')
np.savetxt('cooRing04.csv',np.c_[x0,y0,cons4],delimiter=' ',fmt='%10f')
np.savetxt('cooRing05.csv',np.c_[x1,y1,cons5],delimiter=' ',fmt='%10f')
np.savetxt('cooRing06.csv',np.c_[x0,y0,cons6],delimiter=' ',fmt='%10f')
np.savetxt('cooRing07.csv',np.c_[x1,y1,cons7],delimiter=' ',fmt='%10f')
np.savetxt('cooRing08.csv',np.c_[x0,y0,cons8],delimiter=' ',fmt='%10f')
np.savetxt('cooRing09.csv',np.c_[x1,y1,cons9],delimiter=' ',fmt='%10f')

###Now, I have the x, y, z coordinates in many files (based on the length of the tube I want. 
  • \$\begingroup\$ Please do not update the code in your question to incorporate feedback from answers, doing so goes against the Question + Answer style of Code Review. This is not a forum where you should keep the most updated version in your question. Please see what you may and may not do after receiving answers. \$\endgroup\$ – Sᴀᴍ Onᴇᴌᴀ Dec 6 '18 at 15:48
  • \$\begingroup\$ Okay @SᴀᴍOnᴇᴌᴀ. \$\endgroup\$ – phenomenon Dec 6 '18 at 18:30

Thanks for sharing your code.

For the generation of your z-coordinate you can write something like:

def generate_z_coordinate(n, x0, x1):
    """Generate the z-coordinate for the n first ring.
       :param n: the number of ring
       :param x0: x0 coordinate
       :param x1: x1 coordinate
       :return: A list of the z-coordinate for the n rings
    tmp = 0
    rings = []
    for i in range(0,n):
        if i % 2 == 0:
            x = x0
            x = x1
        rings.append(np.ones(x.shape) * tmp)
        tmp += 2
    return rings

I used tmp because I lack a better name, but there is probably a better way to name it.

And saving in the csv:

def save(filename, rings, x0, y0, x1, y1):
    """Save rings coordinate into filename
       :param filename: Name of the file to save data in
       :param rings: list of z-coordinate for the rings
       :param x0: x0 coordinate
       :param y0: y0 coordinate
       :param x1: x1 coordinate
       :param y1: y1 coordinate
    for i, elt in enumerate(rings):
        name = filename + str(i) + '.csv'
        if i % 2 == 0:
            x = x0
            y = y0
            z = rings[i]
            x = x1
            y = y1
            z = rings[i]

        np.savetxt(name, np.c_[x, y, z], delimiter=' ', fmt='%10f') 

I am not sure it is more readable than your version but it's more scalable. Depend on what you need.

Also try to be consistant in your code and follow Python style guide(see PEP8):

  • write bLength = 1.6 instead of bLength=1.6
  • theta = np.linspace(0, 2 * np.pi, numPoints, endpoint=False)

You can use flake8 or black or PyLint to lint your code

  • \$\begingroup\$ I fixed the functions, is it working for you know? \$\endgroup\$ – Julien Rousé Nov 29 '18 at 13:40
  • \$\begingroup\$ I fixed the generation of the filename try it again. Your filename was cooRing00.csv etc my function is more like cooRing0.csv (without the trailing 0 before the number, maybe that trigger the error? I cant find if savetext need the file to already exist \$\endgroup\$ – Julien Rousé Dec 6 '18 at 13:40
  • \$\begingroup\$ cooxyz.csv need to be a string, 'cooxyz.csv' and the function add csv automatically, try save("cooxyz", rings, x0, y0, x1, y1) \$\endgroup\$ – Julien Rousé Dec 6 '18 at 15:46
  • \$\begingroup\$ I added the change name = filename + str(i) + '.csv'. Still I get following error: save(cooxyz, rings, x0, y0, x1, y1) NameError: name 'cooxyz' is not defined \$\endgroup\$ – phenomenon Dec 6 '18 at 16:10

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.