Nested For loop not running full

2013-04-26 Thread inshu chauhan
Hello everyone,

I have this part of my code where I am trying to traverse over an image by
running a for loop for both x and y co-ordinate axis. But the loop is
terminating by just reading first pixel. Can think of a reason why this is
happening ?

The code is:
for sy in xrange(0, segimage.height):
for sx in xrange(0, segimage.width):
if segimage[sy,sx] == (0.0, 0.0, 0.0):
continue
else:
seg_color = segimage[sy,sx]
blue = int(seg_color[0])
green = int(seg_color[1])
red = int(seg_color[2])
reg_num = blue + 256 * green + 65536 * red
for l in f:
sp = l.split(,)
if len(sp) == 14:
print sy, sx  # for checking which pixel its
reading currently
print reg_num, sp[0]  # for checking whats
happening
if reg_num == int(sp[0].strip()):
print reg_num, sp[0].strip() # for checking
whats happening
classification = int(sp[13].strip())


The inside for loop is for reading a csv format file from which I am
extracting some information.

Thanks in Advance for your suggestions
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Nested For loop not running full

2013-04-26 Thread inshu chauhan
On Fri, Apr 26, 2013 at 2:39 PM, Peter Otten __pete...@web.de wrote:

 inshu chauhan wrote:

  I have this part of my code where I am trying to traverse over an image
 by
  running a for loop for both x and y co-ordinate axis. But the loop is
  terminating by just reading first pixel. Can think of a reason why this
 is
  happening ?
 
  The code is:
  for sy in xrange(0, segimage.height):
  for sx in xrange(0, segimage.width):
  if segimage[sy,sx] == (0.0, 0.0, 0.0):
  continue
  else:
  seg_color = segimage[sy,sx]
  blue = int(seg_color[0])
  green = int(seg_color[1])
  red = int(seg_color[2])
  reg_num = blue + 256 * green + 65536 * red
  for l in f:
  sp = l.split(,)
  if len(sp) == 14:
  print sy, sx  # for checking which pixel its
  reading currently
  print reg_num, sp[0]  # for checking whats
  happening
  if reg_num == int(sp[0].strip()):
  print reg_num, sp[0].strip() # for checking
  whats happening
  classification = int(sp[13].strip())
 
 
  The inside for loop is for reading a csv format file from which I am
  extracting some information.

 My crystal ball says that the 'for sy...' and 'for sx...' loops are running
 to completion, but you don't get the coordinates printed because you put
 them into the 'for l in f' loop which will only run once.


Is there any means by which I can run this 'For l in f' loop again and
again ?


 The quick and dirty fix is to replace

 f = open(...)

 in the code you are not showing with

 f == list(open(...))


f is just a text file(csv format).. so why list ??


 The reasonable thing to do is of course to move the preprocessing (e.g.
 csv-
 parsing) out of the sy and sx loops.


I did this but again then what I intend to do is not really happening, For
every pixel I read,  I want to traverse the full file, so that the
information I am taking from pixel have to match in one of the line in the
file. Can this be done by modifying my code ? or something new has to be
devised ?




 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Nested For loop not running full

2013-04-26 Thread inshu chauhan
, 2013 at 3:15 PM, Chris Angelico ros...@gmail.com wrote:

 On Fri, Apr 26, 2013 at 7:36 PM, inshu chauhan insidesh...@gmail.com
 wrote:
 
  On Fri, Apr 26, 2013 at 2:39 PM, Peter Otten __pete...@web.de wrote:
 
  f = open(...)
 
  in the code you are not showing with
 
  f == list(open(...))
 
  f is just a text file(csv format).. so why list ??

 (That should be =, not ==)

 Instead of having an open file object, you would instead have a list
 of the lines in the file. That can be iterated over more than once.

  The reasonable thing to do is of course to move the preprocessing (e.g.
  csv-
  parsing) out of the sy and sx loops.
 
 
  I did this but again then what I intend to do is not really happening,
 For
  every pixel I read,  I want to traverse the full file, so that the
  information I am taking from pixel have to match in one of the line in
 the
  file. Can this be done by modifying my code ? or something new has to be
  devised ?

 How large is the file? There are two easy solutions:

 1) Open and close the file every time you touch a pixel
 2) Open the file once, read it all into memory, and then iterate over
 the in-memory copy every pixel

 If your file is insanely large then the first option may be better,
 but for anything less than five yottabytes, go with the second. (Okay,
 I may be exaggerating slightly... let's say anything less than half
 your RAM. So if you have 10YB of memory, then I wasn't exaggerating.)
 That's why Peter suggested creating a list; you iterate over the list.
 Another way to do it is to parse the file once and retain a more
 efficient and useful structured form of the data... which is the other
 thing Peter suggested (move the preprocessing (e.g. csv-
 parsing) out of the sy and sx loops).

 So, yeah. Listen to Peter Otten, he knows what he's talking about :)

 ChrisA


Yes I am trying Peter's way and my file is just 500 KB .. :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Running simultaneuos FOR loops

2013-04-23 Thread inshu chauhan
i have to implement the below line in one of my code:

for  p in sorted(segments.iterkeys()) and for k in
sorted(class_count.iterkeys()) and for j in sorted(pixel_count.iterkeys()):

Its giving me a syntax error which is obvious, but how can I make all three
for loop run simultaneously or any other way to do this simultaneous work
???
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running simultaneuos FOR loops

2013-04-23 Thread inshu chauhan
Yes Simultaneously means all three running at the same time, I looked up
zip just now, but will it not disturb my dictionaries ?
And yes the dictionaries have same number of keys.

thanks


On Tue, Apr 23, 2013 at 12:16 PM, Chris Angelico ros...@gmail.com wrote:

 On Tue, Apr 23, 2013 at 4:40 PM, inshu chauhan insidesh...@gmail.com
 wrote:
  i have to implement the below line in one of my code:
 
  for  p in sorted(segments.iterkeys()) and for k in
  sorted(class_count.iterkeys()) and for j in
 sorted(pixel_count.iterkeys()):
 
  Its giving me a syntax error which is obvious, but how can I make all
 three
  for loop run simultaneously or any other way to do this simultaneous work
  ???

 Define simultaneously. Do the three dictionaries have the same number
 of keys? If so, look up zip() or itertools.izip; if not, you may have
 to more clearly define simultaneous.

 ChrisA
 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running simultaneuos FOR loops

2013-04-23 Thread inshu chauhan
zip isn't doing the required


On Tue, Apr 23, 2013 at 12:28 PM, inshu chauhan insidesh...@gmail.comwrote:

 Yes Simultaneously means all three running at the same time, I looked up
 zip just now, but will it not disturb my dictionaries ?
 And yes the dictionaries have same number of keys.

 thanks


 On Tue, Apr 23, 2013 at 12:16 PM, Chris Angelico ros...@gmail.com wrote:

 On Tue, Apr 23, 2013 at 4:40 PM, inshu chauhan insidesh...@gmail.com
 wrote:
  i have to implement the below line in one of my code:
 
  for  p in sorted(segments.iterkeys()) and for k in
  sorted(class_count.iterkeys()) and for j in
 sorted(pixel_count.iterkeys()):
 
  Its giving me a syntax error which is obvious, but how can I make all
 three
  for loop run simultaneously or any other way to do this simultaneous
 work
  ???

 Define simultaneously. Do the three dictionaries have the same number
 of keys? If so, look up zip() or itertools.izip; if not, you may have
 to more clearly define simultaneous.

 ChrisA
 --
 http://mail.python.org/mailman/listinfo/python-list



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running simultaneuos FOR loops

2013-04-23 Thread inshu chauhan
Thanks Gary.




 Be clearer about the problem please.

 Do you wish to produce a loop that:
   On pass 1, each of p,k, and t hold the first item of their respective
 lists, and
   on pass 2, each of p,k, and t hold the second item of their respective
 lists, and
   so on
 until one (or all) lists run out?


Yes this is excatly what I want each loop holds the first item on each
pass.


 If that is what you want, then check out the zip builtin function.  But
 also consider this:  Do you care what happens if one list runs out before
 the others?


Yes, but all dictionaries have same number of items.


 Or is it something else you want?  Perhaps nested loops?
   for  p in sorted(segments.iterkeys()):
   for k in sorted(class_count.iterkeys()):
   for j in sorted(pixel_count.iterkeys()):
  # This will be run with all possible combinations of p,k, and
 t


No, I know about nested loops but I dont want that because all the loops
have same number of items, inner loops will run out earlier.



 Gary Herron





 --
 http://mail.python.org/mailman/listinfo/python-list


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Running simultaneuos FOR loops

2013-04-23 Thread inshu chauhan
This statement is giving me the following error

Statement:
for p, k, j in zip(sorted(segments.iterkeys(), class_count.iterkeys(),
pixel_count.iterkeys())):

Error:
Traceback (most recent call last):
  File C:\Users\inshu\Desktop\Training_segs_trial2.py, line 170, in
module
access_segments(segimage, data)
  File C:\Users\inshu\Desktop\Training_segs_trial2.py, line 147, in
access_segments
for p, k, j in zip(sorted(segments.iterkeys(), class_count.iterkeys(),
pixel_count.iterkeys())):
TypeError: 'dictionary-keyiterator' object is not callable





On Tue, Apr 23, 2013 at 12:33 PM, inshu chauhan insidesh...@gmail.comwrote:

 zip isn't doing the required


 On Tue, Apr 23, 2013 at 12:28 PM, inshu chauhan insidesh...@gmail.comwrote:

 Yes Simultaneously means all three running at the same time, I looked up
 zip just now, but will it not disturb my dictionaries ?
 And yes the dictionaries have same number of keys.

 thanks


 On Tue, Apr 23, 2013 at 12:16 PM, Chris Angelico ros...@gmail.comwrote:

 On Tue, Apr 23, 2013 at 4:40 PM, inshu chauhan insidesh...@gmail.com
 wrote:
  i have to implement the below line in one of my code:
 
  for  p in sorted(segments.iterkeys()) and for k in
  sorted(class_count.iterkeys()) and for j in
 sorted(pixel_count.iterkeys()):
 
  Its giving me a syntax error which is obvious, but how can I make all
 three
  for loop run simultaneously or any other way to do this simultaneous
 work
  ???

 Define simultaneously. Do the three dictionaries have the same number
 of keys? If so, look up zip() or itertools.izip; if not, you may have
 to more clearly define simultaneous.

 ChrisA
 --
 http://mail.python.org/mailman/listinfo/python-list




-- 
http://mail.python.org/mailman/listinfo/python-list


Working with lists within Dictionaries

2013-04-18 Thread inshu chauhan
Hello Everyone,

I am trying to work with lists and dictionaries together.
In the following code I have to access pixels in a segmented image through
a dictionary. But the problem is when I am trying to update the list
through dict it is giving me this error of tuple, ofcourse because list
indices should be integer.

THE CODE IS :
import cv
def access_segments(segimage, data):
print segimage
segments = {}
for y in xrange(0, segimage.height):
for x in xrange(0, segimage.width):
if segimage[y,x] == (0.0, 0.0, 0.0):
continue
else:
seg_color = segimage[y,x]
blue = int(seg_color[0])
green = int(seg_color[1])
red = int(seg_color[2])
reg_num = blue + 256 * green + 65536 * red
point = data[y,x]
segments.setdefault(reg_num, [])[point] += point

for p in sorted(segments.iterkeys()):
points = (segments[p])
print len(points)
print points

if __name__== __main__:
data = cv.Load(rC:\Users\inshu\Desktop\Masters
Thesis\data\xyz_0.yml)
segimage = cv.LoadImageM(rC:\Users\inshu\Desktop\Masters
Thesis\Segmentation\segmentation_numbers_0.tif,
cv.CV_LOAD_IMAGE_UNCHANGED)
access_segments(segimage, data)


THE ERROR IS:

cvmat(type=42424010 8UC3 rows=3000 cols=3000 step=9000 )

Traceback (most recent call last):
  File C:\Users\inshu\Desktop\test_reading .py, line 27, in module
access_segments(segimage, data)
  File C:\Users\inshu\Desktop\test_reading .py, line 16, in
access_segments
segments.setdefault(reg_num, [])[point] += point
TypeError: list indices must be integers, not tuple

How can I access the data without getting this error ? the points have x,
y, z co-ordinates.

Thanks In Advance for your suggestions.
-- 
http://mail.python.org/mailman/listinfo/python-list


Can I iterate over a dictionary outside a function ?

2013-04-11 Thread inshu chauhan
I have a prog in which a functions returns a dict but when I try to iterate
over the dict using iterkeys, It shows an error. I think its because only
address of the dictionary is returned so cannot be iterated upon.

Please suggest some way by which it can be made possible to iterate over
the dictionary using  iterkeys outside the function ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Trying to understand working with dicts

2013-04-05 Thread inshu chauhan
Hello everyone,

Here in my part of the code where cc is a dictionary. I want to understand
what actually cc.iterkeys() and cc[k] actually doing.
I am already reading
http://docs.python.org/2/library/stdtypes.html#dict.items
and http://www.tutorialspoint.com/python/python_dictionary.htm but still
not very clear.

cc = Computesegclass(segimage, refimage)
for k in sorted(cc.iterkeys()):
i = argmax(cc[k])
print  f, i+1

Thanks in Advance
-- 
http://mail.python.org/mailman/listinfo/python-list


Error in working with Dict

2013-04-01 Thread inshu chauhan
I have this program which is working with 2 dictionaries segments,
class_counts.. but I am getting an error mentioned below the programme.

import cv
from itertools import *
from math import floor, sqrt, ceil
from numpy import array, dot, subtract, add, outer, argmax, linalg as lin


def Computesegclass(segimage, refimage):
f = open(Pixel_count_with_region_num_trial1.txt, w)
segments = {}
class_count = {}
for y in xrange(0, segimage.height):
for x in xrange(0, segimage.width):

if segimage[y,x] == (0.0, 0.0, 0.0):
continue
else:
seg_color = segimage[y,x]
blue = int(seg_color[0])
green = int(seg_color[1])
red = int(seg_color[2])
region_num = blue + 256 * green + 65536 * red
#print region_num
segments[region_num] = segments.setdefault(region_num, 0) +
1
#print segments

class_color = refimage[y,x]

if class_color == (0.0,0.0,0.0):
class_number = 0  # No class
elif class_color == (0.0,255.0,0.0):
class_number = 1   # Trees
elif class_color == (255.0, 0.0, 128.0):
class_number = 2   # Buildings
elif class_color == (0.0,0.0,255.0):
class_number = 3# Automobiles
elif class_color == (255.0, 255.0, 0.0):
class_number = 4# Road Points
elif class_color == (0.0, 64.0, 128.0):
class_number = 5# Tree trunks nad branches
elif class_color == (255.0, 0.0 ,0.0):
class_number = 6# Poles
elif class_color == (255.0, 0.0, 255.0):
class_number = 7# Traffic Lights
else:
class_number = 0 # Gray Pixel

class_count.setdefault(region_num, [0, 0, 0, 0, 0, 0, 0,
0])[class_number] += 1
# print class_count
for k in sorted(class_count.iterkeys()):
i = argmax(class_count[k])
print  f, i

if __name__== __main__:
segimage = cv.LoadImageM(rC:\Users\inshu\Desktop\Masters
Thesis\Segmentation\segmentation_numbers_0.tif,
cv.CV_LOAD_IMAGE_UNCHANGED)
refimage = cv.LoadImageM(rC:\Users\inshu\Desktop\Masters
Thesis\Segmentation\Hand_Classified1.tif, cv.CV_LOAD_IMAGE_UNCHANGED)
print segimage
Computesegclass(segimage, refimage)


ERROR :

Traceback (most recent call last):
  File C:\Users\inshu\Desktop\seg.py, line 49, in module
for k in sorted(class_count.iterkeys()):
NameError: name 'class_count' is not defined

I know this error is because I have initialized both dicts inside the
function, But why dicts are not saved out, if I intialize the dicts outside
the function, the processing is still done inside and end result I am
getting is an empty dict.


Thanks in Advance for suggestions
-- 
http://mail.python.org/mailman/listinfo/python-list


working with dict : incrementing dict dynamically

2013-03-11 Thread inshu chauhan
I am trying to create a dictionary with a key and its values seraching from
a data set. But something is going wrong. This is the first time I am
working with dicts.

My code is :

import cv
def Computesegclass(segimage):
num_pixel = 0
for y in xrange(0, segimage.height):
for x in xrange(0, segimage.width):

if segimage[y,x] == (0.0, 0.0, 0.0):
continue
else:
color = segimage[y,x]
blue = color[0]
green = color[1]
red = color[2]
region_num = blue + 256 * green + 65536 * red
print region_num
segments = dict({region_num : num_pixel})
if region_num == region_num:
num_pixel = +1
print segments

if __name__== __main__:
segimage =
cv.LoadImageM(Z:/Segmentation/segmentation_numbers_0.tif,
cv.CV_LOAD_IMAGE_UNCHANGED)
print segimage
Computesegclass(segimage)


here I am traversing through an image which is 3000 x 3000. for each pixel
I calculate a region number. What I am trying to do is increase the
num_pixel by 1 if that pixel falls in the same region which is identified
by region_num. How can I do it in dict created ?

Thanx in Advance
-- 
http://mail.python.org/mailman/listinfo/python-list


working with csv module in python

2013-02-20 Thread inshu chauhan
I have 10 simple text files with 3 columns x,y,z delimited by space. I am
trying to combine these 10 files to get a single text file.

Eg. of data in 10 files is
299 446 2


I had written this prog for merging files:


import csv
import glob

with open(rC:\Users\inshu.chauhan\Desktop\test2.arff, w) as w:
writer = csv.writer(w, delimiter = ' ', quotechar = ' ' ,quoting =
csv.QUOTE_MINIMAL)
for f in glob.glob(rC:\Users\inshu.chauhan\Desktop\For
Model_600\*.arff):
rows = open(f, r).readlines()
writer.writerows(rows)



But the result after merging the files is not I need , it is something like
:
2 9 9  4 4 6  2


2 9 9  4 4 7  2


2 9 9  4 4 8  2


2 9 8  4 4 9  2


Can in some some way I set delimiter to NULL as the prog gives me error if
I do so. I dont why there is space between the attribute of first column in
reading and there is space between every row too..

Or can I merge these text files without using csv module , directly in
python ?

Looking forward to your suggestions.

Thanks in advance !!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: working with csv module in python

2013-02-20 Thread inshu chauhan
On Wed, Feb 20, 2013 at 11:26 AM, Roland Koebler r.koeb...@yahoo.de wrote:

 Hi,

 On Wed, Feb 20, 2013 at 10:50:54AM +0100, inshu chauhan wrote:
  I have 10 simple text files with 3 columns x,y,z delimited by space. I
 am
  trying to combine these 10 files to get a single text file.
 
  Eg. of data in 10 files is
  299 446 2
 Do you only want to concat the files, or do you want to parse/mangle
 them?

 If you only want to concat the files, I would use some shell-tools,
 like cat on Linux or copy on Windows, so

 copy C:\Users\inshu.chauhan\Desktop\ForModel_600\*.arff
 C:\Users\inshu.chauhan\Desktop\test2.arff

 should do it.

  Can in some some way I set delimiter to NULL as the prog gives me error
 if
  I do so.
 Of course -- a CSV without a delimiter doesn't make any sense.

  I dont why there is space between the attribute of first column in
  reading and there is space between every row too..
 Because there's a split() missing in your code. You currently tell the
 CSV-writer to write the columns 2,9,9, , , ,4,4,6, , , ,2 as
 space-separated CSV. So, try something like
 rows = [r.split() for r in open(f, r).readlines()]

  Or can I merge these text files without using csv module , directly in
  python ?
 If you don't need to parse/mangle the contents, you don't need the csv
 module. Simple open the resulting file for writing, and then read out
 the source files and write their contents into the resulting file.


 Yes I just want to concat the files , not parse/mangle the files.  How can
i simply read all files in a folder in my computer and write them into a
single file ? just by 'printf ' is it possible ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: working with csv module in python

2013-02-20 Thread inshu chauhan
On Wed, Feb 20, 2013 at 11:38 AM, inshu chauhan insidesh...@gmail.comwrote:




 On Wed, Feb 20, 2013 at 11:26 AM, Roland Koebler r.koeb...@yahoo.dewrote:

 Hi,

 On Wed, Feb 20, 2013 at 10:50:54AM +0100, inshu chauhan wrote:
  I have 10 simple text files with 3 columns x,y,z delimited by space.
 I am
  trying to combine these 10 files to get a single text file.
 
  Eg. of data in 10 files is
  299 446 2
 Do you only want to concat the files, or do you want to parse/mangle
 them?

 If you only want to concat the files, I would use some shell-tools,
 like cat on Linux or copy on Windows, so

 copy C:\Users\inshu.chauhan\Desktop\ForModel_600\*.arff
 C:\Users\inshu.chauhan\Desktop\test2.arff

 should do it.

  Can in some some way I set delimiter to NULL as the prog gives me error
 if
  I do so.
 Of course -- a CSV without a delimiter doesn't make any sense.

  I dont why there is space between the attribute of first column in
  reading and there is space between every row too..
 Because there's a split() missing in your code. You currently tell the
 CSV-writer to write the columns 2,9,9, , , ,4,4,6, , , ,2 as
 space-separated CSV. So, try something like
 rows = [r.split() for r in open(f, r).readlines()]

  Or can I merge these text files without using csv module , directly in
  python ?
 If you don't need to parse/mangle the contents, you don't need the csv
 module. Simple open the resulting file for writing, and then read out
 the source files and write their contents into the resulting file.


 Yes I just want to concat the files , not parse/mangle the files.  How
 can i simply read all files in a folder in my computer and write them into
 a single file ? just by 'printf ' is it possible ?


 For simple concating the files , I tried the following code :

 import glob

with open(rC:\Users\inshu.chauhan\Desktop\test2.arff, w) as w:
print w
for f in glob.glob(rC:\Users\inshu.chauhan\Desktop\For
Model_600\*.arff):
g = f.read()
w.write(g)


But I think I am having an obvious error :

open file 'C:\Users\inshu.chauhan\Desktop\test2.arff', mode 'w' at
0x01B64F40

Traceback (most recent call last):
  File C:\Users\inshu.chauhan\Desktop\Concatfiles.py, line 6, in module
g = f.read()
AttributeError: 'str' object has no attribute 'read'

Here I am trying to refer the files stored in my folder by 'f',  Why read()
is not working ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: working with csv module in python

2013-02-20 Thread inshu chauhan
On Wed, Feb 20, 2013 at 12:04 PM, Dave Angel da...@davea.name wrote:

 On 02/20/2013 05:38 AM, inshu chauhan wrote:

 On Wed, Feb 20, 2013 at 11:26 AM, Roland Koebler r.koeb...@yahoo.de
 wrote:


 snip


 If you only want to concat the files, I would use some shell-tools,
 like cat on Linux or copy on Windows, so

 copy C:\Users\inshu.chauhan\**Desktop\ForModel_600\*.arff
 C:\Users\inshu.chauhan\**Desktop\test2.arff

 should do it.

   snip

  Yes I just want to concat the files , not parse/mangle the files.  How
 can
 i simply read all files in a folder in my computer and write them into a
 single file ? just by 'printf ' is it possible ?




 Reread Roland's message, quoted above for your convenience.

 Not sure what you mean by printf.  Why would you use C to do it?

 If you need to do it in Python, remember you can read a file with the
 read() method, and write one with the write() method.  They pay no
 attention to newlines, delimiters or anything else.  Just create the output
 file, then in a loop open the input files and loop through each (read
 doesn't necessarily get the whole thing in one pass)

 You'll spend lots more energy on the mechanics of finding the files then
 on the copying.  But perhaps the shutil module will have some further
 shortcuts.

 --
 DaveA
 --
 http://mail.python.org/**mailman/listinfo/python-listhttp://mail.python.org/mailman/listinfo/python-list


You are right Dave, printf was a blunder
-- 
http://mail.python.org/mailman/listinfo/python-list


IOerror : need urgent help

2013-02-19 Thread inshu chauhan
Here is my attempt to merge 10 files stored in a folder into a single file :

import csv

with open(C:\Users\inshu.chauhan\Desktop\test.arff, w) as w:
writer = csv.writer(w)
for f in glob.glob(C:\Users\inshu.chauhan\Desktop\For
Model_600\*.arff):
rows = open(f, r).readlines()
writer.writerows(rows)


Error:

Traceback (most recent call last):
  File C:\Users\inshu.chauhan\Desktop\Mergefiles.py, line 3, in module
with open(C:\Users\inshu.chauhan\Desktop\test.arff, w) as w:
IOError: [Errno 22] invalid mode ('w') or filename:
'C:\\Users\\inshu.chauhan\\Desktop\test.arff'

Why my programme is not working ?? :(

Thanks in Advance !!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: IOerror : need urgent help

2013-02-19 Thread inshu chauhan
On Tue, Feb 19, 2013 at 4:54 PM, MRAB pyt...@mrabarnett.plus.com wrote:

 On 2013-02-19 15:27, inshu chauhan wrote:

 Here is my attempt to merge 10 files stored in a folder into a single
 file :

 import csv

 with open(C:\Users\inshu.chauhan\**Desktop\test.arff, w) as w:
  writer = csv.writer(w)
  for f in glob.glob(C:\Users\inshu.**chauhan\Desktop\For
 Model_600\*.arff):
  rows = open(f, r).readlines()
  writer.writerows(rows)


 Error:

 Traceback (most recent call last):
File C:\Users\inshu.chauhan\**Desktop\Mergefiles.py, line 3, in
 module
  with open(C:\Users\inshu.chauhan\**Desktop\test.arff, w) as w:
 IOError: [Errno 22] invalid mode ('w') or filename:
 'C:\\Users\\inshu.chauhan\\**Desktop\test.arff'

 Why my programme is not working ?? :(

  Look at the traceback. It says that the path is:

 'C:\\Users\\inshu.chauhan\\**Desktop\test.arff'

 All but one of the backslashes are doubled.

 That's because the backslash character \ starts an escape sequence, but
 if it can't recognise the escape sequence, it treats the backslash as a
 literal character.

 In that string literal, '\t' is an escape sequence representing a tab
 character (it's equal to chr(9)), but '\U', '\i' and '\D' are not
 escape sequences, so they are equivalent to '\\U, '\\i' and '\\D'
 respectively.

 What you should do is use raw string literals for paths:


 rC:\Users\inshu.chauhan\**Desktop\test.arff

 or use '/' instead (Windows allows it as an alternative, unless it
 occurs initially, which you'll rarely want to do in practice):

 C:/Users/inshu.chauhan/**Desktop/test.arff

 --
 http://mail.python.org/**mailman/listinfo/python-listhttp://mail.python.org/mailman/listinfo/python-list


Thanks I understood the problem now and my programme is working !!
-- 
http://mail.python.org/mailman/listinfo/python-list


Error in reading and writing CSV format file in python

2013-02-11 Thread inshu chauhan
In the programme below I am trying to read two csv format files and process
them and write a new file with some of theirs data.

import csv
f1_reader = csv.reader(open(rZ:\Weka
work\Feature_Vectors_Fullset_00.arff))
f2_reader = csv.reader(open(rZ:\Weka
work\Feature_Vectors_Fullset_00_noxy+class.arff))
nf = open(rZ:\Weka work\classified_image00_withoutxy.arff, w)

while True:
l1 = f1_reader.next()
while len(l1) != 12:
l1 = f1_reader.next()
l2 = f2_reader.next()
while len(l2) != 11:
l2 = f2_reader.next()

ix = l1[0].strip()
iy = l1[1].strip()
classification = l2[8].strip()

print  nf, ix, iy, classification

nf.close()

This programme is giving me this error now :

Traceback (most recent call last):
  File Z:\Weka work\final_image_classificationwithoutxy.py, line 16, in
module
l2 = f2_reader.next()
StopIteration


what could be a possible reason to StopIteration ???


I checked the syntax and usage of this module looks alright to me , but
then why this error ?


Thankyou in Advance
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Error in reading and writing CSV format file in python

2013-02-11 Thread inshu chauhan
On Mon, Feb 11, 2013 at 1:26 PM, Steven D'Aprano 
steve+comp.lang.pyt...@pearwood.info wrote:

 inshu chauhan wrote:

  In the programme below I am trying to read two csv format files and
  process them and write a new file with some of theirs data.
 
  import csv
  f1_reader = csv.reader(open(rZ:\Weka
  work\Feature_Vectors_Fullset_00.arff))
  f2_reader = csv.reader(open(rZ:\Weka
  work\Feature_Vectors_Fullset_00_noxy+class.arff))
  nf = open(rZ:\Weka work\classified_image00_withoutxy.arff, w)
 
  while True:
  l1 = f1_reader.next()
  while len(l1) != 12:
  l1 = f1_reader.next()
  l2 = f2_reader.next()
  while len(l2) != 11:
  l2 = f2_reader.next()
 
  ix = l1[0].strip()
  iy = l1[1].strip()
  classification = l2[8].strip()
 
  print  nf, ix, iy, classification
 
  nf.close()
 
  This programme is giving me this error now :
 
  Traceback (most recent call last):
File Z:\Weka work\final_image_classificationwithoutxy.py, line 16, in
  module
  l2 = f2_reader.next()
  StopIteration
 
 
  what could be a possible reason to StopIteration ???

 next() raises StopIteration when there is nothing else to return.


 py it = iter([1, 2, 3])
 py it.next()
 1
 py it.next()
 2
 py it.next()
 3
 py it.next()
 Traceback (most recent call last):
   File stdin, line 1, in module
 StopIteration


 You have reached the end of the file and there is nothing else for the CSV
 reader to return, so it raises StopIteration.



But why does it has nothing to return so early before traversing the whole
file ? Is there any way it can be corrected ?  And also the programme isn't
writing anything to the file ?




-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Error in reading and writing CSV format file in python

2013-02-11 Thread inshu chauhan
On Mon, Feb 11, 2013 at 2:02 PM, MRAB pyt...@mrabarnett.plus.com wrote:

 On 2013-02-11 12:44, inshu chauhan wrote:


 On Mon, Feb 11, 2013 at 1:26 PM, Steven D'Aprano
 steve+comp.lang.python@**pearwood.infosteve%2bcomp.lang.pyt...@pearwood.info
 mailto:steve+comp.lang.**pyt...@pearwood.infosteve%2bcomp.lang.pyt...@pearwood.info
 wrote:

 inshu chauhan wrote:

   In the programme below I am trying to read two csv format files and
   process them and write a new file with some of theirs data.
  
   import csv
   f1_reader = csv.reader(open(rZ:\Weka
   work\Feature_Vectors_Fullset_**00.arff))
   f2_reader = csv.reader(open(rZ:\Weka
   work\Feature_Vectors_Fullset_**00_noxy+class.arff))
   nf = open(rZ:\Weka work\classified_image00_**withoutxy.arff,
 w)
  
   while True:
   l1 = f1_reader.next()
   while len(l1) != 12:
   l1 = f1_reader.next()
   l2 = f2_reader.next()
   while len(l2) != 11:
   l2 = f2_reader.next()
  
   ix = l1[0].strip()
   iy = l1[1].strip()
   classification = l2[8].strip()
  
   print  nf, ix, iy, classification
  
   nf.close()
  
   This programme is giving me this error now :
  
   Traceback (most recent call last):
 File Z:\Weka work\final_image_**classificationwithoutxy.py,
 line 16, in
   module
   l2 = f2_reader.next()
   StopIteration
  
  
   what could be a possible reason to StopIteration ???

 next() raises StopIteration when there is nothing else to return.


 py it = iter([1, 2, 3])
 py it.next()
 1
 py it.next()
 2
 py it.next()
 3
 py it.next()
 Traceback (most recent call last):
File stdin, line 1, in module
 StopIteration


 You have reached the end of the file and there is nothing else for
 the CSV
 reader to return, so it raises StopIteration.



 But why does it has nothing to return so early before traversing the
 whole file ? Is there any way it can be corrected ?  And also the
 programme isn't writing anything to the file ?

  Try adding some logging so that you can see what it's doing. A simple way
 would be something like:

 log_file = open(rZ:\Weka work\log.txt, w)

 ...

 l1 = f1_reader.next()
 print  log_file, Read from f1:, l1
 print  log_file, Length is, len(l1)

 while len(l1) != 12:
 l1 = f1_reader.next()
 print  log_file, Read from f1:, l1
 print  log_file, Length is, len(l1)

 and so on.
 --
 http://mail.python.org/**mailman/listinfo/python-listhttp://mail.python.org/mailman/listinfo/python-list



Thanks :) This worked !!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Error in reading and writing CSV format file in python

2013-02-11 Thread inshu chauhan
On Mon, Feb 11, 2013 at 3:22 PM, Dave Angel da...@davea.name wrote:

 On 02/11/2013 06:00 AM, inshu chauhan wrote:

 In the programme below I am trying to read two csv format files and
 process
 them and write a new file with some of theirs data.

 import csv
 f1_reader = csv.reader(open(rZ:\Weka
 work\Feature_Vectors_Fullset_**00.arff))
 f2_reader = csv.reader(open(rZ:\Weka
 work\Feature_Vectors_Fullset_**00_noxy+class.arff))
 nf = open(rZ:\Weka work\classified_image00_**withoutxy.arff, w)

 while True:
  l1 = f1_reader.next()
  while len(l1) != 12:
  l1 = f1_reader.next()
  l2 = f2_reader.next()
  while len(l2) != 11:
  l2 = f2_reader.next()

  ix = l1[0].strip()
  iy = l1[1].strip()
  classification = l2[8].strip()

  print  nf, ix, iy, classification

 nf.close()

 This programme is giving me this error now :

 Traceback (most recent call last):
File Z:\Weka work\final_image_**classificationwithoutxy.py, line
 16, in
 module
  l2 = f2_reader.next()
 StopIteration


 what could be a possible reason to StopIteration ???


 I checked the syntax and usage of this module looks alright to me , but
 then why this error ?



 That's not an error, it's just a normal way to end a for-loop.  If you
 were using a syntax like:
   for item in f2_reader:

 the StopIteration would simply end the loop.  Since you're doing it
 manually, you have to handle the exception yourself.


 --
 DaveA
 --
 http://mail.python.org/**mailman/listinfo/python-listhttp://mail.python.org/mailman/listinfo/python-list


Yes , therefore I used try and except.
-- 
http://mail.python.org/mailman/listinfo/python-list


Run time Error

2013-02-05 Thread inshu chauhan
Hello all,

I am trying to run a small code of mine but I am getting a run time error.
What is actually meant by run time error ? it is saying to contact
programme administrator something. Why one would get this error ? and how
to remove it ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Run time Error

2013-02-05 Thread inshu chauhan
On Tue, Feb 5, 2013 at 3:10 PM, Roy Smith r...@panix.com wrote:

 In article mailman.1366.1360073151.2939.python-l...@python.org,
  inshu chauhan insidesh...@gmail.com wrote:

  Hello all,
 
  I am trying to run a small code of mine but I am getting a run time
 error.
  What is actually meant by run time error ? it is saying to contact
  programme administrator something. Why one would get this error ? and how
  to remove it ?

 Start by telling us:

 1) What code you are running (copy-paste the exact source code).

 2) What error message you got (likewise, copy-paste the exact message).

 3) What version of Python you're running.

 4) What system and version you're running it on (Linux, OSX, Windows,
 etc).
 --
 http://mail.python.org/mailman/listinfo/python-list


Thans to all.. but  I resolved the problem :)
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading data from 2 different files and writing to a single file

2013-01-30 Thread inshu chauhan
On Mon, Jan 28, 2013 at 6:05 PM, Dennis Lee Bieber wlfr...@ix.netcom.comwrote:

 On Mon, 28 Jan 2013 14:31:31 +0100, inshu chauhan
 insidesh...@gmail.com declaimed the following in
 gmane.comp.python.general:

  In the code below I am trying to read 2 files f1 and f2 , extract some
 data
  from them and then trying to write them into a single file that is 'nf'.
 
  import cv
  f1 = open(rZ:\modules\Feature_Vectors_300.arff)
  f2 = open(rZ:\modules\Feature_Vectors_300_Pclass.arff)
  nf = open(rZ:\modules\trial.arff, w)
 
 
  for l in f1:
  sp = l.split(,)

 If you are going to be splitting on commas, you might want to read
 up on the csv (comma separate values) module


The  csv module has many fuctions but not of much use to me and it makes my
programme slower


 
  if len(sp)!= 12:
  continue
  else:

 Given the apparent block structure, you could drop the
 continue/else, and more cleanly just use


Yeah, Thats Right


 if len(sp) == 12:
  ix = sp[0].strip()
  iy = sp[1].strip()
  print ix, iy
 
 for s in f2:

 It's been mentioned that the indentation is wrong here


I dont know why the indentation is wrong ?


  st = s.split(,)
 
 csv module again

  if len(st)!= 11:
  continue
  else:

 I'm tempted to repeat the comment on reversing the conditional BUT

  clas = st[10].strip()
 
   print ix, iy, clas
   print  nf, ix, iy, clas
 
 The indentation of the print statements is not aligned with the
 previous assignment -- the effect is the same however as everything
 under the else is executed anyway.

 But as has also been mentioned, ignoring indentation, the apparent
 algorithm you have here is going to process every line of f2 for the
 first line of f1 -- and then for later lines in f1 it will find f2 is at
 the end of file, and do nothing. If it is supposed to process every line
 of f2 for each line of f1, you'll need to rewind f2.


For that I added 'Break' statement as suggested by Chris in above mails.


 If you mean to match one line of f1 with one line of f2, you do not
 want nested loops. But now you have to define the behavior if one of the
 two files is correct length and the other is not? Do you skip both or
 read the next line from the wrong length file? And how will you handle
 files with different numbers of records.


Yes , actually my Prog was like this :
for l in f1:
sp = l.split(,)

if len(sp)!= 12:
continue
else:
ix = sp[0].strip()
iy = sp[1].strip()


for s in f2:
st = s.split(,)

if len(st)!= 11:
continue
else:
clas = st[10].strip()

print ix, iy, clas
print  nf, ix, iy, clas
break


f1.close()
f2.close()
nf.close()

I actually dont want nested loops but cant find another way to achieve what
I want, But for these files I am sure that they have equal lengths, thats
why I am taking the risk of using nested loops.. Can you suggest any
different way to go around this problem , which could be flexible and
non-errorneous ?





 --
 Wulfraed Dennis Lee Bieber AF6VN
 wlfr...@ix.netcom.comHTTP://wlfraed.home.netcom.com/

 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading data from 2 different files and writing to a single file

2013-01-30 Thread inshu chauhan
On Wed, Jan 30, 2013 at 2:23 PM, Dave Angel d...@davea.name wrote:

 On 01/30/2013 05:43 AM, inshu chauhan wrote:

 On Mon, Jan 28, 2013 at 6:05 PM, Dennis Lee Bieber wlfr...@ix.netcom.com
 wrote:

  On Mon, 28 Jan 2013 14:31:31 +0100, inshu chauhan
 insidesh...@gmail.com declaimed the following in
 gmane.comp.python.general:

  In the code below I am trying to read 2 files f1 and f2 , extract some

 data

 from them and then trying to write them into a single file that is 'nf'.

 import cv
 f1 = open(rZ:\modules\Feature_**Vectors_300.arff)
 f2 = open(rZ:\modules\Feature_**Vectors_300_Pclass.arff)
 nf = open(rZ:\modules\trial.arff, w)


 for l in f1:
  sp = l.split(,)


  If you are going to be splitting on commas, you might want to
 read
 up on the csv (comma separate values) module


 The  csv module has many fuctions but not of much use to me and it makes
 my
 programme slower



  if len(sp)!= 12:
  continue
  else:


  Given the apparent block structure, you could drop the
 continue/else, and more cleanly just use


 Yeah, Thats Right


  if len(sp) == 12:

  ix = sp[0].strip()
  iy = sp[1].strip()
  print ix, iy

 for s in f2:


  It's been mentioned that the indentation is wrong here


 I dont know why the indentation is wrong ?


 Your for statement is not lined up with the print that precedes it.  If
 your code were really that way, you'd be getting an indentation error. So
 we assume it's because your email editor is mangling the code.  Post in
 text email, not in html.




   st = s.split(,)

   csv module again

   if len(st)!= 11:
  continue
  else:


  I'm tempted to repeat the comment on reversing the conditional
 BUT

   clas = st[10].strip()

   print ix, iy, clas
   print  nf, ix, iy, clas

   The indentation of the print statements is not aligned with
 the
 previous assignment -- the effect is the same however as everything
 under the else is executed anyway.

  But as has also been mentioned, ignoring indentation, the
 apparent
 algorithm you have here is going to process every line of f2 for the
 first line of f1 -- and then for later lines in f1 it will find f2 is at
 the end of file, and do nothing. If it is supposed to process every line
 of f2 for each line of f1, you'll need to rewind f2.


 For that I added 'Break' statement as suggested by Chris in above mails.


  If you mean to match one line of f1 with one line of f2, you do
 not
 want nested loops. But now you have to define the behavior if one of the
 two files is correct length and the other is not? Do you skip both or
 read the next line from the wrong length file? And how will you handle
 files with different numbers of records.


 Yes , actually my Prog was like this :
 for l in f1:
  sp = l.split(,)

  if len(sp)!= 12:
  continue
  else:
  ix = sp[0].strip()
  iy = sp[1].strip()


 for s in f2:


 This is not nested, it's back at the left margin.  Or it could be posting
 wrong because you're still posting in html, instead of plain text email.




 Yes My Initial code was not nested at the same time of no use too,  I am
 trying to use zip() now :) :) nyways..


   st = s.split(,)

  if len(st)!= 11:
  continue
  else:
  clas = st[10].strip()

  print ix, iy, clas
  print  nf, ix, iy, clas
  break


 f1.close()
 f2.close()
 nf.close()

 I actually dont want nested loops but cant find another way to achieve
 what
 I want, But for these files I am sure that they have equal lengths, thats
 why I am taking the risk of using nested loops.


 You have that backwards.  Because you say you can assume they're the same
 length, you don't need the flexibility (and unreadability) of the nested
 approach.  The zip approach works great, and nested is unnecessary.


 . Can you suggest any

 different way to go around this problem , which could be flexible and
 non-errorneous ?




-- 
http://mail.python.org/mailman/listinfo/python-list


Reading data from 2 different files and writing to a single file

2013-01-28 Thread inshu chauhan
In the code below I am trying to read 2 files f1 and f2 , extract some data
from them and then trying to write them into a single file that is 'nf'.

import cv
f1 = open(rZ:\modules\Feature_Vectors_300.arff)
f2 = open(rZ:\modules\Feature_Vectors_300_Pclass.arff)
nf = open(rZ:\modules\trial.arff, w)


for l in f1:
sp = l.split(,)

if len(sp)!= 12:
continue
else:
ix = sp[0].strip()
iy = sp[1].strip()
print ix, iy

   for s in f2:
st = s.split(,)

if len(st)!= 11:
continue
else:
clas = st[10].strip()

 print ix, iy, clas
 print  nf, ix, iy, clas

f1.close()
f2.close()
nf.close()


I think my code is not so correct , as I am not getting desired results and
logically it follows also but I am stuck , cannot find a way around this
simple problem of writing to a same file.. Please suggest some good
pythonic way I can do it..


Thanks in Advance
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading data from 2 different files and writing to a single file

2013-01-28 Thread inshu chauhan
Yes Chris, I understand, My Original code was

for l in f1:
sp = l.split(,)

if len(sp)!= 12:
continue
else:
ix = sp[0].strip()
iy = sp[1].strip()


for s in f2:
st = s.split(,)

if len(st)!= 11:
continue
else:
clas = st[10].strip()

print ix, iy, clas
print  nf, ix, iy, clas

f1.close()
f2.close()
nf.close()

where f1 contains something like :

297, 404, , 
298, 404, , ..
299, 404, .
.  ..
295, 452, 

and f2 contains something like :

 7
. 2
2
.7

and what I want to be written in the new file i.e. nf is something like:

297 404 7
297 404 7
297 404 7
297 404 7
297 404 7
297 404 7
297 404 7
297 404 7
297 404 7
297 404 7
297 404 7
297 404 7
297 404 7
297 404 7
297 404 2
297 404 2
297 404 2
297 404 2
297 404 2

which m getting but partially correct because only last value is changing
not the first two... which should not happen.. In every loop all the three
values should change..
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading data from 2 different files and writing to a single file

2013-01-28 Thread inshu chauhan
 In that case, Dave's suggestion to read into a list and iterate over
 the list is to be strongly considered. But I'm not entirely sure what
 your goal is here. Are you trying to make the Cartesian product of the
 two files, where you have one line in the output for each possible
 pair of matching lines? That is, for each line in the first file, you
 make a line in the output for each line in the second? That's what
 your code will currently do.


No , I dont want that , actually both files have equal no of lines, I want
to read the first line of f1 , take 2 datas from it, nd then read first
line of f2, take data from it,
then print them to the same first line of new file i.e nf.


 ChrisA
 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading data from 2 different files and writing to a single file

2013-01-28 Thread inshu chauhan
Your current logic tries to scan through the first file, and for each line
that has 12 elements, scans through the entire second file.  It fails to
actually do it, because you never do a seek on the second file.


 Now it appears your requirement is entirely different.  I believe you have
 two text files each having the same number of lines.  You want to loop
 through the pair of lines (once from each file, doing some kind of
 processing and printing).  If that's the case, your nested loop is the
 wrong thing, and you can forget my caveat about nesting file reads.

 What you want is the zip() function

 for l,s in zip(f1, f2):
 #you now have one line from each file,
 #   which you can then validate and process

 Note, this assumes that when a line is bad from either file, you're
 going to also ignore the corresponding line from the other.  If you have to
 accommodate variable misses in the lining up, then your work is *much*
 harder.





 Actually these are Arff files used in Weka (Data Mining ), So they have a
certain amount of header information which is the same in both files(in
same no. of lines too )  and both files have equal lines, So when I read
basically In both files I am trying to ignore the Header information.

then it is like reading first line from f1 and first line from f2,
extracting the data I want from each file and simply write it to a third
file line by line...

What does actually Zip function do ?

Thanks and Regards
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Reading data from 2 different files and writing to a single file

2013-01-28 Thread inshu chauhan



 That's  zip  not  Zip

 Have you tried looking at the docs?  Or even typing help(zip) at the
 python interpreter prompt?

 In rough terms, zip takes one element (line) from each of the iterators,
 and creates a new list that holds tuples of those elements.  If you use it
 in this form:

  for item1, item2 in zip(iter1, iter2):

 then item1 will be the first item of iter1, and item2 will be the first
 item of iter2.  You then process them, and loop around.  It stops when
 either iterator runs out of items.

 https://duckduckgo.com/?q=**python+ziphttps://duckduckgo.com/?q=python+zip
gives me 
 http://docs.python.org/2/**library/functions.html#ziphttp://docs.python.org/2/library/functions.html#zip

 as the first link.

 This will read the entire content of both files into the list, so if they
 are more than 100meg or so, you might want to use  izip().  (In Python3.x,
  zip will do what izip does on Python 2.x)



 --
 DaveA
 --
 http://mail.python.org/**mailman/listinfo/python-listhttp://mail.python.org/mailman/listinfo/python-list




Thanks Dave, I am Sorry ,  true I dint look up for it because as per the
Suggetion by Chris, 'break' does solve my problem but I wanted to know a
little about 'zip' for I encounter any other problem, parsing these two
files.
-- 
http://mail.python.org/mailman/listinfo/python-list


using split for a string : error

2013-01-24 Thread inshu chauhan
Here I have a code which basically reads a csv file, tries to compare the
last 2 items in each line of the file.

f = open(rZ:\modules\Feature_Vectors_300_Pclass.arff)
for l in f:
sp = l.split(,)
if len(sp) != 11:
print  of, l,

else:
#print sp[9], sp[10]
if sp[9] == sp[10]:
print  Same class
else :
print Different class

f.close()

For me I think the programme is logically correct, but its giving me
results which are strange.
It is  Printing  Different Class  even when sp[9] is equal to sp[10] and
Same class when sp[9] is not equal to sp[10].  and sp[9] and sp[10] are
simple integers like 3, 3, 4 ,4.

I have a little understanding why the programme is behaving like this ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using split for a string : error

2013-01-24 Thread inshu chauhan
On Thu, Jan 24, 2013 at 11:55 AM, Tobias M. t...@tobix.eu wrote:

  Hi,

 do a print sp after the split and you might see that the strings don't
 look as you expected. There might be leading or trailing whitespaces in the
 splitted strings and in sp[10] there probably is a line break \n at the
 end.
 To remove those unwanted characters you could use the strip() function.

 So your code could be:

 if sp[9].strip() == sp[10].strip():

 print Same class
 else:
 print Different class

 At least this works for me when I tried it...

 Am 24.01.2013 11:37, schrieb inshu chauhan:

   Here I have a code which basically reads a csv file, tries to compare
 the last 2 items in each line of the file.

 f = open(rZ:\modules\Feature_Vectors_300_Pclass.arff)
 for l in f:
 sp = l.split(,)
 if len(sp) != 11:
 print  of, l,

 else:
 #print sp[9], sp[10]
 if sp[9] == sp[10]:
 print  Same class
 else :
 print Different class

 f.close()

  For me I think the programme is logically correct, but its giving me
 results which are strange.
  It is  Printing  Different Class  even when sp[9] is equal to sp[10]
 and Same class when sp[9] is not equal to sp[10].  and sp[9] and sp[10]
 are simple integers like 3, 3, 4 ,4.

  I have a little understanding why the programme is behaving like this ?


  Yeah I tried printing, there were trailing white spaces, so i used
 strip() and IT Worked !!! :)


Thank you


 --
 http://mail.python.org/mailman/listinfo/python-list


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: using split for a string : error

2013-01-24 Thread inshu chauhan
Thanks a lot people.. :).. :)



On Thu, Jan 24, 2013 at 1:10 PM, Tobias M. t...@tobix.eu wrote:

 Am 24.01.2013 13:02, schrieb Chris Angelico:

  On Thu, Jan 24, 2013 at 10:58 PM, Tobias M. t...@tobix.eu wrote:

 Chris Angelico wrote:

 I'd not consider the performance, but the correctness. If you're
 expecting them to be integers, just cast them, and specifically
 _don't_ catch ValueError. Any non-integer value will then noisily
 abort the script. (It may be worth checking for blank first, though,
 depending on the data origin.)

 Well, when I said you should catch the ValueError I didn't imply you
 should
 ignore the error and supress any error messages. Of course this depents
 on
 the use case. Maybe you want to raise another exception with a more user
 friendly error message or you might want to skip the line and just print
 a
 warning. :)

 What I'm trying to say: When I give a script/program to a user who is
 not a
 python programmer I don't want him to see an error message like
 ValueError:
 invalid literal for int() with base 10: 'abc' as this would help him in
 no
 way.

 Sure. Definitely. But for a proglet where the programmer IS the user
 (which I think is one of Python's best use-cases), that exception
 landing on the console is better than having to think ahead of time
 about what might go wrong.

 ChrisA

 Okay, I absolutely agree with that :)

 Tobias
 --
 http://mail.python.org/**mailman/listinfo/python-listhttp://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Error .. Please Help

2012-12-13 Thread inshu chauhan

 if-else doesn't define a loop, but each of the for statements do.

 You have defined a classification for 8 of the possible colors, leaving
 millions of them undefined.  If the first time through the loop you
 manage to hit one of those undefined ones, you'll have no value for
 classification.  So you get an exception.


Yes, You are right that I will hit an exception if classification doesnot
have any value.
But as for the colors, actually I myself have colored the image using these
8 colors
but still I think in some pixels I have put more than 1 color, thats why
different colors are being encountered.


 Worse, if you manage to get past that first pixel with a valid
 classification, then for remaining pixels, you'll be using the last
 'valid' classification encountered.  This is the kind of quiet failure
 that programmers dread.  Something that seems to work, but isn't even
 close.


For this I put an else clause at end but is there a better way to avoid
this kind of situation ??


 If you're going to print (to file) on each iteration through the loop
 (except 0,0,0), you need to make sure there's always a valid value.  So
 you need at least one more classification value, and an else clause to
 assign it, as ChrisA pointed out.

 Do you have a reason for treating (0,0,0) specially?  When that value is
 seen, the logic skips the print as well, since continue skips to the
 next loop iteration.


Yes , I have to skip (0,0,0), that is a kind of requirement.


 Are you really getting floating point values, or are they always going
 to be equal to an integer?  Those if/elif statements might be a problem
 if you ever need to compare to a value like (128.4, 255.0, 255.0).


I dont have values with decimal part, All values are integer.


 --

 DaveA


-- 
http://mail.python.org/mailman/listinfo/python-list


Error .. Please Help

2012-12-12 Thread inshu chauhan
In this code :

import cv
g = open(PointCloudmitClass.txt, w)
image = cv.LoadImageM(Z:/modules/intensity_01.tif,
cv.CV_LOAD_IMAGE_UNCHANGED)
print image
for y in xrange(0,image.height):
for x in xrange(0,image.width):
color = image[y,x]
if color == (0.0,0.0,0.0):
continue
else :

if color == (0.0,255.0,0.0):
classification = 1
elif color == (128.0, 0.0, 255.0):
classification = 2
elif color == (255.0,0.0,0.0):
classification = 3
elif color == (128.0, 128.0, 128.0):
classification = 4
elif color == (128.0, 64.0, 0.0):
classification = 5
elif color == (0.0, 0.0, 255.0):
classification = 6
elif color == (255.0, 0.0, 255.0):
classification = 7

print  g, x , y , color, classification


I am getting the following error..

Traceback (most recent call last):
  File Z:\modules\Get_Classification.py, line 27, in module
print  g, x , y , color, classification
NameError: name 'classification' is not defined

Its simple error of name but m not getting why it should come as I have
already defined Classification in between if-else loop ??

Thanks in Advance !!!


IC
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Index Error

2012-11-21 Thread inshu chauhan

 
  Yes i tried or also but no use .

 Explain no use.  If you mean you still fail, then what else did you
 try?  For example, did you try interchanging the two subscripts?  I've
 suspected all along that the meanings of row and column, x and y, [0]
 and [1], height and width are possibly confused.  Perhaps it'd be better
 if you used a similar terminology everywhere.


I tried using or instead of and interchanging also, still the index
error is coming.
x is no of column(300)  and y is no of rows(3000), [0] corresponds to x
i.e. column and [1]  corresponds to y i.e. row and
lastly height is y i.e row and width is column i.e. x



 Or perhaps you should simply make a class called Point, with attributes
 x and y.  And use instances of that class rather than tuples.  And start
 the function by defining  xmax and ymax, from  data.height and
 data.width (in whatever order matches the docs of that library you're
 using)


Ok

 should be added only when this condition is satisfied
 print point
 points.append(point)
 change = True
 print change


 break

 Why do you want to terminate the loop after only iteration?


snip
  The idea here is if no more point is added to the list of points i.e. all
  points surrounding the centre is zero.. I want to break the loop and go
 to
  the next point..
 
 That's not at all what the break does.  But it's probably not what you
 meant to say anyway.
 
 I think what you're saying is that you want to append at most one of the
 points from the ring.  In that case, the break is appropriate, but it'd
 be much clearer if it were inside the clause that triggers it, the place
 where you say points.append(point).  (naturally, it'd be at the end of
 that clause, following print change.)  In other words indent it to line
 up with print change.


Yes done that.. I totally agree..


 Back to an earlier comment.  I asked if N was ever bigger than x or
 bigger than y, and you said never.  But your ComputeClasses will have
 such a case the very first time around, when cx==0, cy==0, and
 ring_number == 1.


I doubt this , M confused..


 Have you actually tested a trivial nested loop:

for cy in xrange(0, data.height):
 for cx in xrange(0, data.width):
 point = data[cy, cx]

 to see whether it blows up.  And if it does, whether reversing cy and cx
 will change it?


Yes , I tested this. Its working fine, reversing cy and cx is not correct



 Your comment in the line:
  if  dist  radius :and rings should be added
only when this condition is satisfied

 is confusing to me.  How can you make the call to GenerateRing() after
 this test, when this test is measuring something about one of the values
 returned by GenerateRing ?


Actually , this is one of my condition.. but i think It means that when
dist  is greater than radius
more points will not be added to the list , So the loop should stop here,
and next centre should be taken, creating second list and so on..


 I must confess I have no idea what data represents.  When you're doing
 rings, you use deltas on the cx and cy values.  But when you're
 computing radius, you use the 3d coordinates returned by  data[cx, cy].
  So is data some kind of transformation, like a projection from a 3d
 object into a plane ?


If I say data is a yml image file.. does it makes sense ? Nyways I am
treating it as matrix with values.
Yes you can say that it is a projection of 3D objects onto a 2D plane..
which I am finding hard to work with !
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Index Error

2012-11-21 Thread inshu chauhan
 
  def GenerateRing(x,y, N): Generates square rings around a point in data
 which has 300 columns(x) and 3000
  rows(y)
  indices = []
  for i in xrange(-N, N):
  indices.append((x+i, y-N))
  indices.append((x+N, y+i))
  indices.append((x-i, y+N))
  indices.append((x-N, y-i))
  return indices

 No, this creates a one dimensional list with 2N elements of where each
 element is a two item tuple.

 Yes, in programme it returns a list of tuples but pysically it is creating
a ring .


  I need help in this part as I am
  unable to device a method in which if the points are out of index,it
 should stop and
  if idx[0] = 300 and idx[1] = 3000:
 go to next centre and start generating
  rings from there.. and again if the index is out of range .. this should
 repeat
  continue
  else :
  point = data[idx[0], idx[1]]

 You can use a few different methods. This is just one example.

 for idx, temp_point in enumerate(new_indices):
 try:
 temp_point[0]
 temp_point[1]
 except Exception: #Should be IndexError I think.
 print 'idx: {0}\ntemp_point:{1}'.format(idx, temp_point)
 # Possibly add a break or exit so you do not have to
 # keep going once you hit a failure.
 point = data[temp_point[0], temp_point[1]]



Thank you for the suggestion.


 What is `data`? I have not seen any built-in structure that takes
 a tuple in this manner...unless it is a dictionary. Or from numpy.
 Given my lack of knowledge of what `data`, it could be the
 problem is there. That is one reason I accessed `temp_point[0]` and
 `temp_point[1]` separately.


Data is an image.



 
  Traceback (most recent call last):
File Z:/modules/Classify.py, line 73, in module
  ComputeClasses(data)
File Z:/modules/Classify.py, line 49, in ComputeClasses
  point = data[idx[0], idx[1]]
  error: index is out of range
 

 Is that the actual error? If so, then the problem is not `idx` or
 `temp_point` but instead `data`. If it is not the exact error, please
 copy and paste the error message *exactly* as given.



Sorry but this is the actual error .
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Index Error

2012-11-21 Thread inshu chauhan
I guess I have to use try and except as Chris suggested, this isn't working.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Error

2012-11-20 Thread inshu chauhan
thanx ..I understand the problem now..


On Wed, Nov 14, 2012 at 7:48 PM, MRAB pyt...@mrabarnett.plus.com wrote:

 On 2012-11-14 15:18, inshu chauhan wrote:


 for this code m getting this error :

 CODE :
 def ComputeClasses(data):
  radius = .5
  points = []
  for cy in xrange(0, data.height):
  for cx in xrange(0, data.width):
  if data[cy,cx] != (0.0,0.0,0.0):
  centre = data[cy, cx]
  points.append(centre)


  Look at this line:

   change = True

  It's indented the same as the preceding 'if' statement, which means
 that it's executed even if the body of the 'if' statement wasn't
 executed and it hasn't assigned to 'centre'.

 So 'change' has been set to True, the 'while' loop is entered, and
 subsequently an attempt is made to get 'centre', which hasn't been set.


   while change:

  for ring_number in xrange(1, 1000):
  change = False
  new_indices = GenerateRing(cx, cy, ring_number)


  for idx in new_indices:
  point = data[idx[0], idx[1]]

  if point == (0.0, 0.0, 0.0 ):
continue
  else:
  dist = distance(centre, point)
  if  dist  radius :
  print point
  points.append(point)
  change = True
  print change


  The indentation of this line looks wrong to me:

   break



But If I change the indentation of break towards inside, its going into
infinite loop.. ???


  It'll affect the 'for cx' loop at the end of its first iteration, every
 time.


 ERROR :
 Traceback (most recent call last):
File Z:\modules\classification2.**py, line 74, in module
  ComputeClasses(data)
File Z:\modules\classification2.**py, line 56, in ComputeClasses
  dist = distance(centre, point)
 UnboundLocalError: local variable 'centre' referenced before assignment

 And i am unable to understand .. WHY ?


 --
 http://mail.python.org/**mailman/listinfo/python-listhttp://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Error

2012-11-20 Thread inshu chauhan
I did the following changes in this part of my programme.. now the
refereence error is removed but its showing me another error :

def ComputeClasses(data):
radius = .5
points = []
for cy in xrange(0, data.height):
for cx in xrange(0, data.width):

if data[cy,cx] == (0.0,0.0,0.0):
continue
else :
centre = data[cy, cx]
print centre
points.append(centre)


change = True

while change:

for ring_number in xrange(1, 1000):
change = False
new_indices = GenerateRing(cx, cy, ring_number)

for idx in new_indices:
point = data[idx[0], idx[1]]

if point == (0.0, 0.0, 0.0 ):
  continue
else:

dist = distance(centre, point)
if  dist  radius :
print point
points.append(point)
change = True
print change


break


print points


ERROR :

Traceback (most recent call last):
  File Z:/modules/classification1.py, line 71, in module
ComputeClasses(data)
  File Z:/modules/classification1.py, line 47, in ComputeClasses
point = data[idx[0], idx[1]]
error: index is out of range

What is meant by this statement ' Index out of range ' ? Does it mean that
my range 1, 1000 is exceeded ??
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Error

2012-11-20 Thread inshu chauhan
Yes you are rightI figured that out after posting to python list.. actually
my index is reaching the last point.. and my prog is not doing what I
want.. I am wondering why it is reaching the last point in my list .. its
never stopping in between ???


On Tue, Nov 20, 2012 at 2:01 PM, Dave Angel d...@davea.name wrote:

 On 11/20/2012 07:31 AM, inshu chauhan wrote:
  I did the following changes in this part of my programme.. now the
  refereence error is removed but its showing me another error :
 
  def ComputeClasses(data):
  radius = .5
  points = []
  for cy in xrange(0, data.height):
  for cx in xrange(0, data.width):
 
  if data[cy,cx] == (0.0,0.0,0.0):
  continue
  else :
  centre = data[cy, cx]
  print centre
  points.append(centre)
 
 
  change = True
 
  while change:
 
  for ring_number in xrange(1, 1000):
  change = False
  new_indices = GenerateRing(cx, cy, ring_number)
 
  for idx in new_indices:
  point = data[idx[0], idx[1]]
 
  if point == (0.0, 0.0, 0.0 ):
continue
  else:
 
  dist = distance(centre, point)
  if  dist  radius :
  print point
  points.append(point)
  change = True
  print change
 
 
  break
 
 
  print points
 
 
  ERROR :
 
  Traceback (most recent call last):
File Z:/modules/classification1.py, line 71, in module
  ComputeClasses(data)
File Z:/modules/classification1.py, line 47, in ComputeClasses
  point = data[idx[0], idx[1]]
  error: index is out of range
 
  What is meant by this statement ' Index out of range ' ? Does it mean
 that
  my range 1, 1000 is exceeded ??
 
 

 When you're using custom classes that mimic the standard ones, the error
 can mean most anything.  But assuming the design was to keep as close as
 possible, it simply means that you're subscripting a list with an index
 that's too large or too small.  So if idx is a list that has only one
 element, element number zero, then idx[1] would be out of range.  On the
 same line, if data is acting kind of like a two-dimensional list, then
 it has limits on each dimension, and either idx[0] is too big/small for
 the first dimension, or idx[1] is too big or small for the second.

 First thing is to figure out which part of this expression is causing
 the exception.  So do a separate pair of assignments,
 dummy0 = idx[0]
 dummy1 = idx[1]

 and then  point = data[dummy0, dummy1]

 Incidentally, if idx is a tuple or a list, of exactly two items, then
 you could just say
point = data[*idx]

 Anyway, if that still doesn't make things clear, then print dummy0 and
 dummy1 before the point= line.  That way you can see the last value, the
 one it dies on, just before the stack trace.  Naturally, you could also
 print the size attributes of the data item as well.


 --

 DaveA


-- 
http://mail.python.org/mailman/listinfo/python-list


Index Error

2012-11-20 Thread inshu chauhan
def distance(c, p):
dist = sqrt(
((c[0]-p[0])**2) +
((c[1]-p[1])**2) +
((c[2]-p[2])**2)
)
return dist


def GenerateRing(x,y, N): Generates square rings around a point in data
which has 300 columns(x) and 3000 rows(y)
indices = []
for i in xrange(-N, N):
indices.append((x+i, y-N))
indices.append((x+N, y+i))
indices.append((x-i, y+N))
indices.append((x-N, y-i))
return indices


def ComputeClasses(data):
radius = .5
points = []
for cy in xrange(0, data.height):
for cx in xrange(0, data.width):

if data[cy,cx] == (0.0,0.0,0.0):
continue
else :
centre = data[cy, cx]
points.append(centre)


change = True

while change:

for ring_number in xrange(1, 100):
change = False
new_indices = GenerateRing(cx, cy, ring_number)
print new_indices
for idx in new_indices:
 I need help in this part as I am unable to device a method in
which if the
points are out of index,it should stop and
if idx[0] = 300 and idx[1] = 3000:   go
to next centre and start generating rings from there.. and again if the
index is out of range .. this should repeat
continue
else :
point = data[idx[0], idx[1]]
if point == (0.0, 0.0, 0.0 ):
print point
continue
else:
dist = distance(centre, point)
print dist
if  dist  radius :   and rings
should be added only when this condition is satisfied
print point
points.append(point)
change = True
print change


break


print points


ERROR now :

data loaded
[(296, 403), (298, 403), (298, 405), (296, 405), (297, 403), (298, 404),
(297, 405), (296, 404)] ... I am printing Indices to know what index it
dies out..

Traceback (most recent call last):
  File Z:/modules/Classify.py, line 73, in module
ComputeClasses(data)
  File Z:/modules/Classify.py, line 49, in ComputeClasses
point = data[idx[0], idx[1]]
error: index is out of range
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Index Error

2012-11-20 Thread inshu chauhan
I am using python 2.7.3 , so can it be done in that ?


On Tue, Nov 20, 2012 at 2:48 PM, Chris Angelico ros...@gmail.com wrote:

 On Wed, Nov 21, 2012 at 12:43 AM, inshu chauhan insidesh...@gmail.com
 wrote:
  I need help in this part as I am unable to device a method in which if
 the
  points are out of index,it should stop.
 
  Traceback (most recent call last):
File Z:/modules/Classify.py, line 73, in module
  ComputeClasses(data)
File Z:/modules/Classify.py, line 49, in ComputeClasses
  point = data[idx[0], idx[1]]
  error: index is out of range
 

 When Python throws an exception, you can catch it and handle it as you
 please. In this instance, it seems to me you want to break out of one
 loop (if I've read your comments correctly), so just bracket that loop
 with try... except. Start here:

 http://docs.python.org/3.3/tutorial/errors.html

 ChrisA
 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Index Error

2012-11-20 Thread inshu chauhan
On Tue, Nov 20, 2012 at 3:00 PM, Chris Angelico ros...@gmail.com wrote:

 On Wed, Nov 21, 2012 at 12:57 AM, inshu chauhan insidesh...@gmail.com
 wrote:
  I am using python 2.7.3 , so can it be done in that ?

 (Please don't top-post; just delete the couple of blank lines that
 gmail oh so kindly provides, and type your response at the bottom. You
 may also want to consider trimming the quoted text to just what you
 actually need.)

 Yes, it certainly can. Go to the docs page I linked to, then up the
 top left you'll see this:

  Python » 3.3.0  Documentation

 with the 3.3.0 part a drop-down list. Drop it down and select 2.7,
 and you'll see the equivalent page for version 2.7.3.

 Have fun!

 ChrisA
 --
 http://mail.python.org/mailman/listinfo/python-list



Ok.. Thanks a ton ...
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Index Error

2012-11-20 Thread inshu chauhan
 def GenerateRing(x,y, N): Generates square rings around a point in data
  which has 300 columns(x) and 3000 rows(y)
  indices = []
  for i in xrange(-N, N):
  indices.append((x+i, y-N))
  indices.append((x+N, y+i))
  indices.append((x-i, y+N))
  indices.append((x-N, y-i))
  return indices
 
 Is it possible that N is greater than either x or y ?  Are negative
 subscripts permissible?

 You should consider doing the clipping logic in this function, perhaps
 by passing xlimit and ylimit in as arguments.



Yes N cannot be greater than x and y and it cant be negative too...

  I need help in this part as I am unable to device a method in
 which if the
 points are out of index,it should stop and
 if idx[0] = 300 and idx[1] = 3000:   go
 to next centre and start generating rings from there.. and again if the
 index is out of range .. this should repeat

This is where you're trying to clip the values that may be outside of
 the total matrix.


Yes I am trying to clip the values here but its not working actually , I
mean not at all working


 You do not want and in that expression.  The way you've coded it,
 it'll only skip items in which both indices are out of range.  Change it to
   if idx[0] = data.width or idx[1] = data.height:

 and depending on your answer to my earlier query, you may want to also
 check if either subscript is negative.

  continue
  else :
  point = data[idx[0], idx[1]]
  if point == (0.0, 0.0, 0.0 ):
  print point
  continue
  else:
  dist = distance(centre, point)
  print dist
  if  dist  radius :   and
 rings
  should be added only when this condition is satisfied
  print point
  points.append(point)
  change = True
  print change
 
 
  break

 Why do you want to terminate the loop after only iteration?


I dint get your question ??
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Index Error

2012-11-20 Thread inshu chauhan


 So did you read the following paragraphs?  You should not be using and
 in that expression.



 Yes i tried or also but no use .

 
  You do not want and in that expression.  The way you've coded it,
  it'll only skip items in which both indices are out of range.  Change
 it to
if idx[0] = data.width or idx[1] = data.height:
 
  and depending on your answer to my earlier query, you may want to also
  check if either subscript is negative.
 
  continue
  else :
  point = data[idx[0], idx[1]]
  if point == (0.0, 0.0, 0.0 ):
  print point
  continue
  else:
  dist = distance(centre, point)
  print dist
  if  dist  radius :   and
  rings
  should be added only when this condition is satisfied
  print point
  points.append(point)
  change = True
  print change
 
 
  break
 
  Why do you want to terminate the loop after only iteration?
 
 
  I dint get your question ??
 

 You have a break there.  What's it for?  It'll make sure you only
 process one of the idx values from new_indices.  i doubt that's what you
 intended.

The idea here is if no more point is added to the list of points i.e. all
points surrounding the centre is zero.. I want to break the loop and go to
the next point..
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Index Error

2012-11-20 Thread inshu chauhan
The catch to the approach suggested by Chris is that you DON'T want to
 break out of the whole loop when one value is out of range.  You only
 want to skip that one point.


 Yes I want to skip only that one point (centre)  but try and expect seems
to be the last option.. I would like to try other ways first..
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Index Error

2012-11-20 Thread inshu chauhan
I dont want error actually to come.  With try nd except , the error will
come but without affecting my prog...
-- 
http://mail.python.org/mailman/listinfo/python-list


Error

2012-11-14 Thread inshu chauhan
for this code m getting this error :

CODE :
def ComputeClasses(data):
radius = .5
points = []
for cy in xrange(0, data.height):
for cx in xrange(0, data.width):
if data[cy,cx] != (0.0,0.0,0.0):
centre = data[cy, cx]
points.append(centre)


change = True

while change:

for ring_number in xrange(1, 1000):
change = False
new_indices = GenerateRing(cx, cy, ring_number)


for idx in new_indices:
point = data[idx[0], idx[1]]

if point == (0.0, 0.0, 0.0 ):
  continue
else:
dist = distance(centre, point)
if  dist  radius :
print point
points.append(point)
change = True
print change


break


ERROR :
Traceback (most recent call last):
  File Z:\modules\classification2.py, line 74, in module
ComputeClasses(data)
  File Z:\modules\classification2.py, line 56, in ComputeClasses
dist = distance(centre, point)
UnboundLocalError: local variable 'centre' referenced before assignment

And i am unable to understand .. WHY ?
-- 
http://mail.python.org/mailman/listinfo/python-list


Fwd: error

2012-11-12 Thread inshu chauhan
No answers for my question ?? :O
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: error

2012-11-09 Thread inshu chauhan
http://www.ymlgroup.com/obscurehiddenlocation/downloads site, and
 imported  it using  from notzanzibar import DataType.


No i haven't downloaded it.. and this site is not opening...


 Then you'd have instantiated it in some code like
   data = DataType(filename)

 and then the type of data would be notzanzibar.DataType  and the docs
 would be probably available somewhere on www.invalid.com/docs


This site is also blocked here.


 The only new guess:

 A 3D image would presumably have 3 subscripts, and you're only supplying
 two.


Yes a 3D image has 3 subscripts but m trying to access all three through
using only  2 subsscripts i.e X and Y



 If you want help, be explicit:

 1) what version of CPython are you using, and on what OS?


I am using 2.7.3 on windows 7

2) what web site did you download some extra library from ?


The only extra libary i am using is Opencv , downloaded from
http://sourceforge.net/projects/opencvlibrary/



 3) what import statement did you use ?


import cv

4) How are all those global variables initialized ?

see attached file

 5) What command did you use to start the script ?  Did you run it from
 command.com, or from some IDE ?


Yes I am running it through IDLE GUI


 5) Exactly what error message did you get, including the traceback ?


 Traceback (most recent call last):
  File Z:\modules\Masking_an_image_dynamically.py, line 155, in module
AccessPixels(data)
  File Z:\modules\.py, line 147, in AccessPixels
CreateMask(data, x, y)
  File Z:\modules\new_classification.py, line 110, in CreateMask
point = data[iy, ix ]
error: index is out of range

The line numbers here and the file attached may be different because I have
removed a lot of print statements which I was using to debug the error..



 6) What have you done to try to figure out your own error?


I have trying print out variables and Indices at each step..


Zero Piraeus : Where are you ?


Masking_an_image_dynamically.py
Description: Binary data
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: error

2012-11-09 Thread inshu chauhan
I attached a wrong file...Right file is attached here


On Fri, Nov 9, 2012 at 11:53 AM, inshu chauhan insidesh...@gmail.comwrote:





 http://www.ymlgroup.com/obscurehiddenlocation/downloads site, and
 imported  it using  from notzanzibar import DataType.


 No i haven't downloaded it.. and this site is not opening...


 Then you'd have instantiated it in some code like
   data = DataType(filename)

 and then the type of data would be notzanzibar.DataType  and the docs
 would be probably available somewhere on www.invalid.com/docs


 This site is also blocked here.


 The only new guess:

 A 3D image would presumably have 3 subscripts, and you're only supplying
 two.


 Yes a 3D image has 3 subscripts but m trying to access all three through
 using only  2 subsscripts i.e X and Y



 If you want help, be explicit:

 1) what version of CPython are you using, and on what OS?


 I am using 2.7.3 on windows 7

 2) what web site did you download some extra library from ?


 The only extra libary i am using is Opencv , downloaded from
 http://sourceforge.net/projects/opencvlibrary/



 3) what import statement did you use ?


 import cv

 4) How are all those global variables initialized ?

 see attached file

 5) What command did you use to start the script ?  Did you run it from
 command.com, or from some IDE ?


 Yes I am running it through IDLE GUI


 5) Exactly what error message did you get, including the traceback ?


  Traceback (most recent call last):
   File Z:\modules\Masking_an_image_dynamically.py, line 155, in module
 AccessPixels(data)
   File Z:\modules\.py, line 147, in AccessPixels
 CreateMask(data, x, y)
   File Z:\modules\new_classification.py, line 110, in CreateMask
 point = data[iy, ix ]

 error: index is out of range

 The line numbers here and the file attached may be different because I
 have removed a lot of print statements which I was using to debug the
 error..



 6) What have you done to try to figure out your own error?


 I have trying print out variables and Indices at each step..


 Zero Piraeus : Where are you ?





Masking_an_image_dynamically.py
Description: Binary data
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: error

2012-11-09 Thread inshu chauhan
Please Ignore the above two files attached,,, See this one


On Fri, Nov 9, 2012 at 12:47 PM, inshu chauhan insidesh...@gmail.comwrote:

 I attached a wrong file...Right file is attached here


 On Fri, Nov 9, 2012 at 11:53 AM, inshu chauhan insidesh...@gmail.comwrote:





 http://www.ymlgroup.com/obscurehiddenlocation/downloads site, and
 imported  it using  from notzanzibar import DataType.


 No i haven't downloaded it.. and this site is not opening...


 Then you'd have instantiated it in some code like
   data = DataType(filename)

 and then the type of data would be notzanzibar.DataType  and the docs
 would be probably available somewhere on www.invalid.com/docs


 This site is also blocked here.


 The only new guess:

 A 3D image would presumably have 3 subscripts, and you're only supplying
 two.


 Yes a 3D image has 3 subscripts but m trying to access all three through
 using only  2 subsscripts i.e X and Y



 If you want help, be explicit:

 1) what version of CPython are you using, and on what OS?


 I am using 2.7.3 on windows 7

 2) what web site did you download some extra library from ?


 The only extra libary i am using is Opencv , downloaded from
 http://sourceforge.net/projects/opencvlibrary/



 3) what import statement did you use ?


 import cv

 4) How are all those global variables initialized ?

 see attached file

 5) What command did you use to start the script ?  Did you run it from
 command.com, or from some IDE ?


 Yes I am running it through IDLE GUI


 5) Exactly what error message did you get, including the traceback ?


  Traceback (most recent call last):
   File Z:\modules\Masking_an_image_dynamically.py, line 155, in module
 AccessPixels(data)
   File Z:\modules\.py, line 147, in AccessPixels
 CreateMask(data, x, y)
   File Z:\modules\new_classification.py, line 110, in CreateMask
 point = data[iy, ix ]

 error: index is out of range

 The line numbers here and the file attached may be different because I
 have removed a lot of print statements which I was using to debug the
 error..



 6) What have you done to try to figure out your own error?


 I have trying print out variables and Indices at each step..


 Zero Piraeus : Where are you ?






Masking_an_image_dynamically.py
Description: Binary data
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: error

2012-11-09 Thread inshu chauhan
Actually this one.. and its the last..


On Fri, Nov 9, 2012 at 2:59 PM, inshu chauhan insidesh...@gmail.com wrote:

 Please Ignore the above two files attached,,, See this one


 On Fri, Nov 9, 2012 at 12:47 PM, inshu chauhan insidesh...@gmail.comwrote:

 I attached a wrong file...Right file is attached here


 On Fri, Nov 9, 2012 at 11:53 AM, inshu chauhan insidesh...@gmail.comwrote:





 http://www.ymlgroup.com/obscurehiddenlocation/downloads site, and
 imported  it using  from notzanzibar import DataType.


 No i haven't downloaded it.. and this site is not opening...


 Then you'd have instantiated it in some code like
   data = DataType(filename)

 and then the type of data would be notzanzibar.DataType  and the docs
 would be probably available somewhere on www.invalid.com/docs


 This site is also blocked here.


 The only new guess:

 A 3D image would presumably have 3 subscripts, and you're only supplying
 two.


 Yes a 3D image has 3 subscripts but m trying to access all three through
 using only  2 subsscripts i.e X and Y



 If you want help, be explicit:

 1) what version of CPython are you using, and on what OS?


 I am using 2.7.3 on windows 7

 2) what web site did you download some extra library from ?


 The only extra libary i am using is Opencv , downloaded from
 http://sourceforge.net/projects/opencvlibrary/



 3) what import statement did you use ?


 import cv

 4) How are all those global variables initialized ?

 see attached file

 5) What command did you use to start the script ?  Did you run it from
 command.com, or from some IDE ?


 Yes I am running it through IDLE GUI


 5) Exactly what error message did you get, including the traceback ?


  Traceback (most recent call last):
   File Z:\modules\Masking_an_image_dynamically.py, line 155, in
 module
 AccessPixels(data)
   File Z:\modules\.py, line 147, in AccessPixels
 CreateMask(data, x, y)
   File Z:\modules\new_classification.py, line 110, in CreateMask
 point = data[iy, ix ]

 error: index is out of range

 The line numbers here and the file attached may be different because I
 have removed a lot of print statements which I was using to debug the
 error..



 6) What have you done to try to figure out your own error?


 I have trying print out variables and Indices at each step..


 Zero Piraeus : Where are you ?







Masking_an_image_dynamically.py
Description: Binary data
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: error

2012-11-08 Thread inshu chauhan
Actually data is neither a zanzibar nor a class nor a list.. its a yml
image. with pixels
I am trying to access pixels of a 3D image through this programme..





On Tue, Nov 6, 2012 at 8:59 PM, woooee woo...@gmail.com wrote:

 From this line, data appears to be a class
 if 0  ix  data.width and 0  iy  data.height:
 From this line, data appears to be a list, although a two
 dimensional list would be accessed as data[ix][iy]
 point = data[ix, iy]

 Also, returning a list from a function is a matter of preference.
 Some people argue that it should be returned to make it obvious.  If
 you do not know the difference between what is mutable and what is not
 mutable, then return everything until you do.
 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


error

2012-11-06 Thread inshu chauhan
I am getting this error while running my programme.. :

error: index is out of range

 I cannot find a valid reason for it in my prog can somebody suggest what
may be possible reasons for this error..

The part of the code is :

def addpoints (data, points, ix, iy): # makes a list of relevant points
if 0  ix  data.width and 0  iy  data.height:
point = data[ix, iy]
if point != (0.0, 0.0, 0.0):
points.append(point)
return points

for dx in xrange(-mask_size2, mask_size2 + 1):
for dy in xrange(-mask_size2, mask_size2 + 1):
ix, iy = x + dx, y + dy
addpoints(data, points, ix , iy )
-- 
http://mail.python.org/mailman/listinfo/python-list


Difference between range and xrange ??

2012-11-05 Thread inshu chauhan
what is the difference between range and xrange.. both seem to work the
same. ? And which should be used where and in what situations.. ??
-- 
http://mail.python.org/mailman/listinfo/python-list


python and Open cv

2012-11-01 Thread inshu chauhan
How to load a yml file in python and work with it ??

I used : import cv
data = cv.Load(Z:/data/xyz_0_
300.yml)

But when I print data.. it just gives the detail of the image like number
of rows and columns etc
I want read what is there in the pixel of the image..

I tried to use the following code .. but it gives me only the pixel values
not the information contained in pixel ??

def AccessPixels(img):
for y in range(0, img.height):
for x in range(0, img.width):
cv.Get2D(img, y, x) # Slow get pixel value.
cv.Set2D(img, y, x, (0, 0, 0, 0)) # Slow set pixel value.


can somebody help.. thanx in advance !!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Fwd: python and Open cv

2012-11-01 Thread inshu chauhan
-- Forwarded message --
From: inshu chauhan insidesh...@gmail.com
Date: Thu, Nov 1, 2012 at 1:26 PM
Subject: Re: python and Open cv
To: Mark Lawrence breamore...@yahoo.co.uk


I am sorry.. but I need to know what are the rules and what about gmail ??
Many people are using gmail to mail to the list.


On Thu, Nov 1, 2012 at 12:15 PM, Mark Lawrence breamore...@yahoo.co.ukwrote:

 On 01/11/2012 08:55, inshu chauhan wrote:

 How to load a yml file in python and work with it ??

 I used : import cv
  data = cv.Load(Z:/data/xyz_0_
 300.yml)

 But when I print data.. it just gives the detail of the image like number
 of rows and columns etc
 I want read what is there in the pixel of the image..

 I tried to use the following code .. but it gives me only the pixel values
 not the information contained in pixel ??

 def AccessPixels(img):
  for y in range(0, img.height):
  for x in range(0, img.width):
  cv.Get2D(img, y, x) # Slow get pixel value.
  cv.Set2D(img, y, x, (0, 0, 0, 0)) # Slow set pixel value.


 can somebody help.. thanx in advance !!!


 I think the subject should be changed to Obnoxious postings from Google
 Groups, given this is the same question from the same person within 24
 hours but a different subject line, and from a gmail address.


 --
 Cheers.

 Mark Lawrence.

 --
 http://mail.python.org/**mailman/listinfo/python-listhttp://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


pythonic way

2012-11-01 Thread inshu chauhan
 what is the most pythonic way to do this :

   if 0  ix  10 and 0  iy  10 ???
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: python and Open cv

2012-11-01 Thread inshu chauhan
On Thu, Nov 1, 2012 at 7:02 PM, Paul Rudin paul.nos...@rudin.co.uk wrote:

 Zero Piraeus sche...@gmail.com writes:

  There aren't any rules about gmail (except the unwritten rule that to
  be a real geek you need to use a mail client that takes a whole
  weekend to configure, and another three years to properly understand).



:D :D :D



 Ha! 3 years? I've been using gnus for nearly 20 years and I still don't
 understand it!
 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: pythonic way

2012-11-01 Thread inshu chauhan
OK ..I got it..

On Thu, Nov 1, 2012 at 5:54 PM, Zero Piraeus sche...@gmail.com wrote:

 :

 On 1 November 2012 11:32, inshu chauhan insidesh...@gmail.com wrote:
   what is the most pythonic way to do this :
 
 if 0  ix  10 and 0  iy  10 ???

 As everyone else has said, it's perfectly pythonic once you stick the
 colon on the end. You might find it more instantly readable with some
 extra parentheses:

 if (0  ix  10) and (0  iy  10):
 # do something

 ... but that's really just down to taste.

  -[]z.
 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


working with yml files in python and opencv

2012-10-31 Thread inshu chauhan
How to load a yml file in python and work with it ??

I used : import cv
data = cv.Load(Z:/data/xyz_0_300.yml)

But when I print data.. it just gives the detail of the image like number
of rows and columns etc
I want read what is there in the pixel of the image.. can somebody help..
thanx in advance !!!
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can we append a list with another list in Python ?

2012-10-25 Thread inshu chauhan
Actually what I wanted to do is :

suppose there are 3 lists :

like for xample :

clist = [] an empty list , alist = [1,2,3], blist = [4,5,6]

and the I want the result is clist = [ [1,2,3], [4,5,6] ..]

then I want to read all the lists in clist one by one

like :

 for item in clist:
 print item

which should print:

[1,2,3]
[4,5,6]




On Tue, Oct 23, 2012 at 10:10 PM, inshu chauhan insidesh...@gmail.comwrote:

 ok I got it guys...


 On Tue, Oct 23, 2012 at 10:08 PM, Joshua Landau 
 joshua.landau...@gmail.com wrote:

  On 23 October 2012 21:06, Joshua Landau joshua.landau...@gmail.comwrote:

  On 23 October 2012 21:03, Joshua Landau joshua.landau...@gmail.comwrote:

 On 23 October 2012 12:07, Jean-Michel Pichavant jeanmic...@sequans.com
  wrote:

 - Original Message -

  Thankyou.. but my problem is different than simply joining 2 lists
  and it is done now :)


 A lot of people though you were asking for joining lists, you
 description was misleading.

 I'll take a guess: you want to flatten a list of list.
 Nested list comprehensions can do the trick.

 aList =[[1,5], [2,'a']]
 [item for sublist in aList for item in sublist]

 ...
 [1, 5, 2, 'a']

 I find it rather difficult to read though.


 We have a library function for this, in the one-and-only itertools.

  listoflists = [list(range(x, 2*x)) for x in range(5)]
  listoflists
 [[], [1], [2, 3], [3, 4, 5], [4, 5, 6, 7]]
  from itertools import chain
   list(chain.from_iterable(listoflists))
 [1, 2, 3, 3, 4, 5, 4, 5, 6, 7]


 It does exactly what it says... fast and easy-to-read.


 Note that I think what he really wanted is to go from

 a, b, c = [list(x) for x in (range(10), range(11, 20), range(21, 30))]

 to

 list(range(30))


 UNDO! UNDO! UNDO!

 I *meant *to say:

  Note that I think what he really wanted is to go from

 a, b, c = [list(x) for x in (range(10), range(11, 20), range(21, 30))]

 to

 [a, b, c]



-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can we append a list with another list in Python ?

2012-10-25 Thread inshu chauhan
  clist = alist, blist

This may work i guess becuase i want list within list [ [1,2,3] , [4,5,6]]



 or if the clist initial contents were relevant, perhaps you mean

  clist += alist, blist


But not this because it will simply concatenate the  list like
[1,2,3,4,5,6] .. I dont want this actaully...




 --

 DaveA


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can we append a list with another list in Python ?

2012-10-25 Thread inshu chauhan

  or if the clist initial contents were relevant, perhaps you mean
 
  clist += alist, blist
 
 
  But not this because it will simply concatenate the  list like
  [1,2,3,4,5,6] .. I dont want this actaully...
 
 

 No, it won't.  Try it to see


Ok but it should be clist + = [alist, blist ] 



 --

 DaveA

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can we append a list with another list in Python ?

2012-10-25 Thread inshu chauhan
 Is this what you mean:

  list_1 = [i for i in range(0,5)]
  list_2 = [i for i in range(5,11)]
  for i in list_2:
 ... list_1.append(i)
 ...
  list_1
 [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]


 or would another example help you out more?


No but really sorry this is what I DONT WANT...

The output I want to have is :

[ [0, 1, 2, 3, 4 ], [5, 6, 7, 8, 9, 10] ].


 --
 Best Regards,
 David Hutto
 CEO: http://www.hitwebdevelopment.com

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can we append a list with another list in Python ?

2012-10-25 Thread inshu chauhan
Yes Dave ..You are right and my problem is solved now..  Thanx to all...

On Thu, Oct 25, 2012 at 11:55 AM, David Hutto dwightdhu...@gmail.comwrote:

 On Thu, Oct 25, 2012 at 5:51 AM, inshu chauhan insidesh...@gmail.com
 wrote:
 
 
 
  Is this what you mean:
 
 list_0 = []
 list_1 = [i for i in range(0,5)]
 list_2 = [i for i in range(5,11)]
 list_0.append(list_1)
 list_0.append(list_2)
   for i in list_2:
  ... list_1.append(i)
  ...
   list_1
  [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
 
 
  or would another example help you out more?
 
 
  No but really sorry this is what I DONT WANT...
 
  The output I want to have is :
 
  [ [0, 1, 2, 3, 4 ], [5, 6, 7, 8, 9, 10] ].

 Then, this is what you want :

 david@david-desktop:~$ python
 Python 2.7.3 (default, Aug  1 2012, 05:16:07)
 [GCC 4.6.3] on linux2
 Type help, copyright, credits or license for more information.
  list_0 = []
  list_1 = [i for i in range(0,5)]
  list_2 = [i for i in range(5,11)]
  list_0.append(list_1)
  list_0.append(list_2)
  list_0
 [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10]]
 




 --
 Best Regards,
 David Hutto
 CEO: http://www.hitwebdevelopment.com

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can we append a list with another list in Python ?

2012-10-25 Thread inshu chauhan
On Thu, Oct 25, 2012 at 12:04 PM, David Hutto dwightdhu...@gmail.comwrote:

 On Thu, Oct 25, 2012 at 5:58 AM, inshu chauhan insidesh...@gmail.com
 wrote:
  Yes Dave ..You are right and my problem is solved now..  Thanx to all...
 
 
  On Thu, Oct 25, 2012 at 11:55 AM, David Hutto dwightdhu...@gmail.com
  wrote:
 
  On Thu, Oct 25, 2012 at 5:51 AM, inshu chauhan insidesh...@gmail.com
  wrote:
  
  
  
   Is this what you mean:
  
  list_0 = []
  list_1 = [i for i in range(0,5)]
  list_2 = [i for i in range(5,11)]
  list_0.append(list_1)
  list_0.append(list_2)
for i in list_2:
   ... list_1.append(i)
   ...
list_1
   [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
  
  
   or would another example help you out more?
  
  
   No but really sorry this is what I DONT WANT...
  
   The output I want to have is :
  
   [ [0, 1, 2, 3, 4 ], [5, 6, 7, 8, 9, 10] ].
 
  Then, this is what you want :
 
  david@david-desktop:~$ python
  Python 2.7.3 (default, Aug  1 2012, 05:16:07)
  [GCC 4.6.3] on linux2
  Type help, copyright, credits or license for more information.
   list_0 = []
   list_1 = [i for i in range(0,5)]
   list_2 = [i for i in range(5,11)]
   list_0.append(list_1)
   list_0.append(list_2)
   list_0
  [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10]]

 if you have a format for the lists, there might be an easier way, or
 simpler. How are the lists coming in?

 The list are in form of

  first list like [(x,y,z) ]

 and another as [(a,b,c), (a+i, b+i, c+i).]

 --
 Best Regards,
 David Hutto
 CEO: http://www.hitwebdevelopment.com

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can we append a list with another list in Python ?

2012-10-25 Thread inshu chauhan
On Thu, Oct 25, 2012 at 12:17 PM, David Hutto dwightdhu...@gmail.comwrote:

 On Thu, Oct 25, 2012 at 6:12 AM, inshu chauhan insidesh...@gmail.com
 wrote:
 
 
  On Thu, Oct 25, 2012 at 12:04 PM, David Hutto dwightdhu...@gmail.com
  wrote:
 
  On Thu, Oct 25, 2012 at 5:58 AM, inshu chauhan insidesh...@gmail.com
  wrote:
   Yes Dave ..You are right and my problem is solved now..  Thanx to
 all...
  
  
   On Thu, Oct 25, 2012 at 11:55 AM, David Hutto dwightdhu...@gmail.com
 
   wrote:
  
   On Thu, Oct 25, 2012 at 5:51 AM, inshu chauhan 
 insidesh...@gmail.com
   wrote:
   
   
   
Is this what you mean:
   
   list_0 = []
   list_1 = [i for i in range(0,5)]
   list_2 = [i for i in range(5,11)]
   list_0.append(list_1)
   list_0.append(list_2)
 for i in list_2:
... list_1.append(i)
...
 list_1
[0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
   
   
or would another example help you out more?
   
   
No but really sorry this is what I DONT WANT...
   
The output I want to have is :
   
[ [0, 1, 2, 3, 4 ], [5, 6, 7, 8, 9, 10] ].
  
   Then, this is what you want :
  
   david@david-desktop:~$ python
   Python 2.7.3 (default, Aug  1 2012, 05:16:07)
   [GCC 4.6.3] on linux2
   Type help, copyright, credits or license for more
 information.
list_0 = []
list_1 = [i for i in range(0,5)]
list_2 = [i for i in range(5,11)]
list_0.append(list_1) list_0 = [list_1,list_2]
list_0.append(list_2)
list_0
   [[0, 1, 2, 3, 4], [5, 6, 7, 8, 9, 10]]
 
  if you have a format for the lists, there might be an easier way, or
  simpler. How are the lists coming in?
 
  The list are in form of
 
   first list like [(x,y,z) ]
 
  and another as [(a,b,c), (a+i, b+i, c+i).]
 
 This shows tuples though, not appended lists, which are immutable.

 My mistake it is :

 The list are in form of
 
   first list like [ [x,y,z] ]
 
  and another as  [  [a,b,c], [a+i, b+i, c+i] and so on .]
 --
 Best Regards,
 David Hutto
 CEO: http://www.hitwebdevelopment.com

-- 
http://mail.python.org/mailman/listinfo/python-list


classes

2012-10-24 Thread inshu chauhan
I was just trying out a programme for learning classes in python

The prog below is showing an error which it should not show :

class Bag:
def __init__(self, x):
self.data = []

def add(self, x):
self.data.append(x)
def addtwice(self, x):
 self.add(x)
 self.add(x)
y = Bag(4)
print  Adding twice of %4.2f gives  % (y.addtwice())


Error is :

Traceback (most recent call last):
  File Z:\learning Python\learn5.py, line 35, in module
print  Adding twice of %4.2f gives  % (y.addtwice())
TypeError: addtwice() takes exactly 2 arguments (1 given)

why the prog is having this error with self nd x as arguments ???
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: classes

2012-10-24 Thread inshu chauhan
I was just trying out a programme for learning classes in python

 The prog below is showing an error which it should not show :

 class Bag:
  def __init__(self, x):
  self.data = []


 You do nothing with x here. Right so x shouldnot be in the argument.

Fine

*class Bag:
  def __init__(self):
  self.data = []*


  def add(self, x):
  self.data.append(x)
  def addtwice(self, x):
   self.add(x)
   self.add(x)
 y = Bag(4)


 Create y with an argument of 4 'which is discarded in the initialiser.'
 means ??


  print  Adding twice of %4.2f gives  % (y.addtwice())


 There's no argument passed to addtwice here. ' why am I not passing y to
 addtwice here ??




 Error is :

 Traceback (most recent call last):
File Z:\learning Python\learn5.py, line 35, in module
  print  Adding twice of %4.2f gives  % (y.addtwice())
 TypeError: addtwice() takes exactly 2 arguments (1 given)


 Exactly what I'd expect to happen.  What did you expect? I am learning
 



 why the prog is having this error with self nd x as arguments ???


 What x argument?  Clearly wrong as I've pointed out above. How can i
 correct it ??





 --
 Cheers.

 Mark Lawrence.

 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: classes

2012-10-24 Thread inshu chauhan
I changed the programme to this :
class Bag:
def __init__(self):
self.data = []

def add(self, x):
self.data.append(x)
def addtwice(self, x):
 self.add(x)
 self.add(x)
 return x
y = Bag()
print y.addtwice(4)

Now its not showing any error but result is same as the number passed for
adding twice 


On Wed, Oct 24, 2012 at 2:55 PM, Dave Angel d...@davea.name wrote:

 On 10/24/2012 08:11 AM, inshu chauhan wrote:
  I was just trying out a programme for learning classes in python
 
  The prog below is showing an error which it should not show :
 
  class Bag:
  def __init__(self, x):
  self.data = []
 
  def add(self, x):
  self.data.append(x)
  def addtwice(self, x):
   self.add(x)
   self.add(x)
  y = Bag(4)
  print  Adding twice of %4.2f gives  % (y.addtwice())
 
 Perhaps you're confusing the two x parameters.  They are totally
 independent local variables, and the value of one is not in any way
 remembered for the other.  When you want to save things between
 multiple methods of an object, then store them in the object, perhaps in
 self.data  As it stands, the __init__ does not save the x at all, so the
 4 that's passed into the initializer is thrown away.

 You call addtwice(), but don't supply any value to add.  y serves as the
 self value, but you have no x value.  What value did you intend to add?

 You'll have another problem, in that addtwice() doesn't return any value
 (so it returns None).  Therefore the print isn't going to work.  Please
 separate the print from the calculations, and the problems will be lots
 easier to figure out.  The wording of the string implies it's going to
 display two values, but the only value given it is None.

  Error is :
 
  Traceback (most recent call last):
File Z:\learning Python\learn5.py, line 35, in module
  print  Adding twice of %4.2f gives  % (y.addtwice())
  TypeError: addtwice() takes exactly 2 arguments (1 given)
 
  why the prog is having this error with self nd x as arguments ???
 
 
 self and x are parameters.  You don't pass an argument for x to be bound
 to.




 --

 DaveA


-- 
http://mail.python.org/mailman/listinfo/python-list


Re: classes

2012-10-24 Thread inshu chauhan
ok.. This was an example i was trying to run from
http://docs.python.org/tutorial/classes.html   ...

On Wed, Oct 24, 2012 at 3:48 PM, Chris Angelico ros...@gmail.com wrote:

 On Thu, Oct 25, 2012 at 12:02 AM, inshu chauhan insidesh...@gmail.com
 wrote:
  I changed the programme to this :
  def addtwice(self, x):
   self.add(x)
   self.add(x)
   return x
  y = Bag()
  print y.addtwice(4)
 
  Now its not showing any error but result is same as the number passed for
  adding twice 

 Do you understand how function calls work? A function like
 y.addtwice is called with an argument of 4, and the return value
 from the function is the value of the expression.

 some_value = y.addtwice(4)
 print some_value

 Take the return value and put it in the place where the function call
 was. In this case, the return value is x, the number you passed in as
 an argument.

 What exactly do you expect addtwice to return? Should it return the
 bag object (self)? Should it return True to say that it's been added
 successfully (or False if there's an error)? Should it return the
 number of items in the bag? Should it return 0 for success and a
 nonzero error code for failure? Should it always return None, throwing
 an exception if anything goes wrong? All of these make sense, you just
 have to choose which one you want.

 (I'm handwaving away a lot of complexity here, like un/bound methods
 and member lookups. Bear with me. I'm also ignoring the fact that some
 things just aren't Pythonic. The bear isn't complaining about that, so
 nor should you.)

 ChrisA
 --
 http://mail.python.org/mailman/listinfo/python-list

-- 
http://mail.python.org/mailman/listinfo/python-list


Appending a list using list obtained from a class

2012-10-24 Thread inshu chauhan
I am having a problem,

I have a class which return me a list. I want to append another list using
this list outside the Class.
can i do it ? if yes how ?
And can i use Userlist function for this ??
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Appending a list using list obtained from a class

2012-10-24 Thread inshu chauhan
yes this i know very well.. but the problem is not so simple...

the class is returning the list , and i want to append the list outside the
class... main problem is this

I know how to append a list with a list nd stuff..

On Wed, Oct 24, 2012 at 4:41 PM, Demian Brecht demianbre...@gmail.comwrote:


 On 2012-10-24, at 7:37 AM, inshu chauhan insidesh...@gmail.com wrote:
  I am having a problem,
 
  I have a class which return me a list. I want to append another list
 using this list outside the Class.
  can i do it ? if yes how ?
  And can i use Userlist function for this ??
  --
  http://mail.python.org/mailman/listinfo/python-list


 If I understand your question correctly, have you read
 http://docs.python.org/tutorial/datastructures.html?

 list_a + list_b = list_c
 list_a.extend(list_b)

 are two methods to concatenate two lists.

 UserList is only there for backwards compatibility (see the docs:
 http://docs.python.org/release/2.3/lib/module-UserList.html)

 Demian Brecht
 @demianbrecht
 http://demianbrecht.github.com





-- 
http://mail.python.org/mailman/listinfo/python-list


Re: Appending a list using list obtained from a class

2012-10-24 Thread inshu chauhan
 the class is returning the list , and i want to append the list outside
the class... main problem is this


 What do you mean the class is returning the list? Do you mean that a
 class stores a list internally and you want to be able to mutate that list?
 Do you mean that you have a class method that returns a list? How does your
 particular use case differ from simply appending one list to another?


Yes, a Class method returns a list. I am trying to append this in main() to
make another list.
But the list i am getting after appending i showing addresses like this
'__main__.Point object at 0x0254FAB0' but if i print the same in the same
loop its showing me numbers which i want. Why I dont know ??





  I know how to append a list with a list nd stuff..

 Without further clarification, this seems to be exactly what you're asking
 how to do.. yeah may i make misunderstanding


 Demian Brecht
 @demianbrecht
 http://demianbrecht.github.com





-- 
http://mail.python.org/mailman/listinfo/python-list


Fwd: can we append a list with another list in Python ?

2012-10-23 Thread inshu chauhan
-- Forwarded message --
From: Zero Piraeus sche...@gmail.com
Date: Tue, Oct 23, 2012 at 11:14 AM
Subject: Re: can we append a list with another list in Python ?
To: inshu chauhan insidesh...@gmail.com


:

On 23 October 2012 05:01, inshu chauhan insidesh...@gmail.com wrote:
 this is because this makes a single list out of 2 lists.. but I want to
retain both lists as lists within list... I hope u got it ??

 my problem is for example :

 x = [1,2,3]
 y = [10, 20, 30]

 I want the output to be :

 [[1,2,3], [10, 20, 30]] .. a list within list

 then i want to process each list in the big list using another function


For the example above, you'd just do:

 x = [1, 2, 3]
 y = [10, 20, 30]
 z = [x, y]
 z
[[1, 2, 3], [10, 20, 30]]

... but presumably there'll be more than two, and you're creating them
in some kind of loop? In that case, append is fine. For example:

 result = []
 for word in squee, kapow, vroom:
... seq = list(word)  # or whatever you're doing to create the list
... result.append(seq)
...
 result
[['s', 'q', 'u', 'e', 'e'], ['k', 'a', 'p', 'o', 'w'], ['v', 'r', 'o',
'o', 'm']]

By the way - you only replied to me. Do you mind if I forward this
back to the list?

 -[]z.
-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can we append a list with another list in Python ?

2012-10-23 Thread inshu chauhan
Thankyou.. but my problem is different than simply joining 2 lists and it
is done now :)

On Tue, Oct 23, 2012 at 11:38 AM, Joshua Landau
joshua.landau...@gmail.comwrote:

 On 23/10/2012, inshu chauhan insidesh...@gmail.com wrote:
  can we append a list with another list in Python ? using the normal
 routine
  syntax but with a for loop ??

 I assume you want to join two lists.

 You are corrrect that we can do:

  start = [1, 2, 3, 4]
  end = [5, 6, 7, 8]
 
  for end_item in end:
  start.append(end_item)
 
  print(start)
 [1, 2, 3, 4, 5, 6, 7, 8]
 

 However, it is markedly repetitive, no?
 This is a common enough operation that there is a shortcut to find out
 about it.

 If you want to find out what methods there are, try help(...). I
 can't stress this enough.

  help(start)
 Help on list object:

 class list(object)
  |  list() - new empty list
  |  list(iterable) - new list initialized from iterable's items
  |
  |  Methods defined here:
  |
  |  __add__(...)
  |  x.__add__(y) == x+y
 ...
  |  append(...)
  |  L.append(object) -- append object to end
 ...
  |  extend(...)
  |  L.extend(iterable) -- extend list by appending elements from
 the iterable
 ...

 So list.extend seems to do exactly this!

 You can always check the documentation
 http://docs.python.org/tutorial/datastructures.html.
 An lo! The documentation says start.extend(end) is _equivilant_ to
 start[len(start):] = end.

 Why?

 Well, this uses the slicing syntax.

  start[:3]
 [1, 2, 3]
  start[3:]
 [4]
  start[2:3]
 [3]

 Wonderously, all these really say are ranges in the list. Hence, you
 can put lists in their place.

 start[len(start):] = end means start[-1:] = end, so what you're
 doing is saying the empty end part of the list is actually this new
 list. Hopefully that makes sense.

 Finally, there is another method. Instead of *changing* the list, you
 can make a new list which is equal to the others added together.

  new = start + end

 ___

 Theses methods all have their own upsides. If you want to change the
 list, use .extend(). If you want to change the list, but by putting
 the new list somewhere inside the old one, use slicing:

  start = [1, 2, 3, 4]
  end = [5, 6, 7, 8]
 
  start[2:2] = end
  print(start)
 [1, 2, 5, 6, 7, 8, 3, 4]

 Looping is good for when you want to generate the extra items as you go
 along.

 Finally, if you want to keep the old list or use these inline, use +.

 ___

 Note that, being in the unfortunate position of away from an
 interpreter, none of my examples are copy-pastes. Hence they may be
 wrong :/

 # Not checked for errors, typos and my friends messing with it.

-- 
http://mail.python.org/mailman/listinfo/python-list


Re: can we append a list with another list in Python ?

2012-10-23 Thread inshu chauhan
ok I got it guys...

On Tue, Oct 23, 2012 at 10:08 PM, Joshua Landau
joshua.landau...@gmail.comwrote:

  On 23 October 2012 21:06, Joshua Landau joshua.landau...@gmail.comwrote:

  On 23 October 2012 21:03, Joshua Landau joshua.landau...@gmail.comwrote:

 On 23 October 2012 12:07, Jean-Michel Pichavant 
 jeanmic...@sequans.comwrote:

 - Original Message -

  Thankyou.. but my problem is different than simply joining 2 lists
  and it is done now :)


 A lot of people though you were asking for joining lists, you
 description was misleading.

 I'll take a guess: you want to flatten a list of list.
 Nested list comprehensions can do the trick.

 aList =[[1,5], [2,'a']]
 [item for sublist in aList for item in sublist]

 ...
 [1, 5, 2, 'a']

 I find it rather difficult to read though.


 We have a library function for this, in the one-and-only itertools.

  listoflists = [list(range(x, 2*x)) for x in range(5)]
  listoflists
 [[], [1], [2, 3], [3, 4, 5], [4, 5, 6, 7]]
  from itertools import chain
   list(chain.from_iterable(listoflists))
 [1, 2, 3, 3, 4, 5, 4, 5, 6, 7]


 It does exactly what it says... fast and easy-to-read.


 Note that I think what he really wanted is to go from

 a, b, c = [list(x) for x in (range(10), range(11, 20), range(21, 30))]

 to

 list(range(30))


 UNDO! UNDO! UNDO!

 I *meant *to say:

  Note that I think what he really wanted is to go from

 a, b, c = [list(x) for x in (range(10), range(11, 20), range(21, 30))]

 to

 [a, b, c]


-- 
http://mail.python.org/mailman/listinfo/python-list


a prob.. error in prog ..dont knw how to correct

2012-10-21 Thread inshu chauhan
I am new to python and have a little problem to solve .. i have an
array with x, y, z co-ordinates in it as a tuple. I am trying to find
the distance between each point and sorting the points according to
the min distance.. i have tried a prog but m stuck bcoz of this error
which I am unable to correct



import cv
from math import floor, sqrt, ceil
from numpy import array, dot, subtract, add, linalg as lin

def calcdist(data):
for p in data:
x = p[0]
y = p[1]
z = p[2]
for i in range(len(data)):
  dist = sqrt((x[i]-x[i+1])**2 + (y[i]-y[i+1])**2 +(z[i]-z[i+1]**2))
  return dist


def ReadPointCloud(filename):
return [tuple(map(float, l.split()[1:4])) for l in open(filename)]

def main (data):

for i in range(len(data)): # Finding Neighbours
   for j in range(len(data)):
  dist = calcdist(data)
  print dist


if __name__ == '__main__':
data = ReadPointCloud(r'C:\Thesis\NEHreflectance_Scanner_1_part.txt')
data = data[0:100]
main(data)






the error m getting is...




Traceback (most recent call last):
  File C:\Users\inshu\Desktop\cal-dist.py, line 29, in module
main(data)
  File C:\Users\inshu\Desktop\cal-dist.py, line 22, in main
dist = calcdist(data)
  File C:\Users\inshu\Desktop\cal-dist.py, line 11, in calcdist
dist = sqrt((x[i]-x[i+1])**2 + (y[i]-y[i+1])**2 +(z[i]-z[i+1]**2))
TypeError: 'float' object has no attribute '__getitem__'
-- 
http://mail.python.org/mailman/listinfo/python-list


pls help me with this prog

2012-10-19 Thread inshu chauhan
in this prog I have written a code to calculate teh centre of a given 3D data..

but i want to calculate it for every 3 points not the whole data, but
instead of giving me centre for every 3 data the prog is printing the
centre 3 times...

import cv
from math import floor, sqrt, ceil
from numpy import array, dot, subtract, add, linalg as lin




def CalcCentre(data):
centre = array([0,0,0])
count = 0
n = 0
for p in data[n:n+3]:
centre = add(centre, array(p[:3]))
count += 1
centre = dot(1./count, centre)
return centre
n += 1
def ReadPointCloud(filename):
f = open(filename)
result = []
for l in f:
sp = l.split()
t = tuple(map(float, sp[1:4]))
result.append(t)
return result

def main (data):


j = 0
for  i in data[:3]:
while j != 3:
 centre = CalcCentre(data)
 j += 1
 print centre


if __name__ == '__main__':
data = ReadPointCloud(r'Z:\data\NEHreflectance_Scanner 1_part.txt')

main(data)




PLS HELP 
-- 
http://mail.python.org/mailman/listinfo/python-list