-
Notifications
You must be signed in to change notification settings - Fork 1
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
Preliminary rdflib resource for ISMI CIDOC-CRM time spans #117
Draft
rlskoeser
wants to merge
13
commits into
develop
Choose a base branch
from
feature/cidoc-crm-dates
base: develop
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Draft
Changes from all commits
Commits
Show all changes
13 commits
Select commit
Hold shift + click to select a range
ffc1993
Add validation and type conversion to interval init
rlskoeser 710c66a
Implement & test an intersection method for UndateInterval
rlskoeser 298bb19
Make conversion to undate more reusable and extensible
rlskoeser fc4f7a9
Drop support for python 3.9 so we can use match/case
rlskoeser a4f2e7b
Add more type checks and tests
rlskoeser b09c9fc
Remove unused import
rlskoeser 4504542
Merge branch 'develop' into feature/intersection
rlskoeser f06960a
Use raise from err on type error in interval init
rlskoeser 9ee14ef
Add and test contains/in method for interval
rlskoeser 700c834
Address nitpicks flagged by @coderabbitai
rlskoeser e547b46
Preliminary rdflib resource for ISMI CIDOC-CRM dates
rlskoeser 6f10c67
Preliminary method to convert ISMI dates to Undates
rlskoeser aa38603
Use full ismi sample data as test fixture
rlskoeser File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import rdflib | ||
|
||
from undate import Undate | ||
|
||
#: CIDOC-CRM namespace | ||
CIDOC_CRM = rdflib.Namespace("http://www.cidoc-crm.org/cidoc-crm/") | ||
ISMI_DATE_TYPE = rdflib.Namespace( | ||
"http://content.mpiwg-berlin.mpg.de/ns/ismi/type/date/" | ||
) | ||
ISMI_CALENDAR_TYPE = rdflib.Namespace( | ||
"http://content.mpiwg-berlin.mpg.de/ns/ismi/type/calendar/" | ||
) | ||
|
||
|
||
class TimeSpan(rdflib.resource.Resource): | ||
@property | ||
def identified_by(self): | ||
# by default, rdflib resource value method will return another Resource | ||
return self.value(CIDOC_CRM.P1_is_identified_by) | ||
|
||
@property | ||
def label(self): | ||
# for ISMI records, label is under the crm identifier/appelation | ||
# other examples have it directly under the time span as RDFS.label | ||
return self.identified_by.value(rdflib.RDFS.label) | ||
|
||
@property | ||
def calendar(self): | ||
# for ISMI records, calendar type is associated with identifier | ||
return self.identified_by.value(CIDOC_CRM.P2_has_type).identifier | ||
|
||
@property | ||
def type(self): | ||
# CIDOC-CRM type | ||
return self.value(CIDOC_CRM.P2_has_type).identifier | ||
|
||
@property | ||
def at_some_time_within(self): | ||
return self.value(CIDOC_CRM.P82_at_some_time_within) | ||
|
||
@property | ||
def begin_of_the_begin(self): | ||
return self.value(CIDOC_CRM.P82a_begin_of_the_begin) | ||
|
||
@property | ||
def end_of_the_end(self): | ||
return self.value(CIDOC_CRM.P82b_end_of_the_end) | ||
|
||
@property | ||
def note(self): | ||
return self.value(CIDOC_CRM.P3_has_note) | ||
|
||
def to_undate(self): | ||
# convert to an undate object, if possible | ||
match self.type: | ||
# day precision | ||
case ISMI_DATE_TYPE.day: | ||
# at_some_time_within is xsd:date; use toPython method | ||
# to convert to datetime.date and then convert to undate | ||
return Undate.to_undate(self.at_some_time_within.toPython()) | ||
# TODO: should we set label before returning? | ||
|
||
# for ISMI dates, could we parse the label and preserve calendar information? | ||
|
||
@classmethod | ||
def time_spans_from_graph(cls, graph): | ||
"""Find and return all entities with CIDOC-CRM type E52 Time-Span | ||
within the rdflib graph and yield them as :class:`TimeSpan` | ||
resources.""" | ||
for timespan_uri in graph.subjects( | ||
predicate=rdflib.RDF.type, object=CIDOC_CRM["E52_Time-Span"] | ||
): | ||
yield cls(graph, timespan_uri) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
import pathlib | ||
import types | ||
|
||
import pytest | ||
import rdflib | ||
|
||
from undate import Undate, DatePrecision | ||
from undate.converters import cidoc_crm | ||
|
||
|
||
# TODO: move or copy example ismi data to test for use as a fixture | ||
ISMI_DATA_PATH = ( | ||
pathlib.Path(__file__) | ||
/ ".." | ||
/ ".." | ||
/ ".." | ||
/ "examples" | ||
/ "use-cases" | ||
/ "ismi" | ||
/ "data" | ||
/ "ismi-crm-date-samples.ttl" | ||
) | ||
|
||
DATE1_URI = rdflib.URIRef("http://content.mpiwg-berlin.mpg.de/ns/ismi/date1") | ||
|
||
|
||
@pytest.fixture | ||
def ismi_data(): | ||
g = rdflib.Graph() | ||
g.parse(ISMI_DATA_PATH) | ||
return g | ||
|
||
|
||
class TestTimeSpan: | ||
def test_properties(self, ismi_data): | ||
# initialize a time span rdflib.resource for date1 in the sample data | ||
# TODO: convert to a fixture | ||
# g = rdflib.Graph() | ||
# g.parse(ISMI_DATA_PATH) | ||
# g.parse(data=sample_data) | ||
|
||
time_span = cidoc_crm.TimeSpan(ismi_data, DATE1_URI) | ||
assert time_span.type == cidoc_crm.ISMI_DATE_TYPE.day | ||
assert time_span.label == rdflib.term.Literal("901 Rabīʿ I 14 (islamic)") | ||
assert time_span.calendar == cidoc_crm.ISMI_CALENDAR_TYPE.islamic | ||
assert time_span.at_some_time_within == rdflib.term.Literal( | ||
"1495-12-11", datatype=rdflib.XSD.date | ||
) | ||
assert time_span.note == rdflib.term.Literal( | ||
"day-precision date in islamic calendar" | ||
) | ||
|
||
def test_time_spans_from_graph(self, ismi_data): | ||
time_spans = cidoc_crm.TimeSpan.time_spans_from_graph(ismi_data) | ||
assert isinstance(time_spans, types.GeneratorType) | ||
time_spans = list(time_spans) | ||
# fixture has 9 time spans | ||
assert len(time_spans) == 9 | ||
assert isinstance(time_spans[0], cidoc_crm.TimeSpan) | ||
assert time_spans[0].identifier == DATE1_URI | ||
|
||
def test_to_undate(self, ismi_data): | ||
time_span = cidoc_crm.TimeSpan(ismi_data, DATE1_URI) | ||
ts_undate = time_span.to_undate() | ||
assert isinstance(ts_undate, Undate) | ||
# 1495-12-11"^^xsd:date ; | ||
assert ts_undate.year == "1495" | ||
assert ts_undate.month == "12" | ||
assert ts_undate.day == "11" | ||
assert ts_undate.precision == DatePrecision.DAY | ||
|
||
# if we round trip the date it comes out the same | ||
assert ts_undate.format("ISO8601") == str(time_span.at_some_time_within) |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The idea is that the Time-Span is the processable Gregorian xsd:Date while the appellation represents the date in its original calendar therefore the appellation has the calendar type and its label has the date rendered in e.g. Hijri (the Time-Span label would rather have the date rendered in Gregorian)
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks for clarifying, I sort of got what you were doing here but not fully.
How would this ideally map to an undate? Would it be two different undate objects? (they should sort the same). Or for this case would you ignore/bypass calendar conversion?