Re: preparing data for visualization
On Nov 12, 5:12 pm, John Machin <[EMAIL PROTECTED]> wrote: > [EMAIL PROTECTED] wrote: > > I would like to have my data in a format so that I can create a > > contour plot. > > > My data is in a file with a format, where there may be multiple fields > > > field = 1 > > > 1a 0 > > 2a 0 > > The above is NOT consistent with the later listing of your data file. > > [big snip > > > 10b 0 > > > where the value is how far from the center it will be displaced, > > > a b a > > b a b > > 10 00|00 00|00 00| > > [big snip of seemingly irrelevant stuff] > > > > > > > 100|00 00|00 00| > > 00 > > > I could possibly have many of these that I will add together and > > normalize to one. > > > Also, there are 60 a and b blocks, the middle 40 are 0.5 times the > > width of the outer 20. > > > I thought about filling an array, but there is not a one to one > > symmetry. > > > I cannot seem to get my head around this. Can anybody help me get > > started? > > > I have tried to use a dictionary, but cannot seem to get it to work > > the way I want. > > > I try this, > > > ----- > > > f = open('TEST1.MLC') > > > fields = {} > > > for line in f: > >if line.split()[0] == 'Field': > > >field = int(line.split()[-1]) > > Do line.split() ONCE per line. > > > > >elif line.split()[0] == 'Leaf': > >fields[field] = line.split()[-1] > >else: > >line = f.next() > > Don't mix > for line in f > and > line = f.next() > otherwise you will you will skip lines that you don't want to skip and/ > or become confused. > > > > > ----- > > > and get, > > > ----- > > > Traceback (most recent call last): > > File "", line 1, in > >line.split()[0] > > IndexError: list index out of range > > This indicates that you have a list for which 0 is not a valid index. > If it had 1 or more elements, then 0 would be a valid index. I > conclude that the list is empty. This would happen if line contained > no characters other than whitespace. In other words, the blank lines between the blocks of data. > > > > > > > Here is my data file, > > > ----- > > > File Rev = G > > Treatment = Dynamic Dose > > Last Name = Fodness > > First Name = Bryan > > Patient ID = 0001 > > Number of Fields = 4 > > Number of Leaves = 120 > > Tolerance = 0.50 > > > Field = 10 > > Index = 0. > > Carriage Group = 1 > > Operator = > > Collimator = 0.0 > > Leaf 1A = 0.00 > > Leaf 2A = 0.00 > [snip] > > Leaf 20A = 0.00 > > Leaf 21A = 5.00 > > Leaf 22A = 5.00 > [snip] > > Leaf 40A = 5.00 > > [big snip -- your code failed no later than the 10th line in the data > file] > To find out what is going on, print out some variables: > > 8<--- fodness.py > f = open('fodness.dat') > fields = {} > for lino, line in enumerate(f): > tokens = line.split() > print "Line %d: tokens = %r" % (lino, tokens) > if not tokens: > continue # blank/empty line > tok0 = tokens[0] > if tok0 == 'Field': > field = int(tokens[-1]) > elif tok0 == 'Leaf': > fields[field] = tokens[-1] > else: > continue > print " Fields:", fields > 8<--- > > Results [truncated]: > > C:\junk>fodness.py | more > Line 0: tokens = [] > Line 1: tokens = ['File', 'Rev', '=', 'G'] > [snip] > Line 8: tokens = ['Tolerance', '=', '0.50'] > Line 9: tokens = [] > Line 10: tokens = ['Field', '=', '10'] >Fields: {} > Line 11: tokens = ['Index', '=', '0.'] > Line 12: tokens = ['Carriage', 'Group', '=', '1'] > Line 13: tokens = ['Operator', '='] > Line 14: tokens = ['Collimator', '=', '0.0'] > Line 15: tokens = ['Leaf', '1A', '=', '0.00'] >Fields: {10: '0.00'} <<== Don't you need a float > instead of a string?? > Line 16: tokens = ['Leaf', '2A', '=', '0.00'] >Fields: {10: '0.00'} > Line 17: tokens = ['Leaf', '3A', '=', '0.00'] >Fields: {10: '0.00'} > Line 18: tokens = ['Leaf', '4A', '=', '0.00'] >Fields: {10: '0.00'} > Yep, crahing on blank lines. f = open(r'C:\python25\user\MLC\TEST1.MLC') fields = {} for line in f: the_line = line.split() # split only once if the_line: # test if the_line empty if the_line[0] == 'Field': # if not, start checking field = int(the_line[-1]) elif the_line[0] == 'Leaf': fields[field] = the_line[-1] ## f.next() removed > Don't you want/need to use the leaf IDs (1A, 2A, etc)?? I guess that > you want to end up with NESTED dictonaries, like this: > fields = { > 10: { > '1A': 0.0, >
Re: preparing data for visualization
[EMAIL PROTECTED] wrote: > I would like to have my data in a format so that I can create a > contour plot. > > My data is in a file with a format, where there may be multiple fields > > field = 1 > > 1a 0 > 2a 0 The above is NOT consistent with the later listing of your data file. [big snip > 10b 0 > > where the value is how far from the center it will be displaced, > > a b a > b a b > 10 00|00 00|00 00| [big snip of seemingly irrelevant stuff] > 100|00 00|00 00| > 00 > > I could possibly have many of these that I will add together and > normalize to one. > > Also, there are 60 a and b blocks, the middle 40 are 0.5 times the > width of the outer 20. > > I thought about filling an array, but there is not a one to one > symmetry. > > I cannot seem to get my head around this. Can anybody help me get > started? > > I have tried to use a dictionary, but cannot seem to get it to work > the way I want. > > I try this, > > - > > f = open('TEST1.MLC') > > fields = {} > > for line in f: >if line.split()[0] == 'Field': > >field = int(line.split()[-1]) Do line.split() ONCE per line. > >elif line.split()[0] == 'Leaf': >fields[field] = line.split()[-1] >else: >line = f.next() Don't mix for line in f and line = f.next() otherwise you will you will skip lines that you don't want to skip and/ or become confused. > > - > > and get, > > - > > Traceback (most recent call last): > File "", line 1, in >line.split()[0] > IndexError: list index out of range This indicates that you have a list for which 0 is not a valid index. If it had 1 or more elements, then 0 would be a valid index. I conclude that the list is empty. This would happen if line contained no characters other than whitespace. > Here is my data file, > > - > > File Rev = G > Treatment = Dynamic Dose > Last Name = Fodness > First Name = Bryan > Patient ID = 0001 > Number of Fields = 4 > Number of Leaves = 120 > Tolerance = 0.50 > > Field = 10 > Index = 0. > Carriage Group = 1 > Operator = > Collimator = 0.0 > Leaf 1A = 0.00 > Leaf 2A = 0.00 [snip] > Leaf 20A = 0.00 > Leaf 21A = 5.00 > Leaf 22A = 5.00 [snip] > Leaf 40A = 5.00 [big snip -- your code failed no later than the 10th line in the data file] To find out what is going on, print out some variables: 8<--- fodness.py f = open('fodness.dat') fields = {} for lino, line in enumerate(f): tokens = line.split() print "Line %d: tokens = %r" % (lino, tokens) if not tokens: continue # blank/empty line tok0 = tokens[0] if tok0 == 'Field': field = int(tokens[-1]) elif tok0 == 'Leaf': fields[field] = tokens[-1] else: continue print " Fields:", fields 8<--- Results [truncated]: C:\junk>fodness.py | more Line 0: tokens = [] Line 1: tokens = ['File', 'Rev', '=', 'G'] [snip] Line 8: tokens = ['Tolerance', '=', '0.50'] Line 9: tokens = [] Line 10: tokens = ['Field', '=', '10'] Fields: {} Line 11: tokens = ['Index', '=', '0.'] Line 12: tokens = ['Carriage', 'Group', '=', '1'] Line 13: tokens = ['Operator', '='] Line 14: tokens = ['Collimator', '=', '0.0'] Line 15: tokens = ['Leaf', '1A', '=', '0.00'] Fields: {10: '0.00'} <<== Don't you need a float instead of a string?? Line 16: tokens = ['Leaf', '2A', '=', '0.00'] Fields: {10: '0.00'} Line 17: tokens = ['Leaf', '3A', '=', '0.00'] Fields: {10: '0.00'} Line 18: tokens = ['Leaf', '4A', '=', '0.00'] Fields: {10: '0.00'} Don't you want/need to use the leaf IDs (1A, 2A, etc)?? I guess that you want to end up with NESTED dictonaries, like this: fields = { 10: { '1A': 0.0, '2A': 0.0, etc, }, 8: { etc, }, etc, } HTH, John -- http://mail.python.org/mailman/listinfo/python-list
preparing data for visualization
I would like to have my data in a format so that I can create a contour plot. My data is in a file with a format, where there may be multiple fields field = 1 1a 0 2a 0 3a 5 4a 5 5a 5 6a 5 7a 5 8a 5 9a 0 10a 0 1b 0 2b 0 3b 5 4b 5 5b 5 6b 5 7b 5 8b 5 9b 0 10b 0 field = 2 1a 0 2a 0 3a 0 4a 4 5a 4 6a 4 7a 4 8a 0 9a 0 10a 0 1b 0 2b 0 3b 0 4b 4 5b 4 6b 4 7b 4 8b 0 9b 0 10b 0 field = 3 1a 0 2a 0 3a 0 4a 0 5a 3 6a 3 7a 0 8a 0 9a 0 10a 0 1b 0 2b 0 3b 0 4b 0 5b 3 6b 3 7b 0 8b 0 9b 0 10b 0 where the value is how far from the center it will be displaced, a b a b a b 10 00|00 00|00 00| 00 900|00 00|00 00| 00 801|10 00|00 00| 00 701|10 00|00 00| 00 601|10 00|00 000111| 111000 501|10 00|00 000111| 111000 401|10 00|00 00| 00 301|10 00|00 00| 00 200|00 00|00 00| 00 100|00 00|00 00| 00 I could possibly have many of these that I will add together and normalize to one. Also, there are 60 a and b blocks, the middle 40 are 0.5 times the width of the outer 20. I thought about filling an array, but there is not a one to one symmetry. I cannot seem to get my head around this. Can anybody help me get started? I have tried to use a dictionary, but cannot seem to get it to work the way I want. I try this, - f = open('TEST1.MLC') fields = {} for line in f: if line.split()[0] == 'Field': field = int(line.split()[-1]) elif line.split()[0] == 'Leaf': fields[field] = line.split()[-1] else: line = f.next() - and get, - Traceback (most recent call last): File "", line 1, in line.split()[0] IndexError: list index out of range - Here is my data file, - File Rev = G Treatment = Dynamic Dose Last Name = Fodness First Name = Bryan Patient ID = 0001 Number of Fields = 4 Number of Leaves = 120 Tolerance = 0.50 Field = 10 Index = 0. Carriage Group = 1 Operator = Collimator = 0.0 Leaf 1A = 0.00 Leaf 2A = 0.00 Leaf 3A = 0.00 Leaf 4A = 0.00 Leaf 5A = 0.00 Leaf 6A = 0.00 Leaf 7A = 0.00 Leaf 8A = 0.00 Leaf 9A = 0.00 Leaf 10A = 0.00 Leaf 11A = 0.00 Leaf 12A = 0.00 Leaf 13A = 0.00 Leaf 14A = 0.00 Leaf 15A = 0.00 Leaf 16A = 0.00 Leaf 17A = 0.00 Leaf 18A = 0.00 Leaf 19A = 0.00 Leaf 20A = 0.00 Leaf 21A = 5.00 Leaf 22A = 5.00 Leaf 23A = 5.00 Leaf 24A = 5.00 Leaf 25A = 5.00 Leaf 26A = 5.00 Leaf 27A = 5.00 Leaf 28A = 5.00 Leaf 29A = 5.00 Leaf 30A = 5.00 Leaf 31A = 5.00 Leaf 32A = 5.00 Leaf 33A = 5.00 Leaf 34A = 5.00 Leaf 35A = 5.00 Leaf 36A = 5.00 Leaf 37A = 5.00 Leaf 38A = 5.00 Leaf 39A = 5.00 Leaf 40A = 5.00 Leaf 41A = 0.00 Leaf 42A = 0.00 Leaf 43A = 0.00 Leaf 44A = 0.00 Leaf 45A = 0.00 Leaf 46A = 0.00 Leaf 47A = 0.00 Leaf 48A = 0.00 Leaf 49A = 0.00 Leaf 50A = 0.00 Leaf 51A = 0.00 Leaf 52A = 0.00 Leaf 53A = 0.00 Leaf 54A = 0.00 Leaf 55A = 0.00 Leaf 56A = 0.00 Leaf 57A = 0.00 Leaf 58A = 0.00 Leaf 59A = 0.00 Leaf 60A = 0.00 Leaf 1B = 0.00 Leaf 2B = 0.00 Leaf 3B = 0.00 Leaf 4B = 0.00 Leaf 5B = 0.00 Leaf 6B = 0.00 Leaf 7B = 0.00 Leaf 8B = 0.00 Leaf 9B = 0.00 Leaf 10B = 0.00 Leaf 11B = 0.00 Leaf 12B = 0.00 Leaf 13B = 0.00 Leaf 14B = 0.00 Leaf 15B = 0.00 Leaf 16B = 0.00 Leaf 17B = 0.00 Leaf 18B = 0.00 Leaf 19B = 0.00 Leaf 20B = 0.00 Leaf 21B = 5.00 Leaf 22B = 5.00 Leaf 23B = 5.00 Leaf 24B = 5.00 Leaf 25B = 5.00 Leaf 26B = 5.00 Leaf 27B = 5.00 Leaf 28B = 5.00 Leaf 29B = 5.00 Leaf 30B = 5.00 Leaf 31B = 5.00 Leaf 32B = 5.00 Leaf 33B = 5.00 Leaf 34B = 5.00 Leaf 35B = 5.00 Leaf 36B = 5.00 Leaf 37B = 5.00 Leaf 38B = 5.00 Leaf 39B = 5.00 Leaf 40B = 5.00 Le