File size: 6,697 Bytes
5a5cab0
1
2
{"task_id":"BigCodeBench\/215","complete_prompt":"import requests\nimport json\nimport pandas as pd\nimport seaborn as sns\n\n# Constants\nHEADERS = {\n    'accept': 'application\/json'\n}\n\ndef task_func(url, parameters):\n    \"\"\"\n    Retrieve data from a specific API endpoint with the provided parameters, \n    convert the data into a pandas dataframe, and draw a heatmap to show \n    the correlation between numerical characteristics. The heatmap is \n    displayed and also returned for further use or testing.\n\n    Parameters:\n    url (str): The API endpoint URL.\n    parameters (dict): The parameters to be sent with the GET request.\n\n    Returns:\n    tuple: A tuple containing:\n        - DataFrame: The pandas DataFrame containing the data.\n        - Axes: The matplotlib Axes object of the heatmap.\n\n    Raises:\n    - Thif function will raise a general Expection if the url is invalid, empty data, invalid data, and url cannot be accessed.\n\n    Requirements:\n    - requests\n    - json\n    - pandas\n    - seaborn\n\n    Example:\n    >>> df, ax = task_func('https:\/\/api.example.com\/data', {'param1': 'value1'})\n    >>> df.iloc[0]['data']\n    1\n    \"\"\"\n","instruct_prompt":"Retrieve data from a specific API endpoint with the provided parameters, convert the data into a pandas dataframe, and draw a heatmap to show the correlation between numerical characteristics. The heatmap is displayed and also returned for further use or testing.\nThe function should raise the exception for: Thif function will raise a general Expection if the url is invalid, empty data, invalid data, and url cannot be accessed.\nThe function should output with:\n    tuple: A tuple containing:\n    DataFrame: The pandas DataFrame containing the data.\n    Axes: The matplotlib Axes object of the heatmap.\nYou should write self-contained code starting with:\n```\nimport requests\nimport json\nimport pandas as pd\nimport seaborn as sns\n# Constants\nHEADERS = {\n    'accept': 'application\/json'\n}\ndef task_func(url, parameters):\n```","canonical_solution":"    try:\n        response = requests.get(url, params=parameters, headers=HEADERS)\n        data = json.loads(response.text)\n\n        df = pd.DataFrame(data)\n        corr = df.corr()\n\n        ax = sns.heatmap(corr, annot=True, cmap='coolwarm')\n        return df, ax\n    except Exception as e:\n        raise(e)","code_prompt":"import requests\nimport json\nimport pandas as pd\nimport seaborn as sns\n# Constants\nHEADERS = {\n    'accept': 'application\/json'\n}\ndef task_func(url, parameters):\n","test":"# Importing the refined function from the refined_function.py file\nimport unittest\nfrom unittest.mock import patch, Mock\nimport json\nimport requests\nclass TestCases(unittest.TestCase):\n    @patch('requests.get')\n    def test_valid_request(self, mock_get):\n        mock_response = Mock()\n        mock_response.text = '{\"data\": [1, 2, 3], \"data_2\": [4, 5, 6]}'\n        mock_get.return_value = mock_response\n        url = 'https:\/\/api.example.com\/data'\n        params = {'param1': 'value1'}\n        df, ax = task_func(url, params)\n        self.assertIsNotNone(df)\n        self.assertIsNotNone(ax)\n        # Check the content of the DataFrame\n        self.assertTrue(df.equals(pd.DataFrame({\"data\": [1, 2, 3], \"data_2\": [4, 5, 6]})))\n        # Check the correlation matrix\n        corr_matrix = df.corr()\n        # Check the data plotted on the heatmap\n        for i in range(df.shape[1]):\n            for j in range(df.shape[1]):\n                self.assertEqual(ax.texts[i * df.shape[1] + j].get_text(), str(int(corr_matrix.iloc[i, j])))\n    @patch('requests.get')\n    def test_empty_response(self, mock_get):\n        mock_response = Mock()\n        mock_response.text = '{}'\n        mock_get.return_value = mock_response\n        url = 'https:\/\/api.example.com\/empty_data'\n        params = {'param1': 'value1'}\n        with self.assertRaises(Exception):\n            task_func(url, params)\n    @patch('requests.get')\n    def test_invalid_url(self, mock_get):\n        mock_get.side_effect = requests.exceptions.RequestException\n        url = 'https:\/\/api.invalid.com\/data'\n        params = {'param1': 'value1'}\n        with self.assertRaises(Exception):\n            task_func(url, params)\n    @patch('requests.get')\n    def test_invalid_json_response(self, mock_get):\n        mock_response = Mock()\n        mock_response.text = 'Invalid JSON'\n        mock_get.return_value = mock_response\n        url = 'https:\/\/api.example.com\/invalid_json'\n        params = {'param1': 'value1'}\n        with self.assertRaises(Exception):\n            task_func(url, params)\n    @patch('requests.get')\n    def test_valid_request_with_no_params(self, mock_get):\n        mock_response = Mock()\n        mock_response.text = '{\"data\": [1, 2, 3, 4, 5]}'\n        mock_get.return_value = mock_response\n        url = 'https:\/\/api.example.com\/data'\n        df, ax = task_func(url, {})\n        self.assertIsNotNone(df)\n        self.assertIsNotNone(ax)\n    @patch('requests.get')\n    def test_plot_attributes(self, mock_get):\n        # Test attributes of the plot\n        mock_response = Mock()\n        mock_response.text = '{\"id\": [1, 2, 3, 4, 5], \"user\": [6, 7, 8, 9, 10]}'\n        mock_get.return_value = mock_response\n        url = 'https:\/\/api.example.com\/data'\n        params = {'param1': 'value1'}\n        df, ax = task_func(url, params)\n        self.assertTrue(hasattr(ax, 'get_xlabel'))\n        self.assertTrue(hasattr(ax, 'get_ylabel'))\n        self.assertTrue(hasattr(ax, 'get_title'))","entry_point":"task_func","doc_struct":"{\"description\": [\"Retrieve data from a specific API endpoint with the provided parameters,\", \"convert the data into a pandas dataframe, and draw a heatmap to show\", \"the correlation between numerical characteristics. The heatmap is\", \"displayed and also returned for further use or testing.\"], \"notes\": [], \"params\": [\"url (str): The API endpoint URL.\", \"parameters (dict): The parameters to be sent with the GET request.\"], \"returns\": [\"tuple: A tuple containing:\", \"DataFrame: The pandas DataFrame containing the data.\", \"Axes: The matplotlib Axes object of the heatmap.\"], \"reqs\": [\"requests\", \"json\", \"pandas\", \"seaborn\"], \"raises\": [\"Thif function will raise a general Expection if the url is invalid, empty data, invalid data, and url cannot be accessed.\"], \"examples\": [\">>> df, ax = task_func('https:\/\/api.example.com\/data', {'param1': 'value1'})\", \">>> df.iloc[0]['data']\", \"1\"]}","libs":"['pandas', 'json', 'requests', 'seaborn']"}