# Accessing clinical records using almirah

This tutorial will guide you through the process of accessing clinical
records from the CALM Brain Resource using the `almirah` Python
library.

## Connecting to the Database

First, we'll import the necessary library and connect to the database.

```python
from almirah import Database
```

Create a database instance with the specified name, host, and backend:

```python
db = Database(name="calm-brain", host="https://calm-brain.ncbs.res.in/db-request/", backend="request")
db
```

This should output:

    <Database url: 'request:calm-brain@https://calm-brain.ncbs.res.in/db-request/'>

## Querying the Database

Next, we connect to the database using credentials:

```python
db.connect("username", "password")
```

Now, let's query a specific table, such as `psychiatric_diagnostic_profile`, and
display some of the data:

```python
df = db.query(table="psychiatric_diagnostic_profile")
df[["subject", "session", "addiction"]].head()
```

This displays the first few rows of the queried table in a DataFrame
format:

<div>
<style scoped>
    .dataframe tbody tr th:only-of-type {
        vertical-align: middle.
    }
    .dataframe tbody tr th {
        vertical-align: top.
    }
    .dataframe thead th {
        text-align: right.
    }
</style>
<table border="1" class="dataframe">
  <thead>
    <tr style="text-align: right;">
      <th></th>
      <th>subject</th>
      <th>session</th>
      <th>addiction</th>
    </tr>
  </thead>
  <tbody>
    <tr>
      <th>0</th>
      <td>D0019</td>
      <td>101</td>
      <td>0</td>
    </tr>
    <tr>
      <th>1</th>
      <td>D0019</td>
      <td>111</td>
      <td>0</td>
    </tr>
    <tr>
      <th>2</th>
      <td>D0020</td>
      <td>101</td>
      <td>0</td>
    </tr>
    <tr>
      <th>3</th>
      <td>D0020</td>
      <td>111</td>
      <td>&lt;NA&gt;</td>
    </tr>
    <tr>
      <th>4</th>
      <td>D0021</td>
      <td>101</td>
      <td>0</td>
    </tr>
  </tbody>
</table>
</div>

We can also get the total number of records in the table:

```python
len(df)
```

This returns the total number of records:

    2561

Lastly, let's find the number of records where addiction is noted:

```python
len(df[df["addiction"] == 1])
```

This gives us the count of records with addiction:

    522

This concludes the tutorial. You've learned how to connect to a
database, query its tables, and analyze the data using the `almirah`
library.
