WorldViz User Forum  

Go Back   WorldViz User Forum > Vizard

Reply
 
Thread Tools Rate Thread Display Modes
  #1  
Old 03-23-2006, 12:08 PM
betancourtb82 betancourtb82 is offline
Member
 
Join Date: Jan 2006
Posts: 103
Data Files

I'm creating a data file for the program I wrote. Here is the setup. I have an input named subject called by viz.input. Then I say the following in the data file:

Code:
start_time = time.time()
question_data = open('study_data','a')
elapsed_time = time.time() -start_time
data = 'Subject is ' + str(subject)
question_data.write(data)
question_data.flush()
The problem is when I open the data file, it prints out the information in some duplicated way (e.g. with subject number 1). Below is how it is printed. Am I doing something wrong?

Subject is Subject is 1
Reply With Quote
  #2  
Old 03-23-2006, 12:38 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
Hi,

You're opening the file for appending. So it is adding the data to the existing data in the file. If you want to create the file from scratch open it using the 'w' flag:
Code:
question_data = open('study_data','w')
Reply With Quote
  #3  
Old 03-23-2006, 12:45 PM
betancourtb82 betancourtb82 is offline
Member
 
Join Date: Jan 2006
Posts: 103
Yes, I understand I can use the 'w' flag but what happens when I want to add data to the same data file. I can use the 'w' flag for the first time, but then i'd want to append to the file after that. I tried the 'w' flag and it printed out correctly, but then when I changed it to the 'a' flag, it went back to printing strangely again.

The 'w' flag prints like this:
Subject 12 Condition 1

The 'a' flag prints like this:
Subject Condition
Subject 12 Condition 2
Reply With Quote
  #4  
Old 03-23-2006, 01:07 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
Hi,

Then I'm not sure exactly what you want. You can either append the data to the file using the 'a' flag, or you can override the existing data using the 'w' flag. What other option do you need?
Reply With Quote
  #5  
Old 03-23-2006, 07:57 PM
betancourtb82 betancourtb82 is offline
Member
 
Join Date: Jan 2006
Posts: 103
Talking

No problem,
I figured out the problem. I had to put those statements in a onkeydown event.

Thanks!
Reply With Quote
  #6  
Old 05-04-2006, 03:23 PM
betancourtb82 betancourtb82 is offline
Member
 
Join Date: Jan 2006
Posts: 103
I created this data file but one question I had was whether there was a way to place Headings at the beginning of the file. If you notice right now, there is text (eg 'male count', 'female count', etc) before the actual data used to identify the data that is being shown during a question_data.write() command. Is there a way to only put those headings at the beginning of the data file instead of having them included in the write command. I hope I'm being clear. Here is an example I typed out (attachment)

Code:
#Mark the time that the program was started.
start_time_sec = time.time()
start_time_clock = time.ctime()
#Open a file to collect the data on this subject.
question_data = open('omar_data','a')
#Get the data from the button press.
def mykey(key):
 global autospawn, soldierCount, maleCount, femaleCount
 if key == 'a':
  autospawn = not autospawn
  if autospawn:
   viz.starttimer(SPAWN_TIMER,GetSpawnTime())
  else:
   viz.killtimer(SPAWN_TIMER)
 elif key == 's':
  StartAvatar(randint(0,5))
 elif key == 'q':
  miss_count = bulletCount - avatarsHit
  #print "miss count is", miss_count
  #Use our starting time variable to find out how much
  #time has elapsed.
  elapsed_time_sec = time.time() - start_time_sec
  #elapsed_time_clock = time.ctime(elapsed_time_sec) + time.ctime(start_time_sec)
  final_time_sec = start_time_sec + elapsed_time_sec
  final_time_clock = time.ctime(final_time_sec)
  #Create string lines to put in the data file, depending on which button was pushed.
  subject_data = str(subject) + '\t'  
  start_data = str(start_time_clock) + '\t'
  final_time_data = str(final_time_clock) + '\t'
  avatar_hit_count = str(avatarsHit) + '\t'
  bullet_count_data = str(bulletCount) + '\t'
  miss_count_data = 'miss count ' + str(miss_count) + '\t'
  soldier_Count_data = 'soldier count ' + str(soldierCount) + '\t'
  male_Count_data = 'male count ' + str(maleCount) + '\t'
  female_Count_data = 'female count ' + str(femaleCount) + '\t'  
  threat_Count_data = 'threat count ' + str(threatCount) + '\t'
  nonthreat_Count_data = 'nonthreat count ' + str(nonthreatCount) + '\t\n'
  question_data.write(subject_data)  
  question_data.write(start_data)
  question_data.write(final_time_data)
  question_data.write(avatar_hit_count)
  question_data.write(bullet_count_data)
  question_data.write(miss_count_data)
  question_data.write(soldier_Count_data)
  question_data.write(male_Count_data)
  question_data.write(female_Count_data)
  question_data.write(threat_Count_data)
  question_data.write(nonthreat_Count_data)  
  #Flush the internal buffer.
  question_data.flush() 
  #Close the world.
  viz.quit()
viz.callback(viz.KEYBOARD_EVENT,mykey)
Attached Files
File Type: txt example.txt (484 Bytes, 2191 views)
Reply With Quote
  #7  
Old 05-04-2006, 03:43 PM
farshizzo farshizzo is offline
WorldViz Team Member
 
Join Date: Mar 2003
Posts: 2,849
Hi,

After you open the file, check if it is empty. If it is then write the headings.
Code:
data = open('test.data','a')

#Seek to end of file
data.seek(0,2)
if data.tell() == 0:
	#File is empty, write heading
	data.write('Column 1\tColumn 2\tColumn 3\n')

data.write('some data')
Reply With Quote
Reply


Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off

Forum Jump


All times are GMT -7. The time now is 06:28 PM.


Powered by vBulletin® Version 3.8.7
Copyright ©2000 - 2024, vBulletin Solutions, Inc.
Copyright 2002-2023 WorldViz LLC