Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I enable the REFS_OK flag in nditer in numpy in Python 3.3?

Does anyone know how one goes about enabling the REFS_OK flag in numpy? I cannot seem to find a clear explanation online.

My code is:

import sys
import string
import numpy as np
import pandas as pd
SNP_df = pd.read_csv('SNPs.txt',sep='\t',index_col = None ,header = None,nrows = 101)
output = open('100 SNPs.fa','a')
for i in SNP_df:
    data = SNP_df[i]
    data = np.array(data)
    for j in np.nditer(data):
        if j == 0:
            output.write(("\n>%s\n")%(str(data(j))))
        else:
            output.write(data(j))

I keep getting the error message: Iterator operand or requested dtype holds references, but the REFS_OK was not enabled.

I cannot work out how to enable the REFS_OK flag so the program can continue...

like image 986
gwilymh Avatar asked May 03 '13 17:05

gwilymh


People also ask

What is the use of nditer() function in NumPy?

nditer () is the most popular function in numpy, and the main purpose of the nditer () function is to iterate an array of objects. 2. Is it possible to iterate a multidimensional array using the nditer () function? Yes, We can iterate multidimensional arrays using this function. 3. What is the syntax for nditer () function? 4.

What is the nditer object in Python?

The remainder of this document presents the nditer object and covers more advanced usage. The iterator object nditer, introduced in NumPy 1.6, provides many flexible ways to visit all the elements of one or more arrays in a systematic fashion.

Why do we use NumPy instead of lists?

Using lists can also iterate an array, but the main reason we are using Numpy is that “it aims to provide an array object that is faster than a traditional Python list”. How does the nditer () function work? nditer () is the most popular function in Numpy. The main purpose of the nditer () function is to iterate an array of objects.

What is the difference between refs_OK and reduce_OK in C++?

refs_ok enables iteration of reference types, such as object arrays. reduce_ok enables iteration of readwrite operands which are broadcasted, also known as reduction operands. zerosize_ok allows itersize to be zero. This is a list of flags for each operand. At minimum, one of readonly, readwrite, or writeonly must be specified.


2 Answers

I have isolated the problem. There is no need to use np.nditer. The main problem was with me misinterpreting how Python would read iterator variables in a for loop. The corrected code is below.

import sys
import string
import fileinput
import numpy as np

SNP_df = pd.read_csv('datafile.txt',sep='\t',index_col = None ,header = None,nrows = 5000)
output = open('outputFile.fa','a')

for i in range(1,51): 
    data = SNP_df[i]
    data = np.array(data)
    for j in range(0,1): 
        output.write(("\n>%s\n")%(str(data[j])))
    for k in range(1,len(data)):
        output.write(str(data[k]))
like image 106
gwilymh Avatar answered Oct 04 '22 07:10

gwilymh


If you really want to enable the flag, I have an working example.

Python 2.7, numpy 1.14.2, pandas 0.22.0

import pandas as pd
import numpy as np

# get all data as panda DataFrame
data = pd.read_csv("./monthdata.csv")
print(data)

# get values as numpy array
data_ar = data.values # numpy.ndarray, every element is a row
for row in data_ar:
    print(row)
    sum = 0
    count = 0
    for month in np.nditer(row, flags=["refs_OK"], op_flags=["readwrite"]):
        print month
like image 40
WesternGun Avatar answered Oct 04 '22 08:10

WesternGun