-
Notifications
You must be signed in to change notification settings - Fork 0
Feature/save file #4
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Open
MelisCakan
wants to merge
9
commits into
develop
Choose a base branch
from
feature/save_file
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from 8 commits
Commits
Show all changes
9 commits
Select commit
Hold shift + click to select a range
70958bd
preprocess - normalization methods are added
MelisCakan 7fde7ac
added return adata to preprocess method
MelisCakan 960a6cb
function data_normalization name changed to normalize_and scale and f…
MelisCakan 4f0df53
try except block has been aded to check var name parameters
MelisCakan 503b798
save_result function has been added
MelisCakan 87f232a
Move DEMO_save_result.ipynb to src/modules and add it
happymealinthebuilding cbc791d
Removed wrong file
happymealinthebuilding 896c59f
add demo file for save_result
happymealinthebuilding 7156a6b
Merge branch 'develop' into feature/save_file
TRextabat File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,119 @@ | ||
| { | ||
| "cells": [ | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 1, | ||
| "id": "efb8299d-379f-4e0b-aaca-e342e64226ff", | ||
| "metadata": {}, | ||
| "outputs": [], | ||
| "source": [ | ||
| "import scanpy as sc\n", | ||
| "import os\n", | ||
| "\n" | ||
| ] | ||
| }, | ||
| { | ||
| "cell_type": "code", | ||
| "execution_count": 3, | ||
| "id": "da42d05f-266b-4610-9fcf-33c1006f24c7", | ||
| "metadata": {}, | ||
| "outputs": [ | ||
| { | ||
| "data": { | ||
| "application/vnd.jupyter.widget-view+json": { | ||
| "model_id": "61f523d945954c429e3152c8b60686bf", | ||
| "version_major": 2, | ||
| "version_minor": 0 | ||
| }, | ||
| "text/plain": [ | ||
| " 0%| | 0.00/5.58M [00:00<?, ?B/s]" | ||
| ] | ||
| }, | ||
| "metadata": {}, | ||
| "output_type": "display_data" | ||
| }, | ||
| { | ||
| "name": "stdout", | ||
| "output_type": "stream", | ||
| "text": [ | ||
| "Saving AnnData object to: pbmc3k_processed.h5ad\n", | ||
| "AnnData object saved successfully to: pbmc3k_processed.h5ad\n", | ||
| "\n", | ||
| "Verification: The file 'pbmc3k_processed.h5ad' has been created.\n" | ||
| ] | ||
| } | ||
| ], | ||
| "source": [ | ||
| "def demo_save_result():\n", | ||
| " \"\"\"Demo function to save an AnnData object to a file.\"\"\"\n", | ||
| " # Load a sample dataset (or use the one preprocessed in the previous demo)\n", | ||
| " try:\n", | ||
| " adata = sc.datasets.pbmc3k()\n", | ||
| " data_normalization_for_demo(adata) # Apply minimal normalization for the demo\n", | ||
| " except Exception as e:\n", | ||
| " print(f\"Error loading sample dataset: {e}\")\n", | ||
| " print(\"Using a dummy AnnData object for the save demo.\")\n", | ||
| " import numpy as np\n", | ||
| " dummy_matrix = np.array([[1, 2], [3, 4]])\n", | ||
| " dummy_var = ['gene1', 'gene2']\n", | ||
| " dummy_obs = ['cell1', 'cell2']\n", | ||
| " adata = sc.AnnData(dummy_matrix, obs={'obs_names': dummy_obs}, var={'var_names': dummy_var})\n", | ||
| " adata.var_names = dummy_var\n", | ||
| " adata.obs_names = dummy_obs\n", | ||
| "\n", | ||
| " # Define the result file name\n", | ||
| " result_file = \"pbmc3k_processed.h5ad\" # .h5ad is the recommended format for AnnData\n", | ||
| "\n", | ||
| " print(f\"Saving AnnData object to: {result_file}\")\n", | ||
| " save_result(adata, result_file)\n", | ||
| " print(f\"AnnData object saved successfully to: {result_file}\")\n", | ||
| "\n", | ||
| " # Verify if the file was created (optional)\n", | ||
| " if os.path.exists(result_file):\n", | ||
| " print(f\"\\nVerification: The file '{result_file}' has been created.\")\n", | ||
| " else:\n", | ||
| " print(f\"\\nVerification: The file '{result_file}' was NOT created.\")\n", | ||
| "\n", | ||
| " # Clean up the created file (optional)\n", | ||
| " # os.remove(result_file)\n", | ||
| " # print(f\"Cleaned up: Removed '{result_file}'\")\n", | ||
| "\n", | ||
| "def save_result(adata, result_file):\n", | ||
| " \"\"\"Saves the AnnData object to the specified file.\"\"\"\n", | ||
| " try:\n", | ||
| " adata.write_h5ad(result_file) # Use .write_h5ad for the recommended format\n", | ||
| " except Exception as e:\n", | ||
| " print(f\"Error during saving: {e}\")\n", | ||
| "\n", | ||
| "def data_normalization_for_demo(adata, target_sum=1e4):\n", | ||
| " \"\"\"Simplified normalization for the save demo.\"\"\"\n", | ||
| " sc.pp.normalize_total(adata, target_sum=target_sum)\n", | ||
| " sc.pp.log1p(adata)\n", | ||
| "\n", | ||
| "# Run the demo\n", | ||
| "demo_save_result()" | ||
| ] | ||
| } | ||
| ], | ||
| "metadata": { | ||
| "kernelspec": { | ||
| "display_name": "Python 3 (ipykernel)", | ||
| "language": "python", | ||
| "name": "python3" | ||
| }, | ||
| "language_info": { | ||
| "codemirror_mode": { | ||
| "name": "ipython", | ||
| "version": 3 | ||
| }, | ||
| "file_extension": ".py", | ||
| "mimetype": "text/x-python", | ||
| "name": "python", | ||
| "nbconvert_exporter": "python", | ||
| "pygments_lexer": "ipython3", | ||
| "version": "3.12.4" | ||
| } | ||
| }, | ||
| "nbformat": 4, | ||
| "nbformat_minor": 5 | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,22 @@ | ||
| import scanpy as sc | ||
|
|
||
| def normalize_and_scale(adata, target_sum, max_value): | ||
| sc.pp.normalize_total(adata, target_sum) | ||
| sc.pp.log1p(adata) | ||
| sc.pp.scale(adata, max_value) | ||
|
|
||
| vars_to_check = ["total_counts", "pct_counts_mt"] | ||
| try: | ||
| for var in vars_to_check: #checking for the var names if adata has them | ||
| if var not in adata.obs.columns: | ||
| raise ValueError(f"{var} is not found in adata.var_names") | ||
|
|
||
| sc.pp.regress_out(adata, ['total_counts', 'pct_counts_mt']) #if adata has the parameters do regress out | ||
| except Exception as e: | ||
| print(f"An error has occurred: {e}") | ||
|
|
||
|
|
||
| def preprocess(adata, target_sum = 1e4, max_value = 10): | ||
| normalize_and_scale(adata, target_sum, max_value) | ||
| #other preprocessing methods here | ||
| return adata | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,2 @@ | ||
| def save_result(adata, result_file): | ||
| adata.write(result_file) |
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The error message in the following line incorrectly references 'adata.var_names' instead of 'adata.obs.columns'. Consider updating the error message to accurately reflect the column lookup.