Metadata-Version: 2.4
Name: TESSify
Version: 0.6.9
Summary: A Python package for detecting exoplanet transit dips in TESS light curves.
Author-email: Arpit Bishnoi <bishnoiarpit29@gmail.com>
License: MIT License
        
        Copyright (c) 2025 arpit290
        
        Permission is hereby granted, free of charge, to any person obtaining a copy
        of this software and associated documentation files (the "Software"), to deal
        in the Software without restriction, including without limitation the rights
        to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
        copies of the Software, and to permit persons to whom the Software is
        furnished to do so, subject to the following conditions:
        
        The above copyright notice and this permission notice shall be included in all
        copies or substantial portions of the Software.
        
        THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
        IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
        FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
        AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
        LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
        OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
        SOFTWARE.
        
Requires-Python: >=3.10
Description-Content-Type: text/markdown
License-File: LICENSE
Requires-Dist: lightkurve>=2.0
Requires-Dist: matplotlib>=3.5
Requires-Dist: pillow>=9.0
Dynamic: license-file

# TESSify
A Python package for helping with detecting exoplanet transit dips in TESS light curves.

---

## 🚀 Features

- Load and clean `.fits` light curve or target pixel files
- Visualize flattened light curves and shortlist transit candidates
- Tune the flattening window and the pixel selection (`window_length`, `aperture_mask`) to suit your targets
- Records the lowest and highest relative brightness of every processed file, so graphs can be filtered by it
- Download and process data in bulk, with parallel downloads and optional multi-core processing
- Resumable at every stage: stop at any time (even with Ctrl-C) and pick up where you left off
- Failed downloads and plots are recorded and retried on the next run instead of crashing the batch
- Designed for students, researchers, and citizen scientists

---

## 📦 Installation

Requires **Python 3.9 or newer**.

```bash
pip install TESSify
```

Or install the dependencies and run from source:

```bash
pip install -r requirements.txt
```

Two things pip can't install for you:

- **`curl`** must be available on your PATH. It is used to run the bulk download scripts. It ships with Windows 10+, macOS, and most Linux distributions.
- **`tkinter`** is needed for the image viewer. It is bundled with the Windows and macOS Python installers. On Debian/Ubuntu, run `sudo apt install python3-tk`. Without it, downloading and processing still work; only `finalise()` needs it.

---

## 🛠️ Usage

The typical workflow is: **create → download → process → finalise → get results**.

### Importing TESSify

```python
from TESSify import Project
```

### Creating a Project

```python
a = Project()
a.create("YOUR_PROJECT_NAME")
```

This makes a new folder with that name. `create` refuses to overwrite a project that already exists.

### Restoring a Project

```python
a = Project()
a.restore("YOUR_PROJECT_NAME")
```

### Downloading `.fits` files in bulk

Get a bulk download script from the official [TESS archive](https://archive.stsci.edu/tess/bulk_downloads/bulk_downloads_ffi-tp-lc-dv.html). You can download either Target Pixel (`tp`) or Light Curve (`lc`) files. After downloading the script, copy its path.

```python
from TESSify import Project

a = Project()
a.create("Project1")

a.download("lc", r"C:\Users\Expert\Downloads\tesscurl_sector_89_lc.sh", 100)
# 1st argument: "lc" for light curves, "tp" for target pixel files
# 2nd argument: path to the download script
# 3rd argument: how many files to download
```

Optional: download several files at once (default is 4).

```python
a.download("lc", "tesscurl_sector_89_lc.sh", 100, workers=8)
```

Notes:

- Running `download` again continues where it stopped: files that are already downloaded are skipped, and files that failed are retried.
- To fetch more files from the same script, call `download` again with a larger amount.
- A project holds one file type. To work with both `lc` and `tp` files, create two projects.

### Processing the files in bulk

Processing cleans each file (removes NaNs, flattens the curve) and saves a plot of it in the project's `Graphs` folder.

```python
from TESSify import Project

a = Project()
a.restore("Project1")

a.process(100)
# The amount can't usefully exceed the number of files you have downloaded
```

Optional: use several CPU cores (default is 1).

```python
a.process(100, workers=4)
```

> **Windows/macOS users:** when using `workers` greater than 1, put your script's top-level code under `if __name__ == "__main__":`. This is the standard rule for Python multiprocessing. Each worker also loads its own copy of the data, so keep `workers` modest if you are short on memory.

Files that can't be read (for example, a corrupt download) are skipped, reported in the console, and listed under `process_failed` in `config.json`. Re-running `process` retries them.

#### Customising the processing

Two optional settings control how each file is cleaned:

```python
a.process(100, window_length=401, aperture_mask="pipeline")
```

| Setting | Default | Applies to | What it does |
|---------|---------|------------|--------------|
| `window_length` | `101` | `lc` and `tp` | Size, in data points, of the smoothing window `flatten()` uses to remove slow brightness drift. Must be an odd integer of 3 or more. |
| `aperture_mask` | `"all"` | `tp` only | Which pixels are added together to build a light curve from a target pixel file. Ignored for `lc` files. |

**`window_length`:** the flattening filter smooths away anything shorter than its window, and a transit is a short dip. At the default of 101 points (about 3.4 hours for 2-minute data), a long transit can be partly flattened away. Use a window several times longer than the transits you are looking for, for example `401` or `1001` for 2-minute data. If the window is too large, slow stellar variability stays in the curve; if it is too small, real dips get erased. Longer cadences (10 or 30 minutes) need fewer points to cover the same time span.

**`aperture_mask`:** `"all"` adds up every pixel in the cutout, including faint, noisy ones. Other options are `"pipeline"` (the pixels chosen by the TESS mission pipeline, usually cleaner) and `"threshold"` (only pixels above a brightness threshold). If your target pixel plots look noisy, try `"pipeline"`.

Files that already have a plot are skipped, so changing these settings only affects files processed afterwards (the one exception is described under [Brightness range](#brightness-range)). To re-plot everything with new settings, delete the images in `Graphs/`, then set `"processed"` to `[]` and `"reviewed"` to `0` in `config.json`. Your shortlist (`"marked"`) is kept.

To process a single file and get the image name back (it accepts the same settings):

```python
image = a.indprocess("tess2019198215352-s0014-0000000261136679-0150-s_lc.fits", window_length=401)
```

#### Brightness range

Whenever a file is processed, TESSify also records the **lowest and highest relative brightness** of its light curve in `config.json`, under `brightness_range`, next to the rest of the project's progress. Entries are keyed by image name, like `marked`:

```json
"brightness_range": {
  "tess2019198215352-s0014-0000000261136679-0150-s_lc.jpeg": [0.9951, 1.0032]
}
```

Relative brightness is the flattened flux, which is what the y-axis of the graph shows: `1.0` is the star's typical brightness, so `0.9951` is about 0.5% fainter. The two numbers are the plain minimum and maximum of the plotted curve, so a single outlier (a flare, a bad data point) moves them. This works the same for `lc` and `tp` files.

```python
a.get_brightness_ranges()
# {'tess2019198215352-s0014-0000000261136679-0150-s_lc.jpeg': (0.9951, 1.0032), ...}
```

Projects processed by an older version have no ranges yet. Run `process` again to fill them in: a file that already has a plot is read once more to measure its range, but **its plot is not regenerated**, so your review decisions stay valid. The measurement uses the `window_length` and `aperture_mask` you pass to that call, so pass the same values you used originally if the range should match the existing plot exactly. Files whose `.fits` file has since been deleted are skipped.

### Finalising the light curves and watching out for transit dips

After processing, you can flip through the graphs and shortlist any that show dips that could indicate an exoplanet.

```python
from TESSify import Project

a = Project()
a.restore("Project1")

a.finalise(100)
# Opens a window showing the processed graphs
```

Viewer controls:

| Key | Action |
|-----|--------|
| **Right arrow** | Next graph |
| **Left arrow** | Previous graph |
| **Down arrow** | Shortlist the current graph (if you see something of interest) |
| **Up arrow** | Remove the current graph from the shortlist |
| **Escape** (or closing the window) | Save and exit |

The title bar shows your position and whether the current graph is shortlisted. Your shortlist is saved as you go, and the next call to `finalise` starts after the last graph you reached.

### Getting the list of shortlisted entities

```python
from TESSify import Project

a = Project()
a.restore("Project1")

finalised_list = a.getresults()
# Returns unique TIC IDs in the order you shortlisted them

print(finalised_list)
# ['TIC 261136679', 'TIC 25155310']
```

### Saving the shortlisted entities as a JSON file

```python
from TESSify import Project

a = Project()
a.restore("Project1")

a.saveresults("shortlist.json")
# Any path works, e.g. r"C:\Users\Expert\Documents\shortlist.json" or "/home/expert/shortlist.json"
```

---

## 📁 Project layout

```
YOUR_PROJECT_NAME/
├── config.json       # progress for every stage, plus each graph's brightness range (safe to leave alone)
├── LightCurves/      # downloaded .fits light curves (type "lc")
├── TargetPixels/     # downloaded .fits target pixel files (type "tp")
└── Graphs/           # plots made by process()
```

`config.json` is written safely, so a crash can't leave it half-written. Projects created with older versions of TESSify load automatically. Re-run `process` on them, because plot names created by older versions may not match.

---

## ❓ Troubleshooting

- **`FileExistsError` on `create`:** a folder with that name already exists. Use `restore` instead, or pick a new name.
- **`FileNotFoundError` on `restore`:** the folder doesn't contain a `config.json`. Check the name and your working directory, since projects are found relative to where you run Python.
- **Downloads show `FAILED`:** check your connection and that `curl` is installed, then re-run `download`. Failed files are retried automatically.
- **`The viewer needs tkinter and Pillow`:** install them as described under [Installation](#-installation).

---

## 🧠 Contributing
Contributions are welcome! Open an issue or submit a pull request at [github.com/arpit290/TESSify](https://github.com/arpit290/TESSify).

---

## 📜 License
This project is licensed under the MIT License. See `LICENSE` for details.

---

## 👤 Author
Arpit Bishnoi
[github.com/arpit290](https://github.com/arpit290) · bishnoiarpit29@gmail.com
