Announcing the new Datavyu v1.3.6 with enhanced features and transcription support!

I am working with a Print Multiple Files script, that runs, and prints the arguments. However, it lumps all the lines in a continuous block, without separating segments for different subjects. Is there a way to tell the script to separate them when it generates the output file? Let me know. Here's the script below:

begin #$debug=true

# Which directory do we want to load files from? Get this from the server.  
file_directory = File.expand_path("~/Desktop/OpenSHAPA/")

# Obtain a listing of files in the directory
static_dir = Dir.new(file_directory)

# Set up the arguments that we want to print for each variable.
# These must be exactly the argument names from openshapa but with all
# punctuation removed and made into all lowercase.
# So Reach.Hand becomes reachhand
# Test.date becomes testdate
# etc
# This is done by variable.
id_order = ["corw","subj","sex", "age", "walkexp", "cruisexp", "crawlexp", "tunnexper", "cond", "flcoder", "tuncoder"]
floor_order = ["onset", "offset", "trialnum", "mom", "choice", "dirshifts", "back", "lookoth", "locstart", "locshift"]
lat_order = ["offset"]
#tun_order = ["onset", "offset", "trialnum", "mom", "choice", "dirshifts", "back", "lookoth", "locstart", "locshift"]
#tunlat_order = ["offset", "shift"]

# Open the file we want to print the output to
# ~ is a shortcut for the current user's home directory, ~/Desktop/ will put it
# on your desktop
output_file = File.new(File.expand_path("~/Desktop/OpenSHAPA_Output.txt"), 'w')

# Put the header together.
header = id_order + floor_order + lat_order #+ tun_order + tunlat_order
for h in header
    output_file.write(h + "\\t")
end
output_file.write("\\n")

# Finally, loop through all of the files and print everything
for file in static_dir
    if file.include?(".opf")

        # Load the OpenSHAPA file into Ruby
        puts "Opening", file
        $db, $pj = load_db(file_directory + "/" + file)

        # Get the variables we want to print from the loaded file
        id = getVariable("ID")
        floor = getVariable("FLOOR")
        lat = getVariable("LATENCY")
        #tun = getVariable("TUNNELS")
        #tunlat = getVariable("TUN-LAT")

        puts "Got variables"

        # Loop over the cells in ID
        puts id.cells
        for idcell in id.cells
          puts idcell
            # Loop over the cells in condition
            for flcell in floor.cells
                # Make sure that the condition cell is INSIDE OF the ID cell
                if idcell.onset <= flcell.onset and idcell.offset >= flcell.offset
                    # Loop over the trial cells
                    for latcell in lat.cells
                        # Make sure that the lat cell is INSIDE OF the floor cell
                        if flcell.onset == latcell.onset and latcell.offset <= flcell.offset
                          # Loop over the tunnel cells
                          #for tuncell in tun.cells
                            # Make sure that the tunlat cell is Inside of the tun cell
                            #if tuncell.onset == tunlatcell.onset and tunlatcell.offset >= tuncell.offset
                              puts "Printing!"
                              # Print this ID's information
                              print_args(idcell, output_file, id_order)
                              # Print this Floor cell's information
                              print_args(flcell, output_file, floor_order)
                              # Print this Latency cell's information
                              print_args(latcell, output_file, lat_order)
                              # Print this Tunnel's information
                              #print_args(tuncell, output_file, tun_order)
                              # Print this Tun-lat's information
                              #print_args(tunlatcell, output_file, tunlat_order)
                              # And write a newline to the output file so the next cell
                              # is on its own line
                              output_file.write("\\n")
                            #end
                          #end
                        end
                    end
                end
            end
        end
    end
end
puts "FINISHED"

end

asked 18 Jul '13, 14:53

jducz's gravatar image

jducz
81558
accept rate: 0%


12next »

Before we can be sure how to best answer your question I need to ask a few of my own. How is the data coming out in blocks? Is it not separating by cell w/in a particular participant or is it affixing the first line of the next participant to the last line of the previous one? Are you planning on importing the data to SPSS or a different program?

permanent link

answered 23 Jul '13, 09:50

david's gravatar image

david ♦
143235
accept rate: 4%

Hey David,

To answer your questions, the script is affixing the first line of the next participant to the last line of the previous one. We are planning to import the data into SPSS.

Let me know if you have any other questions.

permanent link

answered 23 Jul '13, 10:54

jducz's gravatar image

jducz
81558
accept rate: 0%

If I understand correctly, the problem is that it's not inserting a line carriage (like pressing "enter" on the keyboard) in between subjects?

If you're running the script on a Windows machine this is an easy fix. On Windows you need "\r\n" to insert a new line, instead of just "\n". Macs and Linux only require the "\n".

(In case you're curious: "\n" is a line feed, while "\r" is a line carriage. For some reason, Windows requires both to insert a new line, while the other OS just need the line feed.)

permanent link

answered 23 Jul '13, 11:21

WhitneyCole's gravatar image

WhitneyCole ♦
417239
accept rate: 21%

In this script you have "\\n" and "\\t" instead of just "\n" and "\t". These are two different things: "\\n" translates literally to "\n" instead of a newline character. Same with tab, "\\t" is "\t", where "\t" is <tab>. I believe that changing all instances of \\n to \n will solve your newline problem. But if it doesn't on Windows, try what Whitney suggested with \r\n.

permanent link

answered 31 Jul '13, 17:24

Jesse's gravatar image

Jesse ♦
11181015
accept rate: 53%

I am unsure of how it c/p a '\\' instead of a '\n', but the script in my file had a '\n'. I tried re-running the script, but I am still running into the same issue. We are running the script on a Mac computer, so we are not running into the \r\n problem that Whitney highlighted. Is there a different command that translates into a new break line? Let me know.

permanent link

answered 13 Aug '13, 13:14

jducz's gravatar image

jducz
81558
accept rate: 0%

Hm. \r and \n are the only new line characters I know of (at least on the standard systems). What version of OS X are you using? And what program are you opening the text file in (where do you see that the script is printing incorrectly)?

permanent link

answered 13 Aug '13, 13:41

WhitneyCole's gravatar image

WhitneyCole ♦
417239
accept rate: 21%

Dear Whitney,

We are using OSX version 10.8. The printed file is opening up in TextEdit. The script prints all the information we need, it just doesn't print line breaks.

permanent link

answered 13 Aug '13, 14:21

jducz's gravatar image

jducz
81558
accept rate: 0%

Hello,

This is a strange issue. It looks like those newline characters should be printing. Would you mind posting a file that we can test the script on so we can try to debug it by running it?

Also are the tabs between the columns in the also missing or is it just the newlines?

permanent link

answered 13 Aug '13, 14:52

Jesse's gravatar image

Jesse ♦
11181015
accept rate: 53%

I need karma > 60 to upload the file, unfortunately. Jesse, is your nyu e-mail listed on your CV still active? I can send the file to that address if needed.

For reference, here's the correct script. begin #$debug=true

# Which directory do we want to load files from? Get this from the server.  
file_directory = File.expand_path("~/Desktop/OpenSHAPA/")

# Obtain a listing of files in the directory
static_dir = Dir.new(file_directory)

# Set up the arguments that we want to print for each variable.
# These must be exactly the argument names from openshapa but with all
# punctuation removed and made into all lowercase.
# So Reach.Hand becomes reachhand
# Test.date becomes testdate
# etc
# This is done by variable.
id_order = ["corw", "subj", "group", "session", "sex", "age", "walkexp", "cruisexp", "crawlexp", "tunnexper", "cond", "flcoder", "tuncoder", "relflcoder", "reltuncoder"]
floor_order = ["onset", "offset", "trialnum", "mom", "choice", "dirshifts", "back", "lookoth", "locstart", "locshift"]
lat_order = ["offset"]
#tun_order = ["onset", "offset", "trialnum", "mom", "choice", "dirshifts", "back", "lookoth", "locstart", "locshift"]
#tunlat_order = ["offset", "shift"]

# Open the file we want to print the output to
# ~ is a shortcut for the current user's home directory, ~/Desktop/ will put it
# on your desktop
output_file = File.new(File.expand_path("~/Desktop/OpenSHAPA_Output.txt"), 'w')

# Put the header together.
header = id_order + floor_order + lat_order #+ tun_order + tunlat_order
for h in header
    output_file.write(h + "\\t")
end
output_file.write("\\n")

# Finally, loop through all of the files and print everything
for file in static_dir
    if file.include?(".opf")

        # Load the OpenSHAPA file into Ruby
        puts "Opening", file
        $db, $pj = load_db(file_directory + "/" + file)

        # Get the variables we want to print from the loaded file
        id = getVariable("ID")
        floor = getVariable("FLOOR")
        lat = getVariable("LATENCY")
        #tun = getVariable("TUNNELS")
        #tunlat = getVariable("TUN-LAT")

        puts "Got variables"

        # Loop over the cells in ID
        puts id.cells
        for idcell in id.cells
          puts idcell
            # Loop over the cells in condition
            for flcell in floor.cells
                # Make sure that the condition cell is INSIDE OF the ID cell
                if idcell.onset <= flcell.onset and idcell.offset >= flcell.offset
                    # Loop over the trial cells
                    for latcell in lat.cells
                        # Make sure that the lat cell is INSIDE OF the floor cell
                        if flcell.onset == latcell.onset and latcell.offset <= flcell.offset
                          # Loop over the tunnel cells
                          #for tuncell in tun.cells
                            # Make sure that the tunlat cell is Inside of the tun cell
                            #if tuncell.onset == tunlatcell.onset and tunlatcell.offset >= tuncell.offset
                              puts "Printing!"
                              # Print this ID's information
                              print_args(idcell, output_file, id_order)
                              # Print this Floor cell's information
                              print_args(flcell, output_file, floor_order)
                              # Print this Latency cell's information
                              print_args(latcell, output_file, lat_order)
                              # Print this Tunnel's information
                              #print_args(tuncell, output_file, tun_order)
                              # Print this Tun-lat's information
                              #print_args(tunlatcell, output_file, tunlat_order)
                              # And write a newline to the output file so the next cell
                              # is on its own line
                              output_file.write ("\\n")
                            #end
                          #end
                        end
                    end
                end
            end
        end
    end
end
puts "FINISHED"

end

permanent link

answered 13 Aug '13, 15:03

jducz's gravatar image

jducz
81558
accept rate: 0%

You can also send me the file and I can see if I can figure out what the issue is. My email is in my profile.

permanent link

answered 14 Aug '13, 14:01

david's gravatar image

david ♦
143235
accept rate: 4%

Hey David,

I cannot see your e-mail for some reason. Feel free to e-mail me: jducz@gc.cuny.edu

Thanks!

(20 Aug '13, 11:22) jducz jducz's gravatar image
Your answer
toggle preview

Follow this question

By Email:

Once you sign in you will be able to subscribe for any updates here

By RSS:

Answers

Answers and Comments

Markdown Basics

  • *italic* or _italic_
  • **bold** or __bold__
  • link:[text](http://url.com/ "title")
  • image?![alt text](/path/img.jpg "title")
  • numbered list: 1. Foo 2. Bar
  • to add a line break simply add two spaces to where you would like the new line to be.
  • basic HTML tags are also supported

Question tags:

×4
×2
×2
×1
×1

question asked: 18 Jul '13, 14:53

question was seen: 16,430 times

last updated: 22 Aug '13, 17:07

powered by OSQA