> For the complete documentation index, see [llms.txt](https://stitchit.swcmicroscopy.com/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://stitchit.swcmicroscopy.com/installation/setting-up-syncandcrunch.md).

# Setting up syncAndCrunch

A typical data set consists of tens of thousands of TIFFs. The goal of *StitchIt* is to assemble (stitch) these small tiles into planes, such that each channel of each optical section is stored in a single large TIFF image. Once the data have been stitched, *StitchIt* offers further tools to correct for artifacts and facilitate subsequent analyses.

Serial 2-photon tomography systems have good resolution but the acquisition session might last anywhere from a few hours, to a few days for galvo-based systems. *StitchIt* has a tool called `syncAndCrunch` that pre-processes tiles during acquisition, displays the last completed slice on the web, and automatically stitches the data at the conclusion of acquisition. This is useful since pre-processing of the images is more time consuming than stitching. We have found monitoring the state of the acquisition to be enormously helpful. In the past we have used this to rescue samples that would otherwise have been lost to things like laser instability, too much or too little laser power, incorrect PMT gain, air bubbles on the objective, etc.

#### Setting up to analyse during acquisition

Data are streamed directly from the acquisition machine to an analysis PC. The `syncAndCrunch` function running on the analysis machine periodically pulls data off the acquisition PC for pre-processing. Stitching is conducted automatically once acquisition completes. Stitched datasets that pass quality criteria are stored long-term on a large secure server. The analysis machine can only holds a limited number of data sets and are not secure.

#### Running syncAndCrunch

The following is an example of how to start an analysis session in MATLAB. It is highly recommended to run this from within [tmux](https://robots.thoughtbot.com/a-tmux-crash-course): the session will be persistent and you can attach to it remotely over the command line without needing virtual desktop.

```
syncAndCrunch('AcqSystemName')
```

The above command initiates a cycle that alternates between copying data from the buffer server to the local disk on the analysis machine, processing those data, and displaying a preview of the last completed section to the web. `syncAndCrunch` searches for a currently running acquisition and works on that.

Should you need to force it to stop (e.g. you finished the acquisition early) then do one of the following:

* Create a file called ‘FINISHED’ on the analysis machine or acquisition machine in the sample directory. `syncAndCrunch` will notice it and start stitching.
* Quit with `ctrl+c` and [enter the stitching commands manually](https://stitchit.swcmicroscopy.com/advanced-usage/stitching-existing-data.md).

`syncAndCrunch` assembles stitched images at the end of acquisition because it uses all available tiles to calculate the grand average images used for illumination correction. Stitched data are stored as one tiff per optical section in a directory called `stitchedImages_100`. Data from each channel are stored in separate sub-directories. See [here](https://stitchit.swcmicroscopy.com/new-users/syncandcrunch-walk-through.md) for a more detailed walk-through of the process.

#### Configuring syncAndCrunch

In addition to the command-line arguments, `syncAndCrunch` (and other *StitchIt* functions) reads settings from an INI file. [You should set up the INI file for your system](https://stitchit.swcmicroscopy.com/installation/setting-up-the-ini-file.md) before attempting to use `syncAndCrunch`. Make sure you set up a valid `landingDir`, which is where the data will be sent on your analysis machine.

#### Monitoring acquisitions remotely

During acquisition the BakingTray preview tile scan shows the longest-wavelength detection channel only. This is channel that is used for the auto-ROI detection algorithm. The preview takes a little time to build and there is no point slowing down the acquisition to build images locally that nobody is going to look at. Instead, you will monitor acquisitions using the web preview feature of `syncAndCrunch`. This allows you to send images of the last completed section to a website. A [demo of the site can be found here](https://brainsaw.org/livefeed/demo), see also screenshot below. So start streaming to the web, contact Rob at SWC AMF for details on how to set up the tool.

![Web preview for a site with three microscopes](https://326914071-files.gitbook.io/~/files/v0/b/gitbook-x-prod.appspot.com/o/spaces%2Fl1mUe85omq3F87GCTSYT%2Fuploads%2Fgit-blob-0259e715c8f1f4ebfea3130db0b938da51eb12e6%2Fweb_preview_screenshot.png?alt=media)
