Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 13 additions & 4 deletions unilabos/compile/adjustph_protocol.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import networkx as nx
import logging
from typing import List, Dict, Any
from typing import List, Dict, Any, Union
from .pump_protocol import generate_pump_protocol_with_rinsing

logger = logging.getLogger(__name__)
Expand Down Expand Up @@ -216,7 +216,7 @@ def calculate_reagent_volume(target_ph_value: float, reagent: str, vessel_volume

def generate_adjust_ph_protocol(
G: nx.DiGraph,
vessel: dict, # 🔧 修改:从字符串改为字典类型
vessel:Union[dict,str], # 🔧 修改:从字符串改为字典类型
ph_value: float,
reagent: str,
**kwargs
Expand All @@ -235,8 +235,17 @@ def generate_adjust_ph_protocol(
List[Dict[str, Any]]: 动作序列
"""

# 🔧 核心修改:从字典中提取容器ID
vessel_id = vessel["id"]
# 统一处理vessel参数
if isinstance(vessel, dict):
vessel_id = list(vessel.values())[0].get("id", "")
vessel_data = vessel.get("data", {})
else:
vessel_id = str(vessel)
vessel_data = G.nodes[vessel_id].get("data", {}) if vessel_id in G.nodes() else {}

if not vessel_id:
debug_print(f"❌ vessel 参数无效,必须包含id字段或直接提供容器ID. vessel: {vessel}")
raise ValueError("vessel 参数无效,必须包含id字段或直接提供容器ID")

debug_print("=" * 60)
debug_print("🧪 开始生成pH调节协议")
Expand Down
18 changes: 9 additions & 9 deletions unilabos/compile/evaporate_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ def parse_time_input(time_input: Union[str, float]) -> float:
"""
if isinstance(time_input, (int, float)):
debug_print(f"⏱️ 时间输入为数字: {time_input}s ✨")
return float(time_input)
return float(time_input) # 🔧 确保返回float

if not time_input or not str(time_input).strip():
debug_print(f"⚠️ 时间输入为空,使用默认值: 180s (3分钟) 🕐")
Expand All @@ -48,7 +48,7 @@ def parse_time_input(time_input: Union[str, float]) -> float:
try:
value = float(time_str)
debug_print(f"✅ 时间解析成功: {time_str} → {value}s(无单位,默认秒)⏰")
return value
return float(value) # 🔧 确保返回float
except ValueError:
debug_print(f"❌ 无法解析时间: '{time_str}',使用默认值180s (3分钟) 😅")
return 180.0
Expand All @@ -70,7 +70,7 @@ def parse_time_input(time_input: Union[str, float]) -> float:
time_sec = value # 已经是s
debug_print(f"🕐 时间转换: {value}s → {time_sec}s (已是秒) ⏰")

return time_sec
return float(time_sec) # 🔧 确保返回float

def find_rotavap_device(G: nx.DiGraph, vessel: str = None) -> Optional[str]:
"""
Expand Down Expand Up @@ -389,12 +389,12 @@ def generate_evaporate_protocol(
"device_id": rotavap_device,
"action_name": "evaporate",
"action_kwargs": {
"vessel": target_vessel, # 使用 target_vessel
"pressure": pressure,
"temp": temp,
"time": final_time,
"stir_speed": stir_speed,
"solvent": solvent
"vessel": target_vessel,
"pressure": float(pressure),
"temp": float(temp),
"time": float(final_time), # 🔧 强制转换为float类型
"stir_speed": float(stir_speed),
"solvent": str(solvent)
}
}
action_sequence.append(evaporate_action)
Expand Down
77 changes: 69 additions & 8 deletions unilabos/compile/recrystallize_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -170,33 +170,94 @@ def find_solvent_vessel(G: nx.DiGraph, solvent: str) -> str:
debug_print(f" 🎉 通过名称匹配找到容器: {vessel_name} ✨")
return vessel_name

# 第二步:通过模糊匹配
# 第二步:通过模糊匹配(节点ID和名称)
debug_print(" 🔍 步骤2: 模糊名称匹配...")
for node_id in G.nodes():
if G.nodes[node_id].get('type') == 'container':
node_name = G.nodes[node_id].get('name', '').lower()

if solvent.lower() in node_id.lower() or solvent.lower() in node_name:
debug_print(f" 🎉 通过模糊匹配找到容器: {node_id} ✨")
debug_print(f" 🎉 通过模糊匹配找到容器: {node_id} (名称: {node_name}) ✨")
return node_id

# 第三步:通过液体类型匹配
debug_print(" 🧪 步骤3: 液体类型匹配...")
# 第三步:通过配置中的试剂信息匹配
debug_print(" 🧪 步骤3: 配置试剂信息匹配...")
for node_id in G.nodes():
if G.nodes[node_id].get('type') == 'container':
# 检查 config 中的 reagent 字段
node_config = G.nodes[node_id].get('config', {})
config_reagent = node_config.get('reagent', '').lower()

if config_reagent and solvent.lower() == config_reagent:
debug_print(f" 🎉 通过config.reagent匹配找到容器: {node_id} (试剂: {config_reagent}) ✨")
return node_id

# 第四步:通过数据中的试剂信息匹配
debug_print(" 🧪 步骤4: 数据试剂信息匹配...")
for node_id in G.nodes():
if G.nodes[node_id].get('type') == 'container':
vessel_data = G.nodes[node_id].get('data', {})
liquids = vessel_data.get('liquid', [])

# 检查 data 中的 reagent_name 字段
reagent_name = vessel_data.get('reagent_name', '').lower()
if reagent_name and solvent.lower() == reagent_name:
debug_print(f" 🎉 通过data.reagent_name匹配找到容器: {node_id} (试剂: {reagent_name}) ✨")
return node_id

# 检查 data 中的液体信息
liquids = vessel_data.get('liquid', [])
for liquid in liquids:
if isinstance(liquid, dict):
liquid_type = (liquid.get('liquid_type') or liquid.get('name', '')).lower()
reagent_name = vessel_data.get('reagent_name', '').lower()

if solvent.lower() in liquid_type or solvent.lower() in reagent_name:
debug_print(f" 🎉 通过液体类型匹配找到容器: {node_id} ✨")
if solvent.lower() in liquid_type:
debug_print(f" 🎉 通过液体类型匹配找到容器: {node_id} (液体类型: {liquid_type}) ✨")
return node_id

# 第五步:部分匹配(如果前面都没找到)
debug_print(" 🔍 步骤5: 部分匹配...")
for node_id in G.nodes():
if G.nodes[node_id].get('type') == 'container':
node_config = G.nodes[node_id].get('config', {})
node_data = G.nodes[node_id].get('data', {})
node_name = G.nodes[node_id].get('name', '').lower()

config_reagent = node_config.get('reagent', '').lower()
data_reagent = node_data.get('reagent_name', '').lower()

# 检查是否包含溶剂名称
if (solvent.lower() in config_reagent or
solvent.lower() in data_reagent or
solvent.lower() in node_name or
solvent.lower() in node_id.lower()):
debug_print(f" 🎉 通过部分匹配找到容器: {node_id} ✨")
debug_print(f" - 节点名称: {node_name}")
debug_print(f" - 配置试剂: {config_reagent}")
debug_print(f" - 数据试剂: {data_reagent}")
return node_id

# 调试信息:列出所有容器
debug_print(" 🔎 调试信息:列出所有容器...")
container_list = []
for node_id in G.nodes():
if G.nodes[node_id].get('type') == 'container':
node_config = G.nodes[node_id].get('config', {})
node_data = G.nodes[node_id].get('data', {})
node_name = G.nodes[node_id].get('name', '')

container_info = {
'id': node_id,
'name': node_name,
'config_reagent': node_config.get('reagent', ''),
'data_reagent': node_data.get('reagent_name', '')
}
container_list.append(container_info)
debug_print(f" - 容器: {node_id}, 名称: {node_name}, config试剂: {node_config.get('reagent', '')}, data试剂: {node_data.get('reagent_name', '')}")

debug_print(f"❌ 找不到溶剂 '{solvent}' 对应的容器 😭")
debug_print(f"🔍 查找的溶剂: '{solvent}' (小写: '{solvent.lower()}')")
debug_print(f"📊 总共发现 {len(container_list)} 个容器")

raise ValueError(f"找不到溶剂 '{solvent}' 对应的容器")


Expand Down
2 changes: 1 addition & 1 deletion unilabos/compile/stir_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ def extract_vessel_id(vessel: Union[str, dict]) -> str:
str: vessel_id
"""
if isinstance(vessel, dict):
vessel_id = vessel.get("id", "")
vessel_id = list(vessel.values())[0].get("id", "")
debug_print(f"🔧 从vessel字典提取ID: {vessel_id}")
return vessel_id
elif isinstance(vessel, str):
Expand Down
2 changes: 1 addition & 1 deletion unilabos/compile/wash_solid_protocol.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,7 +165,7 @@ def extract_vessel_id(vessel: Union[str, dict]) -> str:
str: vessel_id
"""
if isinstance(vessel, dict):
vessel_id = vessel.get("id", "")
vessel_id = list(vessel.values())[0].get("id", "")
debug_print(f"🔧 从vessel字典提取ID: {vessel_id}")
return vessel_id
elif isinstance(vessel, str):
Expand Down
25 changes: 23 additions & 2 deletions unilabos/devices/virtual/virtual_multiway_valve.py
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,32 @@ def set_position(self, command: Union[int, str]):
command: 目标位置 (0-8) 或位置字符串
0: transfer pump位置
1-8: 其他设备位置
'default': 默认位置(0号位)
"""
try:
# 如果是字符串形式的位置,先转换为数字
# 🔧 处理特殊字符串命令
if isinstance(command, str):
pos = int(command)
command_lower = command.lower().strip()

# 处理特殊命令
if command_lower in ['default', 'pump', 'transfer_pump', 'home']:
pos = 0 # 默认位置为0号位(transfer pump)
self.logger.info(f"🔧 特殊命令 '{command}' 映射到位置 {pos}")
elif command_lower in ['open']:
pos = 0 # open命令也映射到0号位
self.logger.info(f"🔧 OPEN命令映射到位置 {pos}")
elif command_lower in ['close', 'closed']:
# 关闭命令保持当前位置
pos = self._current_position
self.logger.info(f"🔧 CLOSE命令保持当前位置 {pos}")
else:
# 尝试转换为数字
try:
pos = int(command)
except ValueError:
error_msg = f"无法识别的命令: '{command}'"
self.logger.error(f"❌ {error_msg}")
raise ValueError(error_msg)
else:
pos = int(command)

Expand Down
16 changes: 15 additions & 1 deletion unilabos/devices/virtual/virtual_rotavap.py
Original file line number Diff line number Diff line change
Expand Up @@ -88,6 +88,20 @@ async def evaporate(
) -> bool:
"""Execute evaporate action - 简化版 🌪️"""

# 🔧 新增:确保time参数是数值类型
if isinstance(time, str):
try:
time = float(time)
except ValueError:
self.logger.error(f"❌ 无法转换时间参数 '{time}' 为数值,使用默认值180.0秒")
time = 180.0
elif not isinstance(time, (int, float)):
self.logger.error(f"❌ 时间参数类型无效: {type(time)},使用默认值180.0秒")
time = 180.0

# 确保time是float类型
time = float(time)

# 🔧 简化处理:如果vessel就是设备自己,直接操作
if vessel == self.device_id:
debug_print(f"🎯 在设备 {self.device_id} 上直接执行蒸发操作")
Expand Down Expand Up @@ -158,7 +172,7 @@ async def evaporate(
})
return False

# 开始蒸发
# 开始蒸发 - 🔧 现在time已经确保是float类型
self.logger.info(f"🚀 启动蒸发程序! 预计用时 {time/60:.1f}分钟 ⏱️")

self.data.update({
Expand Down
Loading
Loading