|
3 | 3 | import json |
4 | 4 | import os |
5 | 5 | import tempfile |
6 | | -import warnings |
7 | 6 | from pathlib import Path |
8 | 7 | from typing import Callable |
9 | 8 | from typing import Iterable |
@@ -2634,3 +2633,177 @@ def get_subsets(self, project: Union[NotEmptyStr, dict]): |
2634 | 2633 | if response.errors: |
2635 | 2634 | raise AppException(response.errors) |
2636 | 2635 | return BaseSerializer.serialize_iterable(response.data, ["name"]) |
| 2636 | + |
| 2637 | + def create_custom_fields(self, project: NotEmptyStr, fields: dict): |
| 2638 | + """Create custom fields for items in a project in addition to built-in metadata. |
| 2639 | + Using this function again with a different schema won't override the existing fields, but add new ones. |
| 2640 | + Use the upload_custom_values() function to fill them with values for each item. |
| 2641 | +
|
| 2642 | + :param project: project name (e.g., “project1”) |
| 2643 | + :type project: str |
| 2644 | +
|
| 2645 | + :param fields: dictionary describing the fields and their specifications added to the project. |
| 2646 | + You can see the schema structure <here>. |
| 2647 | + :type fields: dict |
| 2648 | +
|
| 2649 | + :return: custom fields actual schema of the project |
| 2650 | + :rtype: dict |
| 2651 | +
|
| 2652 | + Supported Types: |
| 2653 | +
|
| 2654 | + ============== ====================== |
| 2655 | + number |
| 2656 | + -------------------------------------- |
| 2657 | + field spec spec value |
| 2658 | + ============== ====================== |
| 2659 | + minimum any number (int or float) |
| 2660 | + maximum any number (int or float) |
| 2661 | + enum list of numbers (int or float) |
| 2662 | + ============== ====================== |
| 2663 | +
|
| 2664 | + ============== ====================== |
| 2665 | + string |
| 2666 | + -------------------------------------- |
| 2667 | + field spec spec value |
| 2668 | + ============== ====================== |
| 2669 | + format “email” or “date” |
| 2670 | + enum list of strings |
| 2671 | + ============== ====================== |
| 2672 | + :: |
| 2673 | +
|
| 2674 | + custom_fields = { |
| 2675 | + "study_date": { |
| 2676 | + "type": "string", |
| 2677 | + "format": "date" |
| 2678 | + }, |
| 2679 | + "patient_id": { |
| 2680 | + "type": "string" |
| 2681 | + }, |
| 2682 | + "patient_sex": { |
| 2683 | + "type": "string", |
| 2684 | + "enum": [ |
| 2685 | + "male", "female" |
| 2686 | + ] |
| 2687 | + }, |
| 2688 | + "patient_age": { |
| 2689 | + "type": "number" |
| 2690 | + }, |
| 2691 | + "medical_specialist": { |
| 2692 | + "type": "string", |
| 2693 | + "format": "email" |
| 2694 | + }, |
| 2695 | + "duration": { |
| 2696 | + "type": "number", |
| 2697 | + "minimum": 10 |
| 2698 | + } |
| 2699 | + } |
| 2700 | +
|
| 2701 | + client = SAClient() |
| 2702 | + client.create_custom_fields( |
| 2703 | + project="Medical Annotations", |
| 2704 | + fields=custom_fields |
| 2705 | + ) |
| 2706 | +
|
| 2707 | + """ |
| 2708 | + project_name, _ = extract_project_folder(project) |
| 2709 | + response = self.controller.create_custom_schema( |
| 2710 | + project_name=project, schema=fields |
| 2711 | + ) |
| 2712 | + if response.errors: |
| 2713 | + raise AppException(response.errors) |
| 2714 | + return response.data |
| 2715 | + |
| 2716 | + def get_custom_fields(self, project: NotEmptyStr): |
| 2717 | + """Get the schema of the custom fields defined for the project |
| 2718 | +
|
| 2719 | + :param project: project name (e.g., “project1”) |
| 2720 | + :type project: str |
| 2721 | +
|
| 2722 | + :return: custom fields actual schema of the project |
| 2723 | + :rtype: dict |
| 2724 | +
|
| 2725 | + Response Example: |
| 2726 | + :: |
| 2727 | + { |
| 2728 | + "study_date": { |
| 2729 | + "type": "string", |
| 2730 | + "format": "date" |
| 2731 | + }, |
| 2732 | + "patient_id": { |
| 2733 | + "type": "string" |
| 2734 | + }, |
| 2735 | + "patient_sex": { |
| 2736 | + "type": "string", |
| 2737 | + "enum": [ |
| 2738 | + "male", "female" |
| 2739 | + ] |
| 2740 | + }, |
| 2741 | + "patient_age": { |
| 2742 | + "type": "number" |
| 2743 | + }, |
| 2744 | + "medical_specialist": { |
| 2745 | + "type": "string", |
| 2746 | + "format": "email" |
| 2747 | + }, |
| 2748 | + "duration": { |
| 2749 | + "type": "number", |
| 2750 | + "minimum": 10 |
| 2751 | + } |
| 2752 | + } |
| 2753 | + """ |
| 2754 | + project_name, _ = extract_project_folder(project) |
| 2755 | + response = self.controller.get_custom_schema(project_name=project) |
| 2756 | + if response.errors: |
| 2757 | + raise AppException(response.errors) |
| 2758 | + return response.data |
| 2759 | + |
| 2760 | + def delete_custom_fields(self, project: NotEmptyStr, fields: list): |
| 2761 | + """Remove custom fields from a project’s custom metadata schema. |
| 2762 | +
|
| 2763 | + :param project: project name (e.g., “project1”) |
| 2764 | + :type project: str |
| 2765 | +
|
| 2766 | + :param fields: list of field names to remove |
| 2767 | + :type fields: list of strs |
| 2768 | +
|
| 2769 | + :return: custom fields actual schema of the project |
| 2770 | + :rtype: dict |
| 2771 | +
|
| 2772 | + Request Example: |
| 2773 | + :: |
| 2774 | + client = SAClient() |
| 2775 | + client.delete_custom_fields( |
| 2776 | + project = "Medical Annotations", |
| 2777 | + fields = ["duration", patient_age] |
| 2778 | + ) |
| 2779 | +
|
| 2780 | + Response Example: |
| 2781 | + :: |
| 2782 | + { |
| 2783 | + "study_date": { |
| 2784 | + "type": "string", |
| 2785 | + "format": "date" |
| 2786 | + }, |
| 2787 | + "patient_id": { |
| 2788 | + "type": "string" |
| 2789 | + }, |
| 2790 | + "patient_sex": { |
| 2791 | + "type": "string", |
| 2792 | + "enum": [ |
| 2793 | + "male", "female" |
| 2794 | + ] |
| 2795 | + }, |
| 2796 | + "medical_specialist": { |
| 2797 | + "type": "string", |
| 2798 | + "format": "email" |
| 2799 | + } |
| 2800 | + } |
| 2801 | +
|
| 2802 | + """ |
| 2803 | + project_name, _ = extract_project_folder(project) |
| 2804 | + response = self.controller.delete_custom_schema( |
| 2805 | + project_name=project, fields=fields |
| 2806 | + ) |
| 2807 | + if response.errors: |
| 2808 | + raise AppException(response.errors) |
| 2809 | + return response.data |
0 commit comments