Skip to content
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

[FEAT][API] Contributions API #81

Open
youen opened this issue Mar 31, 2024 · 0 comments
Open

[FEAT][API] Contributions API #81

youen opened this issue Mar 31, 2024 · 0 comments

Comments

@youen
Copy link
Collaborator

youen commented Mar 31, 2024

Description

This API allows to get the contributions of the dataset variables within the fitted space.

Request

Method

GET

URL

/contributions

Request Parameters

None

Request Body

None

Result

Result parameters

Parameter Description Required Example
data Dictionary of dictionaries of variable contributions Yes {<br>&emsp;"feature1": {<br>&emsp;&emsp;"value1": 0.1,<br>&emsp;&emsp;"value2": 0.2<br>&emsp;},<br>&emsp;"feature2": {<br>&emsp;&emsp;"value1": 0.3,<br>&emsp;&emsp;"value2": 0.4<br>&emsp;}<br>}

Result Body

The following JSON is returned:

{
  "data": {
    "<variable 1>": {
      "<value 1>": <contribution 1>,
      "<value 2>": <contribution 2>,
    },
    "<variable 2>": {
      "<value 1>": <contribution 1>,
      "<value 2>": <contribution 2>,
    },
  }
}

Example Curl Request

curl -X GET \
  http://localhost:8080/contributions \
  -H 'Content-Type: application/json' \
  -H 'Authorization: Bearer <token>'

Curl Response

{
  "data": {
    "age": {
      "50-60": 0.5,
      "60-70": 0.3,
      "70-80": 0.2
    },
    "gender": {
      "male": 0.7,
      "female": 0.3
    }
  }
}

Go server

Struct

type Contributions struct {
	Data map[string]map[string]float64 `json:"data"`
}

Handler

func (s *Server) contributions(w http.ResponseWriter, r *http.Request) {
	ctx := r.Context()

	jobID := r.FormValue("job_id")
	if jobID == "" {
		http.Error(w, "job_id is required", http.StatusBadRequest)
		return
	}

	datasetID := r.FormValue("dataset_id")
	if datasetID == "" {
		http.Error(w, "dataset_id is required", http.StatusBadRequest)
		return
	}

	// Get the contributions from the database
	contributions, err := s.db.GetContributions(ctx, jobID, datasetID)
	if err != nil {
		http.Error(w, "failed to get contributions", http.StatusInternalServerError)
		return
	}

	// Write the contributions to the response
	if err := json.NewEncoder(w).Encode(contributions); err != nil {
		http.Error(w, "failed to encode contributions", http.StatusInternalServerError)
		return
	}
}

Links

Automated Issue Details

Dear visitor,

This issue has been automatically generated from the Octopize project avatar-python to make SIGO compatible. Please vote with a thumbs up or thumbs down to assess the quality of the automatic generation.

Best regards,
The SIGO Team

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

No branches or pull requests

1 participant