I'm writing a code using python to generate a point shapefile within ArcMAP. I have a 1000 random possibilities (in FileA), and I need to try all of them. The FileA is the index of the position of FileB.
In addition, for each sequence generated I'm looking at the evolution of the area of the Voronoi polygon from 3 points to 50 points in each sequence that contains a total of 50 points.
When I run the code, I have this message which confuse me:
tempXYFile.writerow('{0},{1}'.format(coordinates[entry.toInteger()][0],coordinates[entry.toInteger()][1]))
AttributeError: 'str' object has no attribute 'toInteger'
I'll appreciate any help !
This is my code:
import csv
# create 1000 XY sequences
print 'Start of the script'
sequences = csv.reader(open('FileA.csv','rb'),delimiter=',')
coordinates = []
# read coordinates of volcanos and store in memory
print 'Start reading in the coordinates into the memory'
coordinates_file = csv.reader(open('FileB.csv','rb'),delimiter=',')
for coordinate in coordinates[1:]:
coordinates.append(coordinate)
del coordinates_file
##i = 1
for sequence in sequences:
## print 'sequence # {0}'.format(i)
j = 1
tempXYFile = csv.writer(open('tempXY.csv','wb'),delimiter=',') #add the parameter to create a file if does not exist
for entry in sequence:
tempXYFile.writerow('{0},{1}'.format(coordinates[entry.toInteger()][0],coordinates[entry.toInteger()][1]))
print 'Entry # {0}: {1},{2}'.format(j, coordinates[entry.toInteger()][0],coordinates[entry.toInteger()][1])
j = j + 1
i = i + 1
del tempXYFile
print 'End of Script'
Python doesn't have a toInteger() function for strings you will want to user int(entry[]) instead.
There is no toInteger() method on strings in standard Python. The error message is pretty clear. If you want to convert a string like "37" to the integer 37, try int(entry) instead.
Why did you write toInteger() in your code?
Does not exist toInteger method on strings
Check this: http://docs.python.org/library/stdtypes.html#string-methods
The correct form is:
coordinates[int(entry)][0]
Related
I am working on Ironpython in Revit application.
This is the code below I was trying in python. Help would be appreciated.
From the list of points, there is a first point and second point. I have created functions for them.
The script should check if the y coordinates are same and draw line if true.
Its not working and returning unexpected error - new line error.
`The inputs to this node will be stored as a list in the IN variables.`
points = IN[0]
`# Place your code below this line`
lines = []
def fp(x)
firstpoint = points[x]
return firstpoint
def sp(x)
secondpoint = points[x+1]
return secondpoint
x = 0
while x <= points.Count:
if (fp(x).Y == sp(x).Y) or (fp(x).Z == sp(x).Z):
setlines = Line.ByStartPointEndPoint(fp(x), sp(x))
lines.append(setlines)
x = x + 1
`# Assign your output to the OUT variable.`
OUT = lines
As #itprorh66 points out, there's really not enough info here to definitively answer your question, but one issue is you're incorrectly comparing what I assume are floats.
fp(x).Y == sp(x).Y
Instead of comparing for direct equality, you'll need to compare for equality within a tolerance. Here is some discussion on how to do that, What is the best way to compare floats for almost-equality in Python?
I'm currently trying to determine the frequency of this plot of position vs time:
where the time and position datasets are lists of floats. I've tried using scipy.signal.find_peaks imported as fp but when I run this code:
peaks,_ = fp(pos)
peak_times = []
for i in range(len(peaks)):
peak_times.append(t[i])
peak_dists = [current-next for (current,next) in zip(peak_times,peak_times[1:])]
approx_freq = sum(peak_dists)/len(peak_dists)
return approx_freq
I get a type error: typeError: only integer scalar arrays can be converted to a scalar index
What is going wrong? And how can I fix it?
I spotted a few errors in your code. Here are the suggested corrections:
peaks,_ = fp(pos)
peak_times = []
for i in peaks: # notice correction here! No range or len needed.
peak_times.append(t[i])
peak_dists = [next-current for (current,next) in zip(peak_times,peak_times[1:])] # swapped next with current for positive result
approx_freq = 1/(sum(peak_dists)/len(peak_dists)) # take the inverse of what you did before
Good luck!
I'm trying to push data into my google sheet with the following code, how can i change the code so that it will print in the 2nd row at the correct column base on the header that I've created.
First code:
class Header:
def __init__(self):
self.No_DOB_Y=1
self.No_DOB_M=2
self.No_DOB_D=3
self.Paid_too_much_little=4
self.No_number_of_ins=5
self.No_gender=6
self.No_first_login=7
self.No_last_login=8
self.Too_young_old=9
def __repr__(self):
return str(self.__dict__)
def add_col(self,name):
setattr(self,name,max(anomali_header.__dict__.values())+1)
anomali_header=Header()
2nd part of code (NEW):
# No_gender
a = list(df.loc[df['gender'].isnull()]['id'])
#print(a)
cells=sh3.range(1,1,len(a),1)
for i,cell in enumerate(cells):
cell.value=a[i]
sh3.update_cells(cells)
At the moment it updates into A1 cell....
This is what I essentially want to
As you can see, the code writes the results onto the first available cell which is A1, i essentially want it to appear at the bottom of my anomali_header of "No_gender" but I'm not sure how to link my 1st part of the code to the 2nd part of the code...
Thanks to v25, the code below works, but rather than going through the code one by one, i wanted to create a loop which goes through all the function
I'm trying to run the code below, but it seems I get an error when I use the loop.
Error:
TypeError: 'list' object cannot be interpreted as an integer
Code:
# No_DOB_Y
a = list(df.loc[df['Year'].isnull()]['id'])
# No number of ins
b = list(df.loc[df['number of ins'].isnull()]['id'])
# No_gender
c = list(df.loc[df['gender'].isnull()]['id'])
# Updating anomalies to sheet
condition = [a,b,c]
column = [1,2,3]
for j in range(column,condition):
cells=sh3.range(2,column,len(condition)+1,column)
for i,cell in enumerate(cells):
cell.value=condition[i]
print('end of check')
sh3.update_cells(cells)
You need to change the range() parameters:
first_row (int) – Row number
first_col (int) – Row number
last_row (int) – Row number
last_col (int) – Row number
So something like:
cells=sh3.range(2, 6, len(a)+1, 6)
Or you could issue the range as a string:
cells=sh3.range('F2:F' + str(len(a)+1))
These numbers may not be perfect, but this should change the positioning. You might need to tweak the digits slightly ;)
UPDATE:
I've encountered an error use a loop, updated my original post
TypeError: 'list' object cannot be interpreted as an integer
This is happneing because the function range which you use in the for loop (not to be confused with sh3.range which is a different function altogether) expects integers, but you're passing it lists.
However, a simpler way to implement this would be to create a list of tuples which map the strings to column integers, then loop based on this. Something like:
col_map = [ ('Year', 1),
('number of ins', 5),
('gender', 6)
]
for col_tup in col_map:
df_list = list(df.loc[df[col_tup[0]].isnull()]['id'])
cells = sh3.range(2, col_tup[1], len(df_list)+1, col_tup[1])
for i, cell in enumerate(cells)
cell.value=df_list[i]
sh3.update_cells(cells)
I have written some code which takes a sample of 100 numbers and randomly samples them to increase the list of numbers, then cuts them back down to 100 at random.
I am counting the number of iterations through the loop it takes for all the numbers in the list to be the same number.
output = open("results.txt", 'w')
for i in range(100):
population = range(0, 100)
TTF = 0
while len(set(population)) != 1:
scalingfactor = np.random.poisson(5, 100)
sample = np.repeat(population, scalingfactor)
decrease-to-ten = np.random.choice(sample, 100)
population = decrease-to-ten
results += 1
output.write(str(results))
I am trying to output the numbers to a text file as a list, but I can't manage it.
output.write(str(results))
This gives me all the numbers together as one long string of numbers.
output.write(TTF)
gives me this error:
TypeError: expected a character buffer object
One way or another, you can only write character buffers to python File objects. And python won't include newlines by default when you write to a file, you have to include those yourselves.
I'll also recommend using a context manager for your File object.
See this code:
with open("results.txt", 'w') as output:
for i in range(100):
population = range(0, 100)
# TTF = 0 # Nothing is done with this number. Why is it here?
while len(set(population)) != 1:
scalingfactor = np.random.poisson(5, 100)
sample = np.repeat(population, scalingfactor)
decrease-to-ten = np.random.choice(sample, 100)
population = decrease-to-ten
results += 1
output.write(f"{results}\n") # Assuming you're using Python >= 3.6
If you're on an older version of Python that doesn't support f-string, replace f"{results}\n" with "{0}\n".format(results)
I'm using a function to build an array of strings (which happens to be 0s and 1s only), which are rather large. The function works when I am building smaller strings, but somehow the data type seems to be restricting the size of the string to 32 characters long (U32), without my having asked for it. Am I missing something simple?
As I build the strings, I am first casting them as lists so as to more easily manipulate individual characters before joining them into a string again. Am I somehow limiting my ability to use 'larger' data types by my method? The value of np.max(CM1) in this case is something like ~300 (one recent run yielded 253), but the string only come out 32 characters long...
''' Function to derive genome and count mutations in provided list of cells '''
def derive_genome_biopsy(biopsy_list, family_dict, CM1):
derived_genomes_inBx = np.zeros(len(biopsy_list)).astype(str)
for position, cell in np.ndenumerate(biopsy_list):
if cell == 0: continue
temp_parent = 2
bitstring = list('1')
bitstring += (np.max(CM1)-1)*'0'
if cell == 1:
derived_genomes_inBx[position] = ''.join(bitstring)
continue
else:
while temp_parent > 1:
temp_parent = family_dict[cell]
bitstring[cell-1] = '1'
if temp_parent == 1: break
cell = family_dict[cell]
derived_genomes_inBx[position] = ''.join(bitstring)
return derived_genomes_inBx
The specific error message I get is:
Traceback (most recent call last):
File "biopsyCA.py", line 77, in <module>
if genome[site] == '1':
IndexError: string index out of range
family_dict is a dictionary which carries a list of parents and children that the algorithm above works through to reconstruct the 'genome' of individuals from the branching family tree. it basically sets positions in the bitstring to '1' if your parent had it, then if your grandparent etc... until you get to the first bit, which is always '1', then it should be done.
The 32 character limitation comes from the conversion of float64 array to string array in this line:
derived_genomes_inBx = np.zeros(len(biopsy_list)).astype(str)
The resulting array contains datatype S32 values which limit the contents to 32 characters.
To change this limit, use 'S300' or larger instead of str.
You may also use map(str, np.zeros(len(biopsy_list)) to get more flexible string list and convert it back to numpy array with numpy.array() after you have populated it.
Thanks to help from a number of folks here and local, I finally got this working and the working function is:
''' Function to derive genome and count mutations in provided list of cells '''
def derive_genome_biopsy(biopsy_list, family_dict, CM1):
derived_genomes_inBx = list(map(str, np.zeros(len(biopsy_list))))
for biopsy in range(0,len(biopsy_list)):
if biopsy_list[biopsy] == 0:
bitstring = (np.max(CM1))*'0'
derived_genomes_inBx[biopsy] = ''.join(bitstring)
continue
bitstring = list('1')
bitstring += (np.max(CM1)-1)*'0'
if biopsy_list[biopsy] == 1:
derived_genomes_inBx[biopsy] = ''.join(bitstring)
continue
else:
temp_parent = family_dict[biopsy_list[biopsy]]
bitstring[biopsy_list[biopsy]-1] = '1'
while temp_parent > 1:
temp_parent = family_dict[position]
bitstring[temp_parent-1] = '1'
if temp_parent == 1: break
derived_genomes_inBx[biopsy] = ''.join(bitstring)
return derived_genomes_inBx
The original problem was as Teppo Tammisto pointed out an issue with the 'str' datastructure taking 'S32' format. Once I changed to using the list(map(str, ...) functionality a few more issues arose with the original code, which I've now fixed. When I finish this thesis chapter I'll publish the whole family of functions to use to virtually 'biopsy' a cellular automaton model (well, just an array really) and reconstruct 'genomes' from family tree data and the current automaton state vector.
Thanks all!