john-judge / PhotoZ_upgrades

CCD controller, data acquisition, and stimulus administration software for Dr. Meyer Jackson's hVOS voltage imaging technique. This repository tracks the transition from PhotoZ OG to NI-DAQmx and the "Little Dave" DM2K Camera
MIT License
1 stars 0 forks source link

July 2021 Notes: Superframing and More Image Reassembly #21

Closed john-judge closed 3 years ago

john-judge commented 3 years ago

7/1/2021

Continue from the achievement at the end of #18

TO DO:

7/5/2021

Steps taken to produce the following image: 1) Disable call to remapQuadrantsOneImage 2) Ran TurboSM acquire one image, closed TurboSM 3) Acquired 475 RLI points 4) Plotted 350th image (light on), with Python quadrant-remapping enabled readout-RLI-remappedfull-out450

Improved overall efficiency of image reassembly by reducing number of tmpBuf allocations and copy-overs. Noticeably faster -- by an estimated order of magnitude. Correctness seems to have been preserved.

Now able to remap and flip quadrants entirely within PhotoZ!

2048x50 Readout

Doesn't seem to be successful every time, variation in the raw data. This could suggest the camera is still trying to read out a large 2048x1024 image (instead of downsampling/reducing resolution) and we're only getting pieces of it here. readout-RLI-full-out350

TurboSM: image

Or maybe the PDV channel threads are too slow to keep up with the camera at this frequency? Need superframe? After all, we did just jump from 200 Hz to 2000 Hz

readout-RLI-full-out350

1024x160 Readout

Back down to 1000 Hz. This one seems to follow a different deinterleave pattern (alternating-row stripes appear when zoomed in to pixels). However, maybe it is less likely to suffer from missing frame issues.

readout-RLI-full-out350

Superframing

Attempting to use

pdv_setsize(pdv_pt[ipdv], width(), height() * get_superframe_factor()

to introduce superframing. It seems that dimension validation doesn't match up -- pdv_get_imagesize is reporting a superframe factor of 2 when I asked for a factor of 10. Also, pdv_setsize dimension changes seem to persist after the first call, disallowing further modification -- more investigation needed.

Superframe seems to be possibly working (at least passing validation). However, now the LED stays on for ~10 seconds. I reformatted the raw image organization scheme. Now, all data from a single channel is contiguous in one block, followed by a block for the next channel, and so on. Need to have Camera image reassembly functions adopt this convention, but this is a minor change. Then I can see if superframing is working (I'm not optimistic).

2:40 PM

With the introduction of pdv_setsize, it seems opening TurboSM before PhotoZ right when the cam is booted is no longer needed. Also, having restarted the camera, it no longer takes ~10 seconds with LED on -- now seems to be back to how it worked before. However, I'm not seeing the data read out properly to file. Attach debugger maybe? Could it be that the camera is just reading back all zeros like I have seen before?

3:08 PM

Fixed some memory arithmetic bugs. First, not getting back meaningful data (a few odd columns) Then, opened TurboSM. Suddenly, we have plenty of bright data in PhotoZ, but seemingly not in the correct dimensions. The cam height is stuck at 512 (left there by TurboSM) and the image height is 500 (height * superframe) Then try PhotoZ RLI again. Returns back to first state.

How to superframe? Maybe edit .cfg files? Before that heavy-handed of an approach, will browse TurboSM code some more.

4:14 PM

Attempted superframing (x10) by creating a copy of a .cfg file called 'DM2K_2048x500.cfg' However, I'm not sure this worked properly -- does the camera know that PDV is going to treat the data as coming in periods of 50 rows (for a total of 500 rows per image)? readout-RLI-full-out350 Alternatively -- suspect problem with pointer arithmetic?

4:53 PM

There was one pointer arithmetic problem, but still not correct Looked up .cfg directives and tried setting frame_height = 50 -- but this directive may be deprecated according to some PDV docs. Same question -- looks as if a 2048x500 image is being read out instead of 10 2048x50 images? As a reasonability check, read out 2048x1024 at this point -- appears to be something gone wrong in the transition to the new channel-contiguous blocks of memory. Let's fix that before proceeding.

john-judge commented 3 years ago

7/6/21

From TurboSM:

pdv_start_images(pdv_pt[thread_id], loops + (factor > 1 ? 1 : 0));

where second argument is count -- appears that this needs to be the number of superframed images? Testing modification to Camera::start_images():

    // count    number of images to start. A value of 0 starts freerun. To stop freerun, call pdv_start_images again with a count of 1.
    //pdv_start_images(pdv_pt[ipdv], 0);
    pdv_start_images(pdv_pt[ipdv], get_superframe_factor()); // changed COUNT arg from 0 to superframe factor.

Use this together with pdv_setsize and the height to a factor of superframe? Or with frame_height cfg directive?

Also, previously:

As a reasonability check, read out 2048x1024 at this point -- appears to be something gone wrong in the transition to the new channel-contiguous blocks of memory. Let's fix that before proceeding.

Still seeing the trouble where TurboSM must be run before PhotoZ starts acquiring properly.

10:57 AM

Still hunting down memory pointer arithmetic bug.

12:00 PM

Bug resolved! Turns out walking pointers are much more elegant and easier to debug than arithmetic assignments to pointers.

Testing with pdv_setsize and start_images with count arg passed, and with the original .cfg file. First a regression check with 200 Hz setting. This setting has a superframe factor of 1 and still starts images in freerun mode (count = 0). Result: Need to disable pdv_setsize call for this setting, I think.

john-judge commented 3 years ago

7/7/21

Disabled pdv_setsize and reverted count arg call for the 200 Hz setting, and as expected the previous correct behavior remains (no superframing at this setting). Since is the case that TurboSM must be run first before images in PhotoZ are correctly read out. Maybe TurboSM is setting the camera gain or something?

For now, however, focus on superframing investigation with 2048x50 at 2000 Hz setting.

Superframing Investigation: 2048x50 cfg (2000 Hz)

Questions based on TurboSM investigation

To-do next

1:04 PM

Attempted adding this just after initcam script calls:

    sprintf(command, "@RCL %d", m_program);
    serial_command(command);

Test: RLI at 200 Hz w/ above, no change (still needed TurboSM to init properly)

200 Hz did not work until I reverted serial_command to serial_write and removed @RCL write

john-judge commented 3 years ago

7/8/21

Changed raw size alloc in DataArray.cpp to match the expected display width and display height (since cam.width and cam.height are the cfg quadrant dimensions).

Testing different initcam instruction orderings. Open channel after script load?

Moved channel-opening from MainController::takeRli into the Camera.init_cam just after the cfg files are loaded. Seems no difference -- still requires TurboSM to run beforehand.

Now note where else is Camera.init_cam called -- how might this affect / create a difference from Chun's recommended init seq?

Tried adding @RCL back. According to Little Joe book, RCL # is recall settings -- accesses settings from the camera's storage. (Little Dave seem to use the same commands? Chun's code sends these commands to 2K's, at least). Result: image was read out properly. Does it still require TurboSM to start first? Yes.

In general, the result when reading out before TurboSM has been either no data or just a small amount of data -- apparently just the very top rows and very bottom rows, not in the expected location either: readout-RLI-full-out355

11:30 AM

Replaced pdv_wait_image with pdv_wait_image_raw. Result: no difference. Reverted the pdv_setsize change for 200 Hz. After the first readout with pdv_setsize, all subsequent readouts give no data.

From Little Joe book on @REP:

The last segment in any sequence is the accumulation (or integration) segment. This may be a short do-nothing operation, or may involve dithering on long exposures to reduce dark current.

Repetitions -- Adjust the number of times the accumulation segment of a program is repeated.

So apparently the @REP serial command has nothing to do with dimensions or superframing, and even if it's as relevant to Little Dave as it is to Little Joe, it's not the missing piece.

Testing adding "@SEQ 0" just after the RCL # serial command. Result: camera is slow, taking images takes ~ minutes.

Removed the second RCL call (x4) from cam.program

Maybe the camera hung indefinitely because there was no matching SEQ 1 to start the camera? added these commands at the end of cam.init_cam, mirroring TurboSM:

    sprintf(command, "@TXC 0");
    serial_write(command);
    sprintf(command, "@SEQ 1"); // start 
    serial_write(command);

but the result is unchanged: camera is slow, ~ min. Reverted.

SM_take_tb Superframed Acqui Example

pdv_start_images(pdv_pt[thread_id], 4);
image_ptr = pdv_wait_image_timed_raw(pdv_pt[thread_id], (unsigned int *)&times[thread_id][0], 1);
pdv_start_image(pdv_pt[thread_id]);
for (int i = 0; i < factor; ++i) {
    memcpy(data_ptr[thread_id], image_ptr, frame_length / factor);
    image_ptr += frame_length / factor;
    data_ptr[thread_id] += image_length / factor;
}

Attempted to mirror PDV doc to read images out in multiple buffers, increasing multibuf from 4 to superframe factor -- Result: long-duration acquisition for program 6, still requires TurboSM to be run first, and only works for the 1st frame of each superframe, but the image seems correct: image Repeated it a second time -- like always whenever pdv_setsize is used, the second time reads out no data.

Attempt Same But 4-Multibuf, Superframing still

2000 Hz -- LED was on ~9 s (improvement!) But only the first 1/10 (superframe=10) of each superframe was filled (only frame 1 of 10). Reattempted with having the loop fixed to superframe=1. Took several times longer to acquire. Result: similar. Seems only first of the 4 multibufs has any data.

john-judge commented 3 years ago

7/9/21

Meyer's email from Chun:

For NeuroCCD or any camera single-edt/pdv-channel

pdv_flush_fifo(pdv_pt);
pdv_multibuf(pdv_pt, 4);
//has to be 4 for 64bit. (You are missing this line)

pdv_start_images(pdv_pt, loops);
for (int j = 0; j < loops; j++) {
image_ptr = pdv_wait_image_raw(pdv_pt);
memcpy(data_ptr, image_ptr, frame_length);
data_ptr[0] += image_length;
}

The loops can just be the number of frames for a start.

To reduce interrupts and ensure not to lose any frames (especially at high frame rate), we make super frames, meaning each frame will have height of orig_height*factor, then the loops = num_frames/factor.

For 32bit, we used to read in another config that sets the height 10x (80x800 from 80x80). For 64bit, we do

pdv_setsize(pdv_pt, config_num_col, config_num_row*factor);

For DaVinci, list of serial commands: DV-2K-CamInit.txt

john-judge commented 3 years ago

7/14/21

Summary of progress from past couple days:

john-judge commented 3 years ago

7/20/21

Adjusting start line lib by trial-and-error.

2048x100

readout-RLI-full-out355 readout-RLI-full-out355 readout-RLI-full-out355 Final start_line_lib value is 888+142 (last image)

1024x320

readout-RLI-full-out355 image

1024x160

readout-RLI-full-out355

CDS subtraction width seems to be half of cfg width, not always fixed at 1024.

Correct start_line_lib:

576 1038 766 926 766 926 846 926 - start_line_lib
john-judge commented 3 years ago

7/20/21

hbin and ccd_lib_bin fix did the trick!

Pulled the correct values from sm_config.dat. Also corrected the start_line_lib as well.

The following were read out one after another, without having run TurboSM beforehand, and without any camera restarts.

1024x320

readout-RLI-full-out355

1024x160

readout-RLI-full-out355

2048x100

readout-RLI-full-out355

2048x1024

readout-RLI-full-out355

512x160

readout-RLI-full-out355

512x80

readout-RLI-full-out355

256x60

readout-RLI-full-out355

256x40

image

john-judge commented 3 years ago

Work on this project now continues in https://github.com/john-judge/PhotoLib