Python Forum
Numpy Structure and Efficiency
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Numpy Structure and Efficiency
#1
I have the following code:

import matplotlib.pyplot as plt
import matplotlib.cbook as cbook

import numpy as np
import math

SIGMA = 5.67051e-5														# Stefan-Boltzmann Constant
PI    = math.pi															# Pi
M_SUN = 1.989e33														# Solar mass (g)
L_SUN = 3.826e33														# Solar luminosity (ergs/s)
R_SUN = 6.9599e10														# Solar radius (cm)
YEAR  = 3.15570e7														# AU year (s)


sa_sun = 4 * PI * math.pow(R_SUN, 2)									# Solar surface area
t_sun  = L_SUN / (sa_sun * SIGMA)										# Solar surface temperature



# Converts Fortran output (xd+/-y) into float
def convert(oldValue):
	if oldValue.find('D') != -1:
		newValue = float(oldValue.replace('D', 'e'))
	else:
		if oldValue.find('+') != -1:
			mantissa = float(oldValue[0: oldValue.find('+')])
			exponent = int(oldValue[oldValue.find('+'):])
			
		else:
			mantissa = float(oldValue[0: oldValue.find('-')])
			exponent = int(oldValue[oldValue.find('-'):])

		newValue = mantissa * (math.pow(10, exponent))
	
	return newValue



# Reads in data from STELCOR file
def readData(datafile):
	
	data = np.array([])
	
	file = open(datafile)

	for line in file:
		if (line[1:6] == "hydro") or (line[1:6] == "HYDRO"):
			time      = convert(line[21:35])
			delta_t	  = convert(line[36:50])
			mass      = convert(line[51:65])
			radius    = convert(line[66:80])
			lum_core  = convert(line[81:95])
			lum_tot   = convert(line[96:110])
			flux      = convert(line[111:125])
			ratio     = float(line[125:137])
			data = np.append(data, [time, delta_t, mass, radius, lum_core, lum_tot, flux, ratio], axis = 0)
			
	file.close()
	
	return data



if __name__ == "__main__":
	stellarData = np.array([])

	stellarData = readData("dummymain.lst")
	#CM_Graph(?,?)
I am adding the data from a textfile that contains thousands of lines, similar to this:

Output:
hydro output: 1 1.05200D+09 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 4.99722-235 0.0499938 hydro output: 2 2.10400D+09 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 2.88583-105 0.0499938 hydro output: 3 3.15600D+09 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 3.81557D-62 0.0499938 hydro output: 4 4.20800D+09 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 1.19575D-40 0.0499938 hydro output: 5 5.26000D+09 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 8.64733D-28 0.0499938 hydro output: 6 6.31200D+09 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 3.04933D-19 0.0499938 hydro output: 7 7.36400D+09 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 3.72610D-13 0.0499938 hydro output: 8 8.41600D+09 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 1.32716D-08 0.0499938 hydro output: 9 9.46800D+09 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 4.49291D-05 0.0499938 hydro output: 10 1.05200D+10 1.05200D+09 9.94376D+31 3.66754D+10 7.52265D+31 7.52265D+31 2.93595D-02 0.0499938
The system strips out the initial text, ignores the incremental step (integer) value and populates an array with the converted float data.

All the data is added to a Numpy array of 1 dimension.

I originally envisaged a structure such as:

Output:
[[1052000000.0, 1052000000.0, 9.94376e+31, 36675400000.0, 7.52265e+31, 7.52265e+31, 4.9972200000000005e-235, 0.0499938], [...], ...]
However, I get these all as a single element in each array position@

Output:
[1052000000.0, 1052000000.0, 9.94376e+31, 36675400000.0, 7.52265e+31, 7.52265e+31, 4.9972200000000005e-235, 0.0499938]
I later want to reference all rows of data, but only the data in a specific position in that row (all data in column 3, for example).

How might I alter this and is there a more efficient way of collecting/storing the data?
Reply


Messages In This Thread
Numpy Structure and Efficiency - by garynewport - Oct-19-2022, 11:34 AM
RE: Numpy Structure and Efficiency - by garynewport - Oct-19-2022, 12:00 PM
RE: Numpy Structure and Efficiency - by paul18fr - Oct-19-2022, 10:11 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Efficiency with regard to nested conditionals or and statements Mark17 13 3,375 May-06-2022, 05:16 PM
Last Post: Mark17
  How to use vectorization instead of for loop to improve efficiency in python? PJLEMZ 4 2,518 Feb-06-2021, 09:45 AM
Last Post: paul18fr
  Any suggestions to improve BuySell stock problem efficiency? mrapple2020 0 1,413 May-13-2020, 06:19 PM
Last Post: mrapple2020
  Help improve code efficiency benbrown03 9 4,472 Feb-20-2019, 03:45 PM
Last Post: ichabod801
  Web Scraping efficiency improvement HiImNew 0 2,439 Jun-01-2018, 08:52 PM
Last Post: HiImNew
  Improving Efficiency of SVM by various available kernels Sachtech 0 2,152 Apr-09-2018, 07:29 AM
Last Post: Sachtech
  Still learning - code efficiency, which of these is better? shelzmike 2 3,360 Oct-14-2017, 04:47 AM
Last Post: shelzmike

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020