Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Python: Write-Length Control

I am trying to create a specific output pattern for a textfile I wanted to later load into C++.
I wrote a file in Python that creates random coordinates and movement values in a circle.
The output is supposed to have the output:


   Place_1 Place_2 Place_3 Movement_1 Movement_2 Movement_3\n

   Place_1 Place_2 Place_3 Movement_1 Movement_2 Movement_3\n

   Place_1 Place_2 Place_3 Movement_1 Movement_2 Movement_3

The Code is use is

import numpy as np
file = open('log.txt', 'a')


def f(n, center, radius, ecc):
  pos = np.zeros((n,6))
  r = ecc * radius
  for i in range(n):
    while 1:
        x_1 = -1 + 2 * np.random.rand(1)
        x_2 = -1 + 2 * np.random.rand(1)
        if (x_1*x_1 + x_2*x_2)<1 :
            pos[i,0] = center[0] + r * 2 * x_1 * np.sqrt(1 - x_1*x_1 - x_2*x_2)
            pos[i,1] = center[1] + r * 2 * x_2 * np.sqrt(1 - x_1*x_1 - x_2*x_2)
            pos[i,2] = center[2] + r * (1 - 2 * (x_1*x_1 + x_2*x_2))
            pos[i,3] = (-1 + 2 * np.random.rand(1))
            pos[i,4] = (-1 + 2 * np.random.rand(1))
            pos[i,5] = (-1 + 2 * np.random.rand(1))
            break
    string = str(pos[i,:]).strip('[]').rstrip('\n')
    file.write(string)
  return

f(10000, np.array((127,127,127)), 92, 0.9)

file.close()

The log I create is however very badly formated. How can I get the required format?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

You’re going to a lot of trouble here that you don’t need. This seems to solve the problem, simply.

import numpy as np
file = open('log.txt', 'a')

def f(n, center, radius, ecc):
    r = ecc * radius
    for i in range(n):
        while 1:
            pos = [0]*6
            x_1 = -1 + 2 * np.random.rand(1)[0]
            x_2 = -1 + 2 * np.random.rand(1)[0]
            if (x_1*x_1 + x_2*x_2) < 1:
                pos[0] = center[0] + r * 2 * x_1 * np.sqrt(1 - x_1*x_1 - x_2*x_2)
                pos[1] = center[1] + r * 2 * x_2 * np.sqrt(1 - x_1*x_1 - x_2*x_2)
                pos[2] = center[2] + r * (1 - 2 * (x_1*x_1 + x_2*x_2))
                pos[3] = (-1 + 2 * np.random.rand(1)[0])
                pos[4] = (-1 + 2 * np.random.rand(1)[0])
                pos[5] = (-1 + 2 * np.random.rand(1)[0])
                break
        print(*pos, file=file)

f(10000, np.array((127,127,127)), 92, 0.9)

file.close()

A solution without numpy:

import math
import random
file = open('log.txt', 'a')

def f(n, center, radius, ecc):
    r = ecc * radius
    for _ in range(n):
        pos = [0]*6
        while 1:
            x_1 = 2 * random.random() - 1
            x_2 = 2 * random.random() - 1
            vector = x_1*x_1 + x_2*x_2
            if vector < 1:
                break
        pos[0] = center[0] + r * 2 * x_1 * math.sqrt(1 - vector)
        pos[1] = center[1] + r * 2 * x_2 * math.sqrt(1 - vector)
        pos[2] = center[2] + r * (1 - 2 * vector)
        pos[3] = 2 * random.random() -1
        pos[4] = 2 * random.random() -1
        pos[5] = 2 * random.random() -1
        print(*pos, file=file)

f(10000, (127,127,127), 92, 0.9)

file.close()
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading