Can I overwrite a file without formatting it first?

Particularly useful chunks of hoc and/or NMODL code. May be pedestrian or stunningly brilliant, may make you gasp or bring tears to your eyes, but always makes you think "I wish I had written that; I'm sure going to steal it."
Post Reply
Krollibrius
Posts: 4
Joined: Wed Mar 15, 2017 5:48 pm

Can I overwrite a file without formatting it first?

Post by Krollibrius »

Hi,

I am generating a data table from a simulation. It is a table where line = sections in the neuron; and columns = times where an AP was recorded in each section (there is an APCount in each section). Eventually, I want each column of the file to represent the propagation of one AP through the neuron. APs do not overlap in time (the 1st is finished when the 2nd starts, etc.) so I guess it should be possible.

Problem is, as all APs do not propagate through the whole neuron, all lines do not get filled in at each iteration of the loop going through the sections. Thus, a shift appears in the columns, and columns may contain times belonging to the previous/next AP (not anymore one column = one AP). As I said, I want each AP to have its own column.

The solution I thought about was to first initialize a big table with values eg. 9999 (I'll be looking for the minimum of each column, hence the big number so it does not interfere). I'm first initializing the table (this works fine), then closing it. I want to re-open it and write over some of the 9999s. Now the issue is that NEURON formats the whole file if I try re-writing in it. The culprit seems to be wopen(): it is erasing the whole file each time it is called. However, if I don't close the file after initializing the table, NEURON will just append the data below the initialized table. What I want is NEURON to replace some of the 9999s by the actual data; or in other words, just writing over the file without formatting it.

Is there anyway I can do that?
ted
Site Admin
Posts: 6287
Joined: Wed May 18, 2005 4:50 pm
Location: Yale University School of Medicine
Contact:

Re: Can I overwrite a file without formatting it first?

Post by ted »

I wouldn't bother with APCounts. Instead I'd use NetCons to detect the spikes. Associate each NetCon with a unique identifier, and capture all of the detected spike times and their associated identifiers to a single pair of Vectors. Fast, easy to code, no wasted space, and you can do whatever postprocessing you want to the output. This strategy is commonly used to capture the spike times of cells in a network; see
Hines, M.L. and Carnevale, N.T. Translating network models to parallel hardware in NEURON. J. Neurosci. Methods 169:425-455, 2008
and ModelDB entry 96444 for working code.
The solution I thought about was to first initialize a big table with values eg. 9999 (I'll be looking for the minimum of each column, hence the big number so it does not interfere). I'm first initializing the table (this works fine), then closing it. I want to re-open it and write over some of the 9999s. Now the issue is that NEURON formats the whole file if I try re-writing in it.
Actually it ignores what was previously written and writes anew. Not the same as formatting.
I don't close the file after initializing the table, NEURON will just append the data below the initialized table
because file I/O is inherently serial. hoc does not support random write access to a file.
What I want is NEURON to replace some of the 9999s by the actual data; or in other words, just writing over the file without formatting it.

Is there anyway I can do that?
Wow, what a leap back into the remote past. You're talking about what used to be called "random access of a file's contents." Necessary back in the 1970s and 1980s when RAM was expensive and "mass storage" consisted of tape or those gigantic "hard drive platters" IBM used to make. Remember those sci-fi movies from that era, with big racks of equipment and lots of tape drives spinning back and forth?

Sure, deal with your new data that way if you insist, but you're going to have to do it in RAM, and write the revised data to disk in its entirety after you're done. That would work for a single spike, but it's not a good way to handle two or more spikes.
Post Reply