Skip to content

Commit

Permalink
Python docstrings are added.
Browse files Browse the repository at this point in the history
  • Loading branch information
makyol committed Jul 14, 2024
1 parent 1d02f6e commit 18d2c80
Show file tree
Hide file tree
Showing 3 changed files with 113 additions and 18 deletions.
1 change: 1 addition & 0 deletions docs/conf.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@
extensions = [
'sphinx.ext.autodoc',
'sphinx.ext.napoleon',
'sphinx.ext.viewcode'
]

templates_path = ['_templates']
Expand Down
31 changes: 29 additions & 2 deletions landusemix/indices.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,33 @@

import math


class LandUseMixIndices:
"""
A class that calculates land use mix indices.
Attributes:
land_use_areas (dict): A dictionary containing land use areas.
Methods:
entropy_index: Calculates the entropy index.
herfindahl_hirschman_index: Calculates the Herfindahl-Hirschman index.
"""

def __init__(self, land_use_areas):
"""
Initializes a LandUseMixIndices object.
Args:
land_use_areas (dict): A dictionary containing land use areas.
"""
self.land_use_areas = land_use_areas

def entropy_index(self):
"""
Calculates the entropy index.
Returns:
float: The entropy index value.
"""
total_area = sum(self.land_use_areas.values())
k = len(self.land_use_areas)
if k == 1:
Expand All @@ -16,6 +37,12 @@ def entropy_index(self):
return round(entropy, 2)

def herfindahl_hirschman_index(self):
"""
Calculates the Herfindahl-Hirschman index.
Returns:
float: The Herfindahl-Hirschman index value.
"""
total_area = sum(self.land_use_areas.values())
hhi = sum((100 * (area / total_area)) **
2 for area in self.land_use_areas.values())
Expand Down
99 changes: 83 additions & 16 deletions landusemix/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,58 +7,125 @@


def read_json(filepath):
"""Read a JSON file and return a Python dictionary."""
"""
Read a JSON file and return a Python dictionary.
Parameters:
filepath (str): The path to the JSON file.
Returns:
dict: The contents of the JSON file as a Python dictionary.
"""
with open(filepath, 'r') as f:
data = json.load(f)
return data


def convert_dataframe_to_dict(dataframe):
"""Convert a DataFrame to a Python dictionary."""
"""
Convert a DataFrame to a Python dictionary.
Parameters:
dataframe (pandas.DataFrame): The DataFrame to be converted.
Returns:
dict: The DataFrame converted to a Python dictionary.
"""
return dataframe.to_dict()


def load_geojson(filepath):
"""Load a GeoJSON file into a GeoDataFrame."""
"""
Load a GeoJSON file into a GeoDataFrame.
Parameters:
filepath (str): The path to the GeoJSON file.
Returns:
geopandas.GeoDataFrame: The loaded GeoDataFrame.
"""
return gpd.read_file(filepath)


def load_shapefile(filepath):
"""Load a Shapefile into a GeoDataFrame."""
"""
Load a Shapefile into a GeoDataFrame.
Parameters:
filepath (str): The path to the Shapefile.
Returns:
geopandas.GeoDataFrame: The loaded GeoDataFrame.
"""
return gpd.read_file(filepath)


def load_csv(filepath):
"""Load a CSV file into a DataFrame."""
"""
Load a CSV file into a DataFrame.
Parameters:
filepath (str): The path to the CSV file.
Returns:
pandas.DataFrame: The loaded DataFrame.
"""
return pd.read_csv(filepath)

# Add a function to load raster data

def load_raster(filepath):
"""Load a raster file into a GeoDataFrame."""
"""
Load a raster file into a GeoDataFrame.
Parameters:
filepath (str): The path to the raster file.
Returns:
numpy.ndarray: The loaded raster data.
"""
with rasterio.open(filepath) as src:
return src.read(1)


def load_sample_geojson():
"""Load the included sample GeoJSON file."""
sample_path = os.path.join(os.path.dirname(
__file__), 'data', 'geojson', 'multiple.geojson')
"""
Load the included sample GeoJSON file.
Returns:
geopandas.GeoDataFrame: The loaded sample GeoDataFrame.
"""
sample_path = os.path.join(os.path.dirname(__file__), 'data', 'geojson', 'multiple.geojson')
return load_geojson(sample_path)


def load_sample_shapefile():
"""Load the included sample Shapefile."""
sample_path = os.path.join(os.path.dirname(
__file__), 'data', 'shapefiles', 'multiple', 'multiple.shp')
"""
Load the included sample Shapefile.
Returns:
geopandas.GeoDataFrame: The loaded sample GeoDataFrame.
"""
sample_path = os.path.join(os.path.dirname(__file__), 'data', 'shapefiles', 'multiple', 'multiple.shp')
return load_shapefile(sample_path)


def load_sample_csv():
"""Load the included sample CSV file."""
"""
Load the included sample CSV file.
Returns:
pandas.DataFrame: The loaded sample DataFrame.
"""
sample_path = os.path.join(os.path.dirname(__file__), 'data', 'sample.csv')
return load_csv(sample_path)


def load_sample_raster():
"""Load the included sample raster file."""
"""
Load the included sample raster file.
Returns:
numpy.ndarray: The loaded sample raster data.
"""
sample_path = os.path.join(os.path.dirname(__file__), 'data', 'raster.tif')
print(sample_path)
return load_raster(sample_path)

0 comments on commit 18d2c80

Please sign in to comment.