当前位置: 首页 > news >正文

scanpy赋值问题

今天发现一个很奇怪的bug


import numpy as np
import pandas as pd
import anndata as ad
from scipy.sparse import csr_matrix
print(ad.__version__)counts = csr_matrix(np.random.poisson(1, size=(100, 2000)), dtype=np.float32)
adata1 = ad.AnnData(counts)
print(adata1)def f(adata):adata = adata[:,0:1] # print(adata.shape)f(adata1)
print(adata1.shape)

结果如下
在这里插入图片描述
可以看到在函数中,这个adata的结果是变化了,但是并没有改变外部adata的值


import numpy as np
import pandas as pd
import anndata as ad
from scipy.sparse import csr_matrix
print(ad.__version__)counts = csr_matrix(np.random.poisson(1, size=(100, 2000)), dtype=np.float32)
adata1 = ad.AnnData(counts)
print(adata1.X[0:2,0:10])def f(adata):adata = adata[:,0:1] # print(adata.shape)f(adata1)
print(adata1.shape)
print(adata1.X[0:2,0:10])

在这里插入图片描述

但是如果一开始我不在函数中操作,而是主程序中,这个结果


import numpy as np
import pandas as pd
import anndata as ad
from scipy.sparse import csr_matrix
print(ad.__version__)counts = csr_matrix(np.random.poisson(1, size=(100, 2000)), dtype=np.float32)
adata1 = ad.AnnData(counts)
print(adata1.X.shape)adata1 = adata1[:,0:1]
print(adata1.shape)

结果如下
在这里插入图片描述
这个现象只能解释为adata= adata1[:,0:1]是一个复制的行为,只不过同名了,所以adata的饮用变了,如果

adata2 = adata1[:,0:1],

可以想象,这个结果不会对adata1结果有影响

这仅仅是一个简简单单的例子,下面有一个更奇怪的测试

import scanpy as sc adata= sc.read("/Users/yxk/Desktop/test_dataset/pbmc/pbmc.h5ad")
adata.obs["BATCH"] = adata.obs["batch"].copy()
adata.obs["label"]=adata.obs["celltype"].astype("category").cat.codes 
n_classes= len(adata.obs["label"].value_counts())
print(adata)adata1= adata[adata.obs["batch"]=="pbmc_3p"].copy()
adata2= adata[adata.obs["batch"]=="pbmc_5p"].copy()
#print(adata1.X)
#print(adata2.X)## 如果用这种方式,我的结果是这样的
def preprocessNew(adata_A_input, ):'''Performing preprocess for a pair of datasets.To integrate multiple datasets, use function preprocess_multiple_anndata in utils.py'''adata_A = adata_A_inputprint("Finding highly variable genes...")#sc.pp.highly_variable_genes(adata_A, flavor='seurat_v3', n_top_genes=2000)#hvg_A = adata_A.var[adata_A.var.highly_variable == True].sort_values(by="highly_variable_rank").indexprint("Normalizing and scaling...")sc.pp.normalize_total(adata_A, target_sum=1e4)sc.pp.log1p(adata_A)sc.pp.highly_variable_genes(adata_A,n_top_genes=2000)hvg_A = list(adata1.var_names[adata1.var.highly_variable])adata_A = adata_A[:, hvg_A]sc.pp.scale(adata_A, max_value=10)print(adata_A.X[0:1,0:100])print(adata_A.X.shape)# 为啥这些结果是这样的preprocessNew(adata1)
print(adata1.X.shape)

在这里插入图片描述可以看到adata的结果是没有改变的,还是33694维,但是我在函数中,明明是选择了高变基因的

但是如果采用下面的代码

import scanpy as sc adata= sc.read("/Users/yxk/Desktop/test_dataset/pbmc/pbmc.h5ad")
adata.obs["BATCH"] = adata.obs["batch"].copy()
adata.obs["label"]=adata.obs["celltype"].astype("category").cat.codes 
n_classes= len(adata.obs["label"].value_counts())
print(adata)adata1= adata[adata.obs["batch"]=="pbmc_3p"].copy()
adata2= adata[adata.obs["batch"]=="pbmc_5p"].copy()
#print(adata1.X)
#print(adata2.X)def preprocessNew(adata_A_input, ):'''Performing preprocess for a pair of datasets.To integrate multiple datasets, use function preprocess_multiple_anndata in utils.py'''adata_A = adata_A_inputprint("Finding highly variable genes...")#sc.pp.highly_variable_genes(adata_A, flavor='seurat_v3', n_top_genes=2000)#hvg_A = adata_A.var[adata_A.var.highly_variable == True].sort_values(by="highly_variable_rank").indexprint("Normalizing and scaling...")sc.pp.normalize_total(adata_A, target_sum=1e4)sc.pp.log1p(adata_A)sc.pp.highly_variable_genes(adata_A,n_top_genes=2000,subset=True)#adata_A = adata_A[:, hvg_A]sc.pp.scale(adata_A, max_value=10)print(adata_A.X[0:1,0:100])
preprocessNew(adata1)
print(adata1.X.shape)
print(adata1.X[0:1,0:100])
## 但是线则这个问题为啥不是

结果如下
在这里插入图片描述
这里可以看到,我最终的adata1的维度是改变了,这里需要注意

这里使用
sc.pp.highly_variable_genes(adata1,n_top_genes=2000,subset=True),就是对adata的引用改动了,最终导致最开始的atata出现了变化,反正最好还是用scanpy的内置函数了,一旦在函数里赋值就要注意局部对象的问题

import scanpy as sc adata= sc.read("/Users/yxk/Desktop/test_dataset/pbmc/pbmc.h5ad")
adata.obs["BATCH"] = adata.obs["batch"].copy()
adata.obs["label"]=adata.obs["celltype"].astype("category").cat.codes 
n_classes= len(adata.obs["label"].value_counts())
print(adata)
adata1= adata[adata.obs["batch"]=="pbmc_3p"].copy()
#adata2= adata[adata.obs["batch"]=="pbmc_5p"].copy()
#print(adata1.X)
#print(adata2.X)
print("Normalizing and scaling...")
sc.pp.normalize_total(adata1, target_sum=1e4)
sc.pp.log1p(adata1)
sc.pp.highly_variable_genes(adata1,n_top_genes=2000,subset=True)
sc.pp.scale(adata1, max_value=10)
print(adata1.X[0:1,0:100])
print(adata1.X.shape)
print(adata1.X[0:1,0:100])
## 但是线则这个问题为啥不是

如果采用了preprocessNew的函数,那么本质上只对adata做了如下变化

import scanpy as sc adata= sc.read("/Users/yxk/Desktop/test_dataset/pbmc/pbmc.h5ad")
adata.obs["BATCH"] = adata.obs["batch"].copy()
adata.obs["label"]=adata.obs["celltype"].astype("category").cat.codes 
n_classes= len(adata.obs["label"].value_counts())
print(adata)adata1= adata[adata.obs["batch"]=="pbmc_3p"].copy()
#adata2= adata[adata.obs["batch"]=="pbmc_5p"].copy()
#print(adata1.X)
#print(adata2.X)## 如果用这种方式,我的结果是这样的
def preprocessNew(adata_A_input, ):'''Performing preprocess for a pair of datasets.To integrate multiple datasets, use function preprocess_multiple_anndata in utils.py'''adata_A = adata_A_inputprint("Finding highly variable genes...")#sc.pp.highly_variable_genes(adata_A, flavor='seurat_v3', n_top_genes=2000)#hvg_A = adata_A.var[adata_A.var.highly_variable == True].sort_values(by="highly_variable_rank").indexprint("Normalizing and scaling...")sc.pp.normalize_total(adata_A, target_sum=1e4)sc.pp.log1p(adata_A)sc.pp.highly_variable_genes(adata_A,n_top_genes=2000)hvg_A = list(adata1.var_names[adata1.var.highly_variable])adata_A = adata_A[:, hvg_A]sc.pp.scale(adata_A, max_value=10)print(adata_A.X[0:1,0:100])print(adata_A.X.shape)# 为啥这些结果是这样的preprocessNew(adata1)
print(adata1.X.shape)
print(adata1.X[0:1,0:100])

结果如下
在这里插入图片描述reproduce result

import scanpy as sc adata= sc.read("/Users/yxk/Desktop/test_dataset/pbmc/pbmc.h5ad")
adata.obs["BATCH"] = adata.obs["batch"].copy()
adata.obs["label"]=adata.obs["celltype"].astype("category").cat.codes 
n_classes= len(adata.obs["label"].value_counts())
print(adata)adata2= adata[adata.obs["batch"]=="pbmc_3p"].copy()#print(adata1.X)
#print(adata2.X)## 如果用这种方式,我的结果是这样的print("Normalizing and scaling...")
sc.pp.normalize_total(adata2, target_sum=1e4)
sc.pp.log1p(adata2) # 真正对adata1只有这么多的操作# 为啥这些结果是这样的
print(adata2.X.shape)
print(adata2.X[0:1,0:100])

在这里插入图片描述

from sklearn.metrics import mean_squared_error
mean_squared_error(adata1.X.toarray(),adata2.X.toarray())

结果如下
在这里插入图片描述

相关文章:

scanpy赋值问题

今天发现一个很奇怪的bug import numpy as np import pandas as pd import anndata as ad from scipy.sparse import csr_matrix print(ad.__version__)counts csr_matrix(np.random.poisson(1, size(100, 2000)), dtypenp.float32) adata1 ad.AnnData(counts) print(adata1)…...

腾讯云域名备案后,如何解析到华为云服务器Linux宝塔面板

一、购买域名并且进行备案和解析,正常情况下,购买完域名,如果找不到去哪备案,可以在腾讯云上搜索“备案”关键词就会出现了,所以这里不做详细介绍,直接进行步骤提示: 二、申请ssl证书&#xff0…...

odoo 按钮打印pdf报表

odoo打印一般是在动作里面进行的 所以此方法可用自定义按钮进行打印 <template id"report_sale_line_packing_template"> xxx </template><template id"report_sale_line_packing"><t t-call"web.basic_layout"><t …...

用逻辑分析仪观察串口Uart数据波形

一、概述 只讨论嵌入式编程中较为常用的异步串行接口&#xff08;Universal Asynchronous Receiver/Transmitter&#xff0c; UART&#xff09;&#xff0c;TTL电平。 串口的参数一般有&#xff1a; 1.波特率&#xff0c;数据传输速率&#xff0c;单位bps&#xff08;bits per…...

数据结构-栈应用括号匹配

1、顺序栈的定义 2、顺序栈的入栈&#xff0c;出栈&#xff0c;取出栈顶元素&#xff0c;匹配判断函数 3、顺序栈的运行测试 4、实现代码 #include<iostream> using namespace std; #define OK 1 #define ERROR 0 #define OVERFLOW -2 typedef int Status; #define M…...

leetcode做题笔记209. 长度最小的子数组

给定一个含有 n 个正整数的数组和一个正整数 target 。 找出该数组中满足其总和大于等于 target 的长度最小的 连续子数组 [numsl, numsl1, ..., numsr-1, numsr] &#xff0c;并返回其长度。如果不存在符合条件的子数组&#xff0c;返回 0 。 示例 1&#xff1a; 输入&#…...

【机器学习】几种常用的机器学习调参方法

在机器学习中&#xff0c;模型的性能往往受到模型的超参数、数据的质量、特征选择等因素影响。其中&#xff0c;模型的超参数调整是模型优化中最重要的环节之一。超参数&#xff08;Hyperparameters&#xff09;在机器学习算法中需要人为设定&#xff0c;它们不能直接从训练数据…...

使用免费 FlaskAPI 部署 YOLOv8

目标检测和实例分割是计算机视觉中关键的任务&#xff0c;使计算机能够在图像和视频中识别和定位物体。YOLOv8是一种先进的、实时的目标检测系统&#xff0c;因其速度和准确性而备受欢迎。 Flask是一个轻量级的Python Web框架&#xff0c;简化了Web应用程序的开发。通过结合Fla…...

不使用屏幕在树莓派4B安装Ubuntu22.04桌面版(64位)

因为时间有限只说一下基本路径&#xff1a; 1首先安装Ubuntu22.04server版本 2设置服务器版本的SSH和WiFi 3通过服务器版本安装Ubuntu-desktop升级到Ubuntu22.04桌面版 4在桌面版上安装远程控制软件&#xff1a;xrdp; 5使用Windows自带的远程桌面连接访问Ubuntu 6完成...

Pymysql模块使用操作

一、pymysql模块安装 二、测试数据库连接 测试数据库连接.py from pymysql import Connectioncon None try:# 创建数据库连接con Connection(host"localhost",port3306,user"root",password"XXXXX")# 测试链接print(con.get_host_info())print…...

8+双疾病+WGCNA+多机器学习筛选疾病的共同靶点并验证表达

今天给同学们分享一篇双疾病WGCNA多机器学习的生信文章“Shared diagnostic genes and potential mechanism between PCOS and recurrent implantation failure revealed by integrated transcriptomic analysis and machine learning”&#xff0c;这篇文章于2023年5月16日发表…...

springboot如何获取前端请求头的值并加入ThreadLocal

依赖&#xff1a; <dependency><groupId>org.aspectj</groupId><artifactId>aspectjweaver</artifactId><version>1.9.7</version> </dependency>示例&#xff1a; public class ThreadLocalUtil {private static ThreadLoc…...

程序员想要网上接单却看花了眼?那这几个平台你可得收藏好了!

现在经济压力这么大&#xff0c;但是生活成本还在上升&#xff0c;相信大家都知道“四脚吞金兽”的威力了吧&#xff01;话虽如此&#xff0c;但是生活总得继续&#xff0c;为了家庭的和谐幸福&#xff0c;为了孩子的未来&#xff0c;不少人选择多干几份工作&#xff0c;赚点外…...

前端食堂技术周刊第 102 期:Next.js 14、Yarn 4.0、State of HTML、SEO 从 0 到 1

美味值&#xff1a;&#x1f31f;&#x1f31f;&#x1f31f;&#x1f31f;&#x1f31f; 口味&#xff1a;肥牛宽粉 食堂技术周刊仓库地址&#xff1a;https://github.com/Geekhyt/weekly 大家好&#xff0c;我是童欧巴。欢迎来到前端食堂技术周刊&#xff0c;我们先来看下…...

GPT与人类共生:解析AI助手的兴起

随着GPT模型的崭新应用&#xff0c;如百度的​1​和CSDN的​2​&#xff0c;以及AI助手的普及&#xff0c;人们开始讨论AI对就业市场和互联网公司的潜在影响。本文将探讨GPT和AI助手的共生关系&#xff0c;以及我们如何使用它们&#xff0c;以及使用的平台和动机。 GPT和AI助手…...

HTML脚本、字符实体、URL

HTML脚本&#xff1a; JavaScript 使 HTML 页面具有更强的动态和交互性。 <script> 标签用于定义客户端脚本&#xff0c;比如 JavaScript。<script> 元素既可包含脚本语句&#xff0c;也可通过 src 属性指向外部脚本文件。 JavaScript 最常用于图片操作、表单验…...

UOS安装Jenkins

一&#xff0c;环境准备 1.安装jdk 直接使用命令行&#xff08;sudo apt install -y openjdk-11-jdk&#xff09;安装jdk11 2.安装maven 参考此篇文章即可 UOS安装并配置Maven工具_uos 安装maven_蓝天下的一员的博客-CSDN博客 不过要注意这篇文章有个小错误&#xff0c;我…...

纯CSS实现卡片上绘制透明圆孔

<template><div class"dot-card-wrapper"><div class"top-wrapper"><slot name"top"></slot></div><!-->核心是下面这部分</--><div class"dot-row"><div class"left-…...

用前端框架Bootstrap的AdminLTE模板和Django实现后台首页的页面

承接博文 用前端框架Bootstrap和Django实现用户注册页面 继续开发实现 后台首页的页面。 01-下载 AdminLTE-3.1.0-rc 并解压缩 以下需要的四个文件夹及里面的文件百度网盘下载链接&#xff1a; https://pan.baidu.com/s/1QYpjOfSBJPmjmVuFZdSgFQ?pwdo9ta 下载 AdminLTE-3.1…...

Linux驱动 编译乱序和执行乱序

编译乱序 现代的高性能编译器在目标码优化上都具备对指令进行乱序优化的能力。编译器可以对访存的指令进行乱序&#xff0c;减少逻辑上不必要的访存&#xff0c;以及尽量提高Cache命中率和CPU的Load/Store单元的工作效率。 因此在打开编译器优化以后&#xff0c;看到生成的汇编…...

地震勘探——干扰波识别、井中地震时距曲线特点

目录 干扰波识别反射波地震勘探的干扰波 井中地震时距曲线特点 干扰波识别 有效波&#xff1a;可以用来解决所提出的地质任务的波&#xff1b;干扰波&#xff1a;所有妨碍辨认、追踪有效波的其他波。 地震勘探中&#xff0c;有效波和干扰波是相对的。例如&#xff0c;在反射波…...

7.4.分块查找

一.分块查找的算法思想&#xff1a; 1.实例&#xff1a; 以上述图片的顺序表为例&#xff0c; 该顺序表的数据元素从整体来看是乱序的&#xff0c;但如果把这些数据元素分成一块一块的小区间&#xff0c; 第一个区间[0,1]索引上的数据元素都是小于等于10的&#xff0c; 第二…...

使用VSCode开发Django指南

使用VSCode开发Django指南 一、概述 Django 是一个高级 Python 框架&#xff0c;专为快速、安全和可扩展的 Web 开发而设计。Django 包含对 URL 路由、页面模板和数据处理的丰富支持。 本文将创建一个简单的 Django 应用&#xff0c;其中包含三个使用通用基本模板的页面。在此…...

Prompt Tuning、P-Tuning、Prefix Tuning的区别

一、Prompt Tuning、P-Tuning、Prefix Tuning的区别 1. Prompt Tuning(提示调优) 核心思想:固定预训练模型参数,仅学习额外的连续提示向量(通常是嵌入层的一部分)。实现方式:在输入文本前添加可训练的连续向量(软提示),模型只更新这些提示参数。优势:参数量少(仅提…...

SciencePlots——绘制论文中的图片

文章目录 安装一、风格二、1 资源 安装 # 安装最新版 pip install githttps://github.com/garrettj403/SciencePlots.git# 安装稳定版 pip install SciencePlots一、风格 简单好用的深度学习论文绘图专用工具包–Science Plot 二、 1 资源 论文绘图神器来了&#xff1a;一行…...

无法与IP建立连接,未能下载VSCode服务器

如题&#xff0c;在远程连接服务器的时候突然遇到了这个提示。 查阅了一圈&#xff0c;发现是VSCode版本自动更新惹的祸&#xff01;&#xff01;&#xff01; 在VSCode的帮助->关于这里发现前几天VSCode自动更新了&#xff0c;我的版本号变成了1.100.3 才导致了远程连接出…...

可靠性+灵活性:电力载波技术在楼宇自控中的核心价值

可靠性灵活性&#xff1a;电力载波技术在楼宇自控中的核心价值 在智能楼宇的自动化控制中&#xff0c;电力载波技术&#xff08;PLC&#xff09;凭借其独特的优势&#xff0c;正成为构建高效、稳定、灵活系统的核心解决方案。它利用现有电力线路传输数据&#xff0c;无需额外布…...

在 Nginx Stream 层“改写”MQTT ngx_stream_mqtt_filter_module

1、为什么要修改 CONNECT 报文&#xff1f; 多租户隔离&#xff1a;自动为接入设备追加租户前缀&#xff0c;后端按 ClientID 拆分队列。零代码鉴权&#xff1a;将入站用户名替换为 OAuth Access-Token&#xff0c;后端 Broker 统一校验。灰度发布&#xff1a;根据 IP/地理位写…...

c++ 面试题(1)-----深度优先搜索(DFS)实现

操作系统&#xff1a;ubuntu22.04 IDE:Visual Studio Code 编程语言&#xff1a;C11 题目描述 地上有一个 m 行 n 列的方格&#xff0c;从坐标 [0,0] 起始。一个机器人可以从某一格移动到上下左右四个格子&#xff0c;但不能进入行坐标和列坐标的数位之和大于 k 的格子。 例…...

基础测试工具使用经验

背景 vtune&#xff0c;perf, nsight system等基础测试工具&#xff0c;都是用过的&#xff0c;但是没有记录&#xff0c;都逐渐忘了。所以写这篇博客总结记录一下&#xff0c;只要以后发现新的用法&#xff0c;就记得来编辑补充一下 perf 比较基础的用法&#xff1a; 先改这…...