Skip to content
This repository has been archived by the owner on Dec 18, 2024. It is now read-only.

Commit

Permalink
move feature attributes to stats submodule #6
Browse files Browse the repository at this point in the history
  • Loading branch information
Guts committed Sep 4, 2019
1 parent b0ace2d commit f21d5da
Show file tree
Hide file tree
Showing 2 changed files with 104 additions and 97 deletions.
58 changes: 16 additions & 42 deletions isogeotoxlsx/isogeo2xlsx.py
Original file line number Diff line number Diff line change
Expand Up @@ -319,14 +319,6 @@ def store_metadatas(self, metadata: Metadata, share: Share = None):
"Type of metadata is not recognized/handled: {}".format(metadata.type)
)

# special analisis
if hasattr(self, "ws_fa"):
self.analisis_attributes()
# style it
self.column_width(self.ws_fa, self.columns_attributes)
self.row_height(self.ws_fa)
self.styling_cells(self.ws_fa, self.columns_attributes)

def store_md_generic(self, md: Metadata, ws: Worksheet, idx: int):
"""Exports generic metadata attributes into Excel worksheet with some dynamic
adaptations based on metadata type.
Expand Down Expand Up @@ -629,36 +621,17 @@ def store_md_generic(self, md: Metadata, ws: Worksheet, idx: int):
)

# ------------ Analisis --------------------------------------------------
def analisis_attributes(self):
"""Perform feature attributes analisis and write results into the
dedicatedWworksheet."""
# local arrays
fa_names = []
fa_types = []
fa_alias = []
fa_descr = []

# parsing
for dico_fa in self.fa_all:
for fa in dico_fa:
fa_names.append(fa.get("name"))
# fa_alias.append(fa.get("alias", "NR"))
# fa_types.append(fa.get("dataType"))
# fa_descr.append(fa.get("description", "NR"))
del fa

# stats
frq_names = Counter(fa_names)
frq_alias = Counter(fa_alias)
frq_types = Counter(fa_types)
frq_descr = Counter(fa_descr)

# write
ws = self.ws_fa
for fa in frq_names:
self.idx_fa += 1
ws["A{}".format(self.idx_fa)] = fa
ws["B{}".format(self.idx_fa)] = frq_names.get(fa)
def launch_analisis(self):
# special analisis
if hasattr(self, "ws_fa"):
self.stats.attributes(all_attributes=self.fa_all, ws_attributes=self.ws_fa)
# style it
self.column_width(self.ws_fa, self.columns_attributes)
self.row_height(self.ws_fa)
self.styling_cells(self.ws_fa, self.columns_attributes)

if hasattr(self, "ws_d"):
print("youpi")

# ------------ Customize worksheet ----------------------------------------
def headers_writer(self, ws: Worksheet, columns: ColumnPattern):
Expand Down Expand Up @@ -813,21 +786,22 @@ def tunning_worksheets(self):

isogeo.close() # close session

print(
"{}/{} metadata ready to be exported.".format(len(search.results), search.total)
)
print("{}/{} metadata to be exported.".format(len(search.results), search.total))

# instanciate th final workbook
out_workbook = Isogeo2xlsx(
lang=isogeo.lang, url_base_edit=isogeo.app_url, url_base_view=isogeo.oc_url
)
# add needed worksheets
out_workbook.set_worksheets(auto=search.tags.keys(), attributes=1)
out_workbook.set_worksheets(auto=search.tags.keys(), attributes=1, dashboard=1)

# parse search results
for md in map(Metadata.clean_attributes, search.results):
out_workbook.store_metadatas(md)

# launch analisis
out_workbook.launch_analisis()

# apply filters
out_workbook.tunning_worksheets()

Expand Down
143 changes: 88 additions & 55 deletions isogeotoxlsx/utils/stats.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
# -*- coding: UTF-8 -*-
#!/usr/bin/env python
from __future__ import absolute_import, print_function, unicode_literals


# ----------------------------------------------------------------------------
# Name: OpenCatalog to Excel
Expand All @@ -24,6 +24,7 @@

# 3rd party library
from openpyxl.chart import BarChart, Reference
from openpyxl.worksheet.worksheet import Worksheet


# ##############################################################################
Expand All @@ -50,16 +51,48 @@ def __init__(self, lang=None):
# self._ = _
super(Stats, self).__init__()

def fillfull(self):
"""Calculate fields fillfull level."""
return "HOHOHOHO"

def week_work(self, search_results=list):
"""Return histogram data to represent cataloging activity per week."""
for md in search_results:
print(md.get("type", "No md, no type"))
def attributes(self, all_attributes: list, ws_attributes: Worksheet):
"""Perform feature attributes analisis and write results into the
dedicatedWworksheet."""
idx_fa = 1
# local arrays
fa_names = []
# fa_types = []
# fa_alias = []
# fa_descr = []

# parsing
for dico_fa in all_attributes:
for fa in dico_fa:
fa_names.append(fa.get("name"))
# fa_alias.append(fa.get("alias", "NR"))
# fa_types.append(fa.get("dataType"))
# fa_descr.append(fa.get("description", "NR"))
del fa

# stats
frq_names = Counter(fa_names)
# frq_alias = Counter(fa_alias)
# frq_types = Counter(fa_types)
# frq_descr = Counter(fa_descr)

# write
ws = ws_attributes
for fa in frq_names:
idx_fa += 1
ws["A{}".format(idx_fa)] = fa
ws["B{}".format(idx_fa)] = frq_names.get(fa)

# def fillfull(self):
# """Calculate fields fillfull level."""
# return "HOHOHOHO"

# def week_work(self, search_results=list):
# """Return histogram data to represent cataloging activity per week."""
# for md in search_results:
# print(md.get("type", "No md, no type"))

return "weekly baby!"
# return "weekly baby!"

# def type_pie(self, sheet, total=20):
# """Return histogram data to represent cataloging activity per week."""
Expand Down Expand Up @@ -89,51 +122,51 @@ def week_work(self, search_results=list):

# return pie

def keywords_bar(self, sheet, results, total=20):
"""Return histogram data to represent cataloging activity per week."""
# tags parsing
li_keywords = []
li_inspire = []
for md in results:
li_keywords.extend(
(
i.get("text")
for i in md.get("keywords", [])
if i.get("_tag").startswith("keyword:is")
)
)
li_inspire.extend(
(
i.get("text")
for i in md.get("keywords", [])
if i.get("_tag").startswith("keyword:in")
)
)
keywords = Counter(li_keywords)
inspire = Counter(li_inspire)

data_k = [("Keyword", "Count")]
for k, c in keywords.most_common(50):
data_k.append((k, c))

# write data into worksheet
for row in data_k:
sheet.append(row)

bar = BarChart()
bar.type = "bar"
bar.style = 10
bar.title = "Keywords by occurrences"
bar.y_axis.title = "Occurences"
bar.x_axis.title = "Keywords"

data = Reference(sheet, min_col=2, min_row=1, max_row=50, max_col=3)
cats = Reference(sheet, min_col=1, min_row=2, max_row=50)
bar.add_data(data, titles_from_data=True)
bar.set_categories(cats)
bar.shape = 4

return bar
# def keywords_bar(self, sheet, results, total=20):
# """Return histogram data to represent cataloging activity per week."""
# # tags parsing
# li_keywords = []
# li_inspire = []
# for md in results:
# li_keywords.extend(
# (
# i.get("text")
# for i in md.get("keywords", [])
# if i.get("_tag").startswith("keyword:is")
# )
# )
# li_inspire.extend(
# (
# i.get("text")
# for i in md.get("keywords", [])
# if i.get("_tag").startswith("keyword:in")
# )
# )
# keywords = Counter(li_keywords)
# inspire = Counter(li_inspire)

# data_k = [("Keyword", "Count")]
# for k, c in keywords.most_common(50):
# data_k.append((k, c))

# # write data into worksheet
# for row in data_k:
# sheet.append(row)

# bar = BarChart()
# bar.type = "bar"
# bar.style = 10
# bar.title = "Keywords by occurrences"
# bar.y_axis.title = "Occurences"
# bar.x_axis.title = "Keywords"

# data = Reference(sheet, min_col=2, min_row=1, max_row=50, max_col=3)
# cats = Reference(sheet, min_col=1, min_row=2, max_row=50)
# bar.add_data(data, titles_from_data=True)
# bar.set_categories(cats)
# bar.shape = 4

# return bar


# ############################################################################
Expand Down

0 comments on commit f21d5da

Please sign in to comment.