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
59 changes: 41 additions & 18 deletions medcat-trainer/client/mctclient.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,8 +65,6 @@ class MCTConceptDB(MCTObj):

def __post_init__(self):
if self.name is not None:
if not self.name[0].islower():
raise ValueError("Name must start with a lowercase letter")
if not self.name.replace('_', '').replace('-', '').isalnum():
raise ValueError("Name must contain only alphanumeric characters and underscores")

Expand All @@ -90,26 +88,24 @@ def __str__(self):


@dataclass
class MCTModelPack(MCTObj):
"""A model pack in the MedCATTrainer instance.
class MCTMetaTask(MCTObj):
"""A meta task in the MedCATTrainer instance.

Attributes:
name (str): The name of the model pack.
model_pack_zip (str): The path to the model pack zip file, should be a <modelpack_name>.zip file.
name (str): The name of the meta task.
"""
name: str=None
model_pack_zip: str=None

def __str__(self):
return f'{self.id} : {self.name} \t {self. model_pack_zip}'
return f'{self.id} : {self.name}'


@dataclass
class MCTMetaTask(MCTObj):
"""A meta task in the MedCATTrainer instance.
class MCTRelTask(MCTObj):
"""A relation extraction task in the MedCATTrainer instance.

Attributes:
name (str): The name of the meta task.
name (str): The name of the relation extraction task.
"""
name: str=None

Expand All @@ -118,16 +114,22 @@ def __str__(self):


@dataclass
class MCTRelTask(MCTObj):
"""A relation extraction task in the MedCATTrainer instance.
class MCTModelPack(MCTObj):
"""A model pack in the MedCATTrainer instance.

Attributes:
name (str): The name of the relation extraction task.
name (str): The name of the model pack.
model_pack_zip (str): The path to the model pack zip file, should be a <modelpack_name>.zip file.
"""
name: str=None
model_pack_zip: str=None
concept_db: MCTConceptDB=None
vocab: MCTVocab=None
meta_cats: List[MCTMetaTask]=None

def __str__(self):
return f'{self.id} : {self.name}'
return f'{self.id} : {self.name} \t {self. model_pack_zip}'



@dataclass
Expand Down Expand Up @@ -520,7 +522,11 @@ def get_model_packs(self) -> List[MCTModelPack]:
List[MCTModelPack]: A list of all MedCAT model packs in the MedCATTrainer instance
"""
resp = json.loads(requests.get(f'{self.server}/api/modelpacks/', headers=self.headers).text)['results']
mct_model_packs = [MCTModelPack(id=mp['id'], name=mp['name'], model_pack_zip=mp['model_pack']) for mp in resp]
mct_model_packs = [MCTModelPack(id=mp['id'], name=mp['name'], model_pack_zip=mp['model_pack'],
concept_db=MCTConceptDB(id=mp['concept_db']),
vocab=MCTVocab(id=mp['vocab']),
meta_cats=[MCTMetaTask(id=mt) for mt in mp['meta_cats']])
for mp in resp]
return mct_model_packs

def get_meta_tasks(self) -> List[MCTMetaTask]:
Expand Down Expand Up @@ -595,24 +601,41 @@ def get_project_annos(self, projects: List[MCTProject]):
def upload_projects_export(self, projects: Dict[str, Any],
cdb: Union[MCTConceptDB, str]=None,
vocab: Union[MCTVocab, str]=None,
modelpack: Union[MCTModelPack, str]=None):
modelpack: Union[MCTModelPack, str]=None,
import_project_name_suffix: str=' IMPORTED',
cdb_search_filter: Union[MCTConceptDB, str]=None,
members: Union[List[MCTUser], List[str]]=None,
set_validated_docs: bool=False):
"""Upload Trainer export as a list of projects to a MedCATTrainer instance.

Args:
projects (List[MCTProject]): A list of projects to upload
cdb (Union[MCTConceptDB, str]): The concept database to be used in the project - CDB name or the MCTCDB Object
vocab (Union[MCTVocab, str]): The vocabulary to be used in the project - Vocab name or the MCTVocab Object
modelpack (Union[MCTModelPack, str]): The model pack to be used in the project - ModelPack name or the MCTModelPack Object
import_project_name_suffix (str): The suffix to be added to the project name
cdb_search_filter (Union[MCTConceptDB, str]): The concept database to be used in the project - CDB name or the MCTCDB Object
members (Union[List[MCTUser], List[str]]): The annotators for the project - List of MCTUser objects or list of user names
set_validated_docs (bool): Whether to set the validated documents, e.g. their annotation submit status.
"""
if isinstance(cdb, str):
cdb = [c for c in self.get_concept_dbs() if c.name == cdb].pop()
if isinstance(vocab, str):
vocab = [v for v in self.get_vocabs() if v.name == vocab].pop()
if isinstance(modelpack, str):
modelpack = [m for m in self.get_model_packs() if m.name == modelpack].pop()
if isinstance(cdb_search_filter, str):
cdb_search_filter = [c for c in self.get_concept_dbs() if c.name == cdb_search_filter].pop()
if members and all(isinstance(m, str) for m in members):
members = [m for m in self.get_users() if m.username in members]

payload = {
'exported_projects': projects
'exported_projects': projects,
'project_name_suffix': import_project_name_suffix,
'cdb_search_filter': cdb_search_filter.id if cdb_search_filter else None,
'members': [m.id for m in members] if members else None,
'import_project_name_suffix': import_project_name_suffix,
'set_validated_docs': set_validated_docs,
}

if cdb and vocab:
Expand Down
Loading